[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | exports.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 type
|
---|
| 15 | */
|
---|
| 16 |
|
---|
| 17 | exports.find = (node, type) => node.nodes.find(node => node.type === type);
|
---|
| 18 |
|
---|
| 19 | /**
|
---|
| 20 | * Find a node of the given type
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | exports.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.value
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | exports.escapeNode = (block, n = 0, type) => {
|
---|
| 34 | let 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 braces
|
---|
| 47 | */
|
---|
| 48 |
|
---|
| 49 | exports.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 |
|
---|
| 62 | exports.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 node
|
---|
| 78 | */
|
---|
| 79 |
|
---|
| 80 | exports.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 |
|
---|
| 91 | exports.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 array
|
---|
| 99 | */
|
---|
| 100 |
|
---|
| 101 | exports.flatten = (...args) => {
|
---|
| 102 | const result = [];
|
---|
| 103 | const flat = arr => {
|
---|
| 104 | for (let i = 0; i < arr.length; i++) {
|
---|
| 105 | let ele = arr[i];
|
---|
| 106 | Array.isArray(ele) ? flat(ele, result) : ele !== void 0 && result.push(ele);
|
---|
| 107 | }
|
---|
| 108 | return result;
|
---|
| 109 | };
|
---|
| 110 | flat(args);
|
---|
| 111 | return result;
|
---|
| 112 | };
|
---|