You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. exports.isInteger = num => {
  3. if (typeof num === 'number') {
  4. return Number.isInteger(num);
  5. }
  6. if (typeof num === 'string' && num.trim() !== '') {
  7. return Number.isInteger(Number(num));
  8. }
  9. return false;
  10. };
  11. /**
  12. * Find a node of the given type
  13. */
  14. exports.find = (node, type) => node.nodes.find(node => node.type === type);
  15. /**
  16. * Find a node of the given type
  17. */
  18. exports.exceedsLimit = (min, max, step = 1, limit) => {
  19. if (limit === false) return false;
  20. if (!exports.isInteger(min) || !exports.isInteger(max)) return false;
  21. return ((Number(max) - Number(min)) / Number(step)) >= limit;
  22. };
  23. /**
  24. * Escape the given node with '\\' before node.value
  25. */
  26. exports.escapeNode = (block, n = 0, type) => {
  27. const node = block.nodes[n];
  28. if (!node) return;
  29. if ((type && node.type === type) || node.type === 'open' || node.type === 'close') {
  30. if (node.escaped !== true) {
  31. node.value = '\\' + node.value;
  32. node.escaped = true;
  33. }
  34. }
  35. };
  36. /**
  37. * Returns true if the given brace node should be enclosed in literal braces
  38. */
  39. exports.encloseBrace = node => {
  40. if (node.type !== 'brace') return false;
  41. if ((node.commas >> 0 + node.ranges >> 0) === 0) {
  42. node.invalid = true;
  43. return true;
  44. }
  45. return false;
  46. };
  47. /**
  48. * Returns true if a brace node is invalid.
  49. */
  50. exports.isInvalidBrace = block => {
  51. if (block.type !== 'brace') return false;
  52. if (block.invalid === true || block.dollar) return true;
  53. if ((block.commas >> 0 + block.ranges >> 0) === 0) {
  54. block.invalid = true;
  55. return true;
  56. }
  57. if (block.open !== true || block.close !== true) {
  58. block.invalid = true;
  59. return true;
  60. }
  61. return false;
  62. };
  63. /**
  64. * Returns true if a node is an open or close node
  65. */
  66. exports.isOpenOrClose = node => {
  67. if (node.type === 'open' || node.type === 'close') {
  68. return true;
  69. }
  70. return node.open === true || node.close === true;
  71. };
  72. /**
  73. * Reduce an array of text nodes.
  74. */
  75. exports.reduce = nodes => nodes.reduce((acc, node) => {
  76. if (node.type === 'text') acc.push(node.value);
  77. if (node.type === 'range') node.type = 'text';
  78. return acc;
  79. }, []);
  80. /**
  81. * Flatten an array
  82. */
  83. exports.flatten = (...args) => {
  84. const result = [];
  85. const flat = arr => {
  86. for (let i = 0; i < arr.length; i++) {
  87. const ele = arr[i];
  88. if (Array.isArray(ele)) {
  89. flat(ele);
  90. continue;
  91. }
  92. if (ele !== undefined) {
  93. result.push(ele);
  94. }
  95. }
  96. return result;
  97. };
  98. flat(args);
  99. return result;
  100. };