All files utils.js

50% Statements 8/16
50% Branches 1/2
40% Functions 2/5
50% Lines 7/14

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 49151x               2x                                 36x 36x 46x   36x                           2x          
const $ = id => /^[$]/.test(id)? id: '$' + id;
 
/**
 * Converts a dAst object to JSON format.
 *
 * @param {Object} dAst - The dAst object to be converted.
 * @returns {Object} - The converted JSON object.
 */
const dast2json =  (dAst) => {
  let jsonDAst = dAst;
  jsonDAst.dependencies = Array.from(dAst.dependencies);
  jsonDAst.symbolTable = Array.from(dAst.symbolTable);
  jsonDAst.used = Array.from(dAst.used);
 
  return jsonDAst;
}
 
/**
 * Calculates the difference between two sets.
 *
 * @param {Set} setA - The first set.
 * @param {Set} setB - The second set.
 * @returns {Set} - The difference between setA and setB.
 */
function difference(setA, setB) {
  const _difference = new Set(setA);
  for (const elem of setB) {
    _difference.delete(elem);
  }
  return _difference;
}
 
/**
 * Creates a regular expression pattern from an array of names.
 *
 * @param {string[]} names - The array of names.
 * @returns {RegExp} The regular expression pattern.
 */
function RegexpFromNames(names) {
  let escaped = names.map(n => n.replace(/[$*.^]/, '[$&]')) 
  return new RegExp('\\b'+escaped.join('|')+'\\b');
}
 
module.exports = {
  $, 
  dast2json,
  difference,
  RegexpFromNames
};