選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.js 4.7KB

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