index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. var balanced = require('balanced-match');
  2. module.exports = expandTop;
  3. var escSlash = '\0SLASH'+Math.random()+'\0';
  4. var escOpen = '\0OPEN'+Math.random()+'\0';
  5. var escClose = '\0CLOSE'+Math.random()+'\0';
  6. var escComma = '\0COMMA'+Math.random()+'\0';
  7. var escPeriod = '\0PERIOD'+Math.random()+'\0';
  8. function numeric(str) {
  9. return parseInt(str, 10) == str
  10. ? parseInt(str, 10)
  11. : str.charCodeAt(0);
  12. }
  13. function escapeBraces(str) {
  14. return str.split('\\\\').join(escSlash)
  15. .split('\\{').join(escOpen)
  16. .split('\\}').join(escClose)
  17. .split('\\,').join(escComma)
  18. .split('\\.').join(escPeriod);
  19. }
  20. function unescapeBraces(str) {
  21. return str.split(escSlash).join('\\')
  22. .split(escOpen).join('{')
  23. .split(escClose).join('}')
  24. .split(escComma).join(',')
  25. .split(escPeriod).join('.');
  26. }
  27. // Basically just str.split(","), but handling cases
  28. // where we have nested braced sections, which should be
  29. // treated as individual members, like {a,{b,c},d}
  30. function parseCommaParts(str) {
  31. if (!str)
  32. return [''];
  33. var parts = [];
  34. var m = balanced('{', '}', str);
  35. if (!m)
  36. return str.split(',');
  37. var pre = m.pre;
  38. var body = m.body;
  39. var post = m.post;
  40. var p = pre.split(',');
  41. p[p.length-1] += '{' + body + '}';
  42. var postParts = parseCommaParts(post);
  43. if (post.length) {
  44. p[p.length-1] += postParts.shift();
  45. p.push.apply(p, postParts);
  46. }
  47. parts.push.apply(parts, p);
  48. return parts;
  49. }
  50. function expandTop(str) {
  51. if (!str)
  52. return [];
  53. // I don't know why Bash 4.3 does this, but it does.
  54. // Anything starting with {} will have the first two bytes preserved
  55. // but *only* at the top level, so {},a}b will not expand to anything,
  56. // but a{},b}c will be expanded to [a}c,abc].
  57. // One could argue that this is a bug in Bash, but since the goal of
  58. // this module is to match Bash's rules, we escape a leading {}
  59. if (str.substr(0, 2) === '{}') {
  60. str = '\\{\\}' + str.substr(2);
  61. }
  62. return expand(escapeBraces(str), true).map(unescapeBraces);
  63. }
  64. function embrace(str) {
  65. return '{' + str + '}';
  66. }
  67. function isPadded(el) {
  68. return /^-?0\d/.test(el);
  69. }
  70. function lte(i, y) {
  71. return i <= y;
  72. }
  73. function gte(i, y) {
  74. return i >= y;
  75. }
  76. function expand(str, isTop) {
  77. var expansions = [];
  78. var m = balanced('{', '}', str);
  79. if (!m) return [str];
  80. // no need to expand pre, since it is guaranteed to be free of brace-sets
  81. var pre = m.pre;
  82. var post = m.post.length
  83. ? expand(m.post, false)
  84. : [''];
  85. if (/\$$/.test(m.pre)) {
  86. for (var k = 0; k < post.length; k++) {
  87. var expansion = pre+ '{' + m.body + '}' + post[k];
  88. expansions.push(expansion);
  89. }
  90. } else {
  91. var isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
  92. var isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
  93. var isSequence = isNumericSequence || isAlphaSequence;
  94. var isOptions = m.body.indexOf(',') >= 0;
  95. if (!isSequence && !isOptions) {
  96. // {a},b}
  97. if (m.post.match(/,.*\}/)) {
  98. str = m.pre + '{' + m.body + escClose + m.post;
  99. return expand(str);
  100. }
  101. return [str];
  102. }
  103. var n;
  104. if (isSequence) {
  105. n = m.body.split(/\.\./);
  106. } else {
  107. n = parseCommaParts(m.body);
  108. if (n.length === 1) {
  109. // x{{a,b}}y ==> x{a}y x{b}y
  110. n = expand(n[0], false).map(embrace);
  111. if (n.length === 1) {
  112. return post.map(function(p) {
  113. return m.pre + n[0] + p;
  114. });
  115. }
  116. }
  117. }
  118. // at this point, n is the parts, and we know it's not a comma set
  119. // with a single entry.
  120. var N;
  121. if (isSequence) {
  122. var x = numeric(n[0]);
  123. var y = numeric(n[1]);
  124. var width = Math.max(n[0].length, n[1].length)
  125. var incr = n.length == 3
  126. ? Math.abs(numeric(n[2]))
  127. : 1;
  128. var test = lte;
  129. var reverse = y < x;
  130. if (reverse) {
  131. incr *= -1;
  132. test = gte;
  133. }
  134. var pad = n.some(isPadded);
  135. N = [];
  136. for (var i = x; test(i, y); i += incr) {
  137. var c;
  138. if (isAlphaSequence) {
  139. c = String.fromCharCode(i);
  140. if (c === '\\')
  141. c = '';
  142. } else {
  143. c = String(i);
  144. if (pad) {
  145. var need = width - c.length;
  146. if (need > 0) {
  147. var z = new Array(need + 1).join('0');
  148. if (i < 0)
  149. c = '-' + z + c.slice(1);
  150. else
  151. c = z + c;
  152. }
  153. }
  154. }
  155. N.push(c);
  156. }
  157. } else {
  158. N = [];
  159. for (var j = 0; j < n.length; j++) {
  160. N.push.apply(N, expand(n[j], false));
  161. }
  162. }
  163. for (var j = 0; j < N.length; j++) {
  164. for (var k = 0; k < post.length; k++) {
  165. var expansion = pre + N[j] + post[k];
  166. if (!isTop || isSequence || expansion)
  167. expansions.push(expansion);
  168. }
  169. }
  170. }
  171. return expansions;
  172. }