index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*!
  2. * expand-brackets <https://github.com/jonschlinkert/expand-brackets>
  3. *
  4. * Copyright (c) 2015 Jon Schlinkert.
  5. * Licensed under the MIT license.
  6. */
  7. 'use strict';
  8. var isPosixBracket = require('is-posix-bracket');
  9. /**
  10. * POSIX character classes
  11. */
  12. var POSIX = {
  13. alnum: 'a-zA-Z0-9',
  14. alpha: 'a-zA-Z',
  15. blank: ' \\t',
  16. cntrl: '\\x00-\\x1F\\x7F',
  17. digit: '0-9',
  18. graph: '\\x21-\\x7E',
  19. lower: 'a-z',
  20. print: '\\x20-\\x7E',
  21. punct: '-!"#$%&\'()\\*+,./:;<=>?@[\\]^_`{|}~',
  22. space: ' \\t\\r\\n\\v\\f',
  23. upper: 'A-Z',
  24. word: 'A-Za-z0-9_',
  25. xdigit: 'A-Fa-f0-9',
  26. };
  27. /**
  28. * Expose `brackets`
  29. */
  30. module.exports = brackets;
  31. function brackets(str) {
  32. if (!isPosixBracket(str)) {
  33. return str;
  34. }
  35. var negated = false;
  36. if (str.indexOf('[^') !== -1) {
  37. negated = true;
  38. str = str.split('[^').join('[');
  39. }
  40. if (str.indexOf('[!') !== -1) {
  41. negated = true;
  42. str = str.split('[!').join('[');
  43. }
  44. var a = str.split('[');
  45. var b = str.split(']');
  46. var imbalanced = a.length !== b.length;
  47. var parts = str.split(/(?::\]\[:|\[?\[:|:\]\]?)/);
  48. var len = parts.length, i = 0;
  49. var end = '', beg = '';
  50. var res = [];
  51. // start at the end (innermost) first
  52. while (len--) {
  53. var inner = parts[i++];
  54. if (inner === '^[!' || inner === '[!') {
  55. inner = '';
  56. negated = true;
  57. }
  58. var prefix = negated ? '^' : '';
  59. var ch = POSIX[inner];
  60. if (ch) {
  61. res.push('[' + prefix + ch + ']');
  62. } else if (inner) {
  63. if (/^\[?\w-\w\]?$/.test(inner)) {
  64. if (i === parts.length) {
  65. res.push('[' + prefix + inner);
  66. } else if (i === 1) {
  67. res.push(prefix + inner + ']');
  68. } else {
  69. res.push(prefix + inner);
  70. }
  71. } else {
  72. if (i === 1) {
  73. beg += inner;
  74. } else if (i === parts.length) {
  75. end += inner;
  76. } else {
  77. res.push('[' + prefix + inner + ']');
  78. }
  79. }
  80. }
  81. }
  82. var result = res.join('|');
  83. var rlen = res.length || 1;
  84. if (rlen > 1) {
  85. result = '(?:' + result + ')';
  86. rlen = 1;
  87. }
  88. if (beg) {
  89. rlen++;
  90. if (beg.charAt(0) === '[') {
  91. if (imbalanced) {
  92. beg = '\\[' + beg.slice(1);
  93. } else {
  94. beg += ']';
  95. }
  96. }
  97. result = beg + result;
  98. }
  99. if (end) {
  100. rlen++;
  101. if (end.slice(-1) === ']') {
  102. if (imbalanced) {
  103. end = end.slice(0, end.length - 1) + '\\]';
  104. } else {
  105. end = '[' + end;
  106. }
  107. }
  108. result += end;
  109. }
  110. if (rlen > 1) {
  111. result = result.split('][').join(']|[');
  112. if (result.indexOf('|') !== -1 && !/\(\?/.test(result)) {
  113. result = '(?:' + result + ')';
  114. }
  115. }
  116. result = result.replace(/\[+=|=\]+/g, '\\b');
  117. return result;
  118. }
  119. brackets.makeRe = function(pattern) {
  120. try {
  121. return new RegExp(brackets(pattern));
  122. } catch (err) {}
  123. };
  124. brackets.isMatch = function(str, pattern) {
  125. try {
  126. return brackets.makeRe(pattern).test(str);
  127. } catch (err) {
  128. return false;
  129. }
  130. };
  131. brackets.match = function(arr, pattern) {
  132. var len = arr.length, i = 0;
  133. var res = arr.slice();
  134. var re = brackets.makeRe(pattern);
  135. while (i < len) {
  136. var ele = arr[i++];
  137. if (!re.test(ele)) {
  138. continue;
  139. }
  140. res.splice(i, 1);
  141. }
  142. return res;
  143. };