Source: utils.js

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
};