CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
utils.js123 linesDownload Raw Back to lib
1'use strict';2 3exports.isInteger = num => {4  if (typeof num === 'number') {5    return Number.isInteger(num);6  }7  if (typeof num === 'string' && num.trim() !== '') {8    return Number.isInteger(Number(num));9  }10  return false;11};12 13/**14 * Find a node of the given type15 */16 17exports.find = (node, type) => node.nodes.find(node => node.type === type);18 19/**20 * Find a node of the given type21 */22 23exports.exceedsLimit = (min, max, step = 1, limit) => {24  if (limit === false) return false;25  if (!exports.isInteger(min) || !exports.isInteger(max)) return false;26  return ((Number(max) - Number(min)) / Number(step)) >= limit;27};28 29/**30 * Escape the given node with '\\' before node.value31 */32 33exports.escapeNode = (block, n = 0, type) => {34  const node = block.nodes[n];35  if (!node) return;36 37  if ((type && node.type === type) || node.type === 'open' || node.type === 'close') {38    if (node.escaped !== true) {39      node.value = '\\' + node.value;40      node.escaped = true;41    }42  }43};44 45/**46 * Returns true if the given brace node should be enclosed in literal braces47 */48 49exports.encloseBrace = node => {50  if (node.type !== 'brace') return false;51  if ((node.commas >> 0 + node.ranges >> 0) === 0) {52    node.invalid = true;53    return true;54  }55  return false;56};57 58/**59 * Returns true if a brace node is invalid.60 */61 62exports.isInvalidBrace = block => {63  if (block.type !== 'brace') return false;64  if (block.invalid === true || block.dollar) return true;65  if ((block.commas >> 0 + block.ranges >> 0) === 0) {66    block.invalid = true;67    return true;68  }69  if (block.open !== true || block.close !== true) {70    block.invalid = true;71    return true;72  }73  return false;74};75 76/**77 * Returns true if a node is an open or close node78 */79 80exports.isOpenOrClose = node => {81  if (node.type === 'open' || node.type === 'close') {82    return true;83  }84  return node.open === true || node.close === true;85};86 87/**88 * Reduce an array of text nodes.89 */90 91exports.reduce = nodes => nodes.reduce((acc, node) => {92  if (node.type === 'text') acc.push(node.value);93  if (node.type === 'range') node.type = 'text';94  return acc;95}, []);96 97/**98 * Flatten an array99 */100 101exports.flatten = (...args) => {102  const result = [];103 104  const flat = arr => {105    for (let i = 0; i < arr.length; i++) {106      const ele = arr[i];107 108      if (Array.isArray(ele)) {109        flat(ele);110        continue;111      }112 113      if (ele !== undefined) {114        result.push(ele);115      }116    }117    return result;118  };119 120  flat(args);121  return result;122};123