all files / helpers/ text-highlight.js

100% Statements 20/20
100% Branches 16/16
100% Functions 2/2
100% Lines 20/20
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                                                                    17×     17× 17×   17×     12×                             17× 17×      
/**
 * Created by konradjurk on 19.09.15.
 */
 
import Ember from 'ember';
import _lang from 'lodash/lang';
import _array from 'lodash/array';
import _object from 'lodash/object';
 
import {isSafari} from 'ember-text-highlight/-private/env-detection';
 
import indicesImplementation from 'ember-text-highlight/-private/indices-implementation';
import regexImplementation from 'ember-text-highlight/-private/regex-implementation';
 
// RegEx Implementation exceeds Indices Implementation on large texts independent of environment
export const MAX_VALUE_LENGTH_FOR_INDICES_IMPL = 250;
 
const DEFAULT_OPTIONS = {
  caseSensitive: false
};
 
/**
 * Text Highlight Helper
 *
 * Mark all occurences of a string in input string with <span class="mark">
 *
 * Usage:
 * {{{text-highlight value query=myQuery}}}
 *
 * Expected Input:
 * `params` = ['valueString']
 * `options` = {query: 'queryString'}
 *
 * Picks the best implementation concerning input and environment.
 */
export default Ember.Helper.helper(function (params = [], options = DEFAULT_OPTIONS) {
  let value, query;
 
  // validate and transform input
  const queryIsValid = _lang.isString(query = options.query) && !_lang.isEmpty(query.trim());
  const valueIsValid = _lang.isString(value = findValueAndTransformToStringIfApplicable(params)) && !_lang.isEmpty(value.trim());
 
  if (!queryIsValid) {
    return Ember.String.htmlSafe(value);
  }
 
  if (!valueIsValid) {
    return '';
  }
 
  options = _object.merge(DEFAULT_OPTIONS, _lang.clone(options));
 
  // as of a certain value length, regular expressions will likely start to exceed the performance of our indices
  // implementation
  if (value.length > MAX_VALUE_LENGTH_FOR_INDICES_IMPL) {
    return regexImplementation(value, query, options);
  }
 
  // pick the best implementation for this environment (indices, except Safari)
  if (!window._text_highlight_fastest_impl) {
    if (isSafari()) {
      window._text_highlight_fastest_impl = regexImplementation;
    } else {
      window._text_highlight_fastest_impl = indicesImplementation;
    }
  }
 
  return window._text_highlight_fastest_impl(value, query, options);
});
 
function findValueAndTransformToStringIfApplicable(params) {
  const value = _array.nth(params, 0);
  return _lang.isNumber(value) ? value.toString() : value;
}