All files / lib/util helper.js

96% Statements 72/75
70% Branches 7/10
81.25% Functions 13/16
100% Lines 65/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151                                                  282x 282x 282x 282x       3450x 8567x 3704x 3704x           3450x       42x     88x 88x 88x 88x 5896x 5896x   88x       112x 112x 112x 2254x 2254x   112x       112x 112x 112x 334x   112x       282x 282x 3450x 3450x 3450x 74074x   3450x       42x 42x 42x 42x 42x 42x   42x         42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x     42x 42x 42x                 42x     42x     2381x 42x 2381x 42x 2381x   42x                        
/*
 * Copyright (c) AXA Group Operations Spain S.A.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
class LookupTable {
  constructor(data, prop) {
    this.length = 0;
    this.prop = prop;
    this.table = {};
    this.build(data);
  }
 
  buildDatum(datum) {
    Object.keys(datum[this.prop]).forEach(key => {
      if (this.table[key] === undefined) {
        this.table[key] = this.length;
        this.length += 1;
      }
    });
  }
 
  build(data) {
    data.forEach(datum => this.buildDatum(datum));
  }
}
 
const toArray = values => Object.keys(values).map(key => values[key]);
 
function toHash(hash) {
  const currentLookup = {};
  let index = 0;
  const keys = Object.keys(hash);
  for (let i = 0; i < keys.length; i += 1) {
    currentLookup[keys[i]] = index;
    index += 1;
  }
  return currentLookup;
}
 
function lookupToArray(currentLookup, object, arrayLength) {
  const result = new Float32Array(arrayLength);
  const keys = Object.keys(currentLookup);
  for (let i = 0; i < keys.length; i += 1) {
    const key = keys[i];
    result[currentLookup[key]] = object[key] || 0;
  }
  return result;
}
 
function lookupToObject(currentLookup, array) {
  const result = {};
  const keys = Object.keys(currentLookup);
  for (let i = 0; i < keys.length; i += 1) {
    result[keys[i]] = array[currentLookup[keys[i]]];
  }
  return result;
}
 
function getTypedArrayFn(table) {
  const { length } = Object.keys(table);
  return v => {
    const result = new Float32Array(length);
    const keys = Object.keys(table);
    for (let i = 0; i < length; i += 1) {
      result[table[keys[i]]] = v[keys[i]] || 0;
    }
    return result;
  };
}
 
const rsAstralRange = '\\ud800-\\udfff';
const rsComboMarksRange = '\\u0300-\\u036f';
const reComboHalfMarksRange = '\\ufe20-\\ufe2f';
const rsComboSymbolsRange = '\\u20d0-\\u20ff';
const rsComboMarksExtendedRange = '\\u1ab0-\\u1aff';
const rsComboMarksSupplementRange = '\\u1dc0-\\u1dff';
const rsComboRange =
  rsComboMarksRange +
  reComboHalfMarksRange +
  rsComboSymbolsRange +
  rsComboMarksExtendedRange +
  rsComboMarksSupplementRange;
const rsVarRange = '\\ufe0e\\ufe0f';
const rsAstral = `[${rsAstralRange}]`;
const rsCombo = `[${rsComboRange}]`;
const rsFitz = '\\ud83c[\\udffb-\\udfff]';
const rsModifier = `(?:${rsCombo}|${rsFitz})`;
const rsNonAstral = `[^${rsAstralRange}]`;
const rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}';
const rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]';
const rsZWJ = '\\u200d';
const reOptMod = `${rsModifier}?`;
const rsOptVar = `[${rsVarRange}]?`;
const rsOptJoin = `(?:${rsZWJ}(?:${[rsNonAstral, rsRegional, rsSurrPair].join(
  '|'
)})${rsOptVar + reOptMod})*`;
const rsSeq = rsOptVar + reOptMod + rsOptJoin;
const rsNonAstralCombo = `${rsNonAstral}${rsCombo}?`;
const rsSymbol = `(?:${[
  rsNonAstralCombo,
  rsCombo,
  rsRegional,
  rsSurrPair,
  rsAstral,
].join('|')})`;
 
/* eslint-disable no-misleading-character-class */
const reHasUnicode = RegExp(
  `[${rsZWJ + rsAstralRange + rsComboRange + rsVarRange}]`
);
const reUnicode = RegExp(`${rsFitz}(?=${rsFitz})|${rsSymbol + rsSeq}`, 'g');
/* eslint-enable no-misleading-character-class */
 
const hasUnicode = str => reHasUnicode.test(str);
const unicodeToArray = str => str.match(reUnicode) || [];
const asciiToArray = str => str.split('');
const stringToArray = str =>
  hasUnicode(str) ? unicodeToArray(str) : asciiToArray(str);
 
module.exports = {
  LookupTable,
  toArray,
  toHash,
  lookupToArray,
  lookupToObject,
  getTypedArrayFn,
  hasUnicode,
  unicodeToArray,
  asciiToArray,
  stringToArray,
};