all files / -private/ regex-implementation.js

100% Statements 8/8
100% Branches 2/2
100% Functions 2/2
100% Lines 8/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                              28×   28× 16×     28× 28×       28×      
import Ember from 'ember';
 
/**
 * RegEx Implementation
 *
 * Highlight `value` input String with String.replace(RegExp) implementation that can be more performant on many
 * large texts than the indices implementation.
 *
 * @param {String} value The template string to highlight matches if any
 * @param {String} query The string to search in `value`
 * @param {Object} options
 * @param {Boolean} options.caseSensitive
 *
 * @returns {Ember.String.htmlSafe}
 */
export default function (value, query, options) {
  let regexFlags = 'gm'; // g=global, m=multi-line
 
  if (!options.caseSensitive) {
    regexFlags += 'i';
  }
 
  const regexp = new RegExp(escapeRegExp(query), regexFlags);
  return Ember.String.htmlSafe(value.replace(regexp, '<span class="mark">$&</span>'));
}
 
function escapeRegExp(str) {
  /* eslint-disable no-useless-escape */
  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}