'use strict';

import React from 'react';

export function highlight(regExp = null, text = null, title = '') {
    if (regExp === null || text === null) {
        return <span title={title}>{text}</span>;
    }
    let highlightedText = String(text).replace(regExp as any, handleTextParts);
    return <span title={title} dangerouslySetInnerHTML={{__html: highlightedText}} />;
}

export function handleTextParts() {
    let parts = [].slice.call(arguments, 1, -2);
    return parts.map(addHighLightTags).join('');
}

export function addHighLightTags(part, index) {
    return index % 2 === 1 ? `<strong>${part}</strong>` : part;
}
