index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. 'use strict';
  2. /**
  3. * Local dependencies
  4. */
  5. var compilers = require('./lib/compilers');
  6. var parsers = require('./lib/parsers');
  7. /**
  8. * Module dependencies
  9. */
  10. var debug = require('debug')('expand-brackets');
  11. var extend = require('extend-shallow');
  12. var Snapdragon = require('snapdragon');
  13. var toRegex = require('to-regex');
  14. /**
  15. * Parses the given POSIX character class `pattern` and returns a
  16. * string that can be used for creating regular expressions for matching.
  17. *
  18. * @param {String} `pattern`
  19. * @param {Object} `options`
  20. * @return {Object}
  21. * @api public
  22. */
  23. function brackets(pattern, options) {
  24. debug('initializing from <%s>', __filename);
  25. var res = brackets.create(pattern, options);
  26. return res.output;
  27. }
  28. /**
  29. * Takes an array of strings and a POSIX character class pattern, and returns a new
  30. * array with only the strings that matched the pattern.
  31. *
  32. * ```js
  33. * var brackets = require('expand-brackets');
  34. * console.log(brackets.match(['1', 'a', 'ab'], '[[:alpha:]]'));
  35. * //=> ['a']
  36. *
  37. * console.log(brackets.match(['1', 'a', 'ab'], '[[:alpha:]]+'));
  38. * //=> ['a', 'ab']
  39. * ```
  40. * @param {Array} `arr` Array of strings to match
  41. * @param {String} `pattern` POSIX character class pattern(s)
  42. * @param {Object} `options`
  43. * @return {Array}
  44. * @api public
  45. */
  46. brackets.match = function(arr, pattern, options) {
  47. arr = [].concat(arr);
  48. var opts = extend({}, options);
  49. var isMatch = brackets.matcher(pattern, opts);
  50. var len = arr.length;
  51. var idx = -1;
  52. var res = [];
  53. while (++idx < len) {
  54. var ele = arr[idx];
  55. if (isMatch(ele)) {
  56. res.push(ele);
  57. }
  58. }
  59. if (res.length === 0) {
  60. if (opts.failglob === true) {
  61. throw new Error('no matches found for "' + pattern + '"');
  62. }
  63. if (opts.nonull === true || opts.nullglob === true) {
  64. return [pattern.split('\\').join('')];
  65. }
  66. }
  67. return res;
  68. };
  69. /**
  70. * Returns true if the specified `string` matches the given
  71. * brackets `pattern`.
  72. *
  73. * ```js
  74. * var brackets = require('expand-brackets');
  75. *
  76. * console.log(brackets.isMatch('a.a', '[[:alpha:]].[[:alpha:]]'));
  77. * //=> true
  78. * console.log(brackets.isMatch('1.2', '[[:alpha:]].[[:alpha:]]'));
  79. * //=> false
  80. * ```
  81. * @param {String} `string` String to match
  82. * @param {String} `pattern` Poxis pattern
  83. * @param {String} `options`
  84. * @return {Boolean}
  85. * @api public
  86. */
  87. brackets.isMatch = function(str, pattern, options) {
  88. return brackets.matcher(pattern, options)(str);
  89. };
  90. /**
  91. * Takes a POSIX character class pattern and returns a matcher function. The returned
  92. * function takes the string to match as its only argument.
  93. *
  94. * ```js
  95. * var brackets = require('expand-brackets');
  96. * var isMatch = brackets.matcher('[[:lower:]].[[:upper:]]');
  97. *
  98. * console.log(isMatch('a.a'));
  99. * //=> false
  100. * console.log(isMatch('a.A'));
  101. * //=> true
  102. * ```
  103. * @param {String} `pattern` Poxis pattern
  104. * @param {String} `options`
  105. * @return {Boolean}
  106. * @api public
  107. */
  108. brackets.matcher = function(pattern, options) {
  109. var re = brackets.makeRe(pattern, options);
  110. return function(str) {
  111. return re.test(str);
  112. };
  113. };
  114. /**
  115. * Create a regular expression from the given `pattern`.
  116. *
  117. * ```js
  118. * var brackets = require('expand-brackets');
  119. * var re = brackets.makeRe('[[:alpha:]]');
  120. * console.log(re);
  121. * //=> /^(?:[a-zA-Z])$/
  122. * ```
  123. * @param {String} `pattern` The pattern to convert to regex.
  124. * @param {Object} `options`
  125. * @return {RegExp}
  126. * @api public
  127. */
  128. brackets.makeRe = function(pattern, options) {
  129. var res = brackets.create(pattern, options);
  130. var opts = extend({strictErrors: false}, options);
  131. return toRegex(res.output, opts);
  132. };
  133. /**
  134. * Parses the given POSIX character class `pattern` and returns an object
  135. * with the compiled `output` and optional source `map`.
  136. *
  137. * ```js
  138. * var brackets = require('expand-brackets');
  139. * console.log(brackets('[[:alpha:]]'));
  140. * // { options: { source: 'string' },
  141. * // input: '[[:alpha:]]',
  142. * // state: {},
  143. * // compilers:
  144. * // { eos: [Function],
  145. * // noop: [Function],
  146. * // bos: [Function],
  147. * // not: [Function],
  148. * // escape: [Function],
  149. * // text: [Function],
  150. * // posix: [Function],
  151. * // bracket: [Function],
  152. * // 'bracket.open': [Function],
  153. * // 'bracket.inner': [Function],
  154. * // 'bracket.literal': [Function],
  155. * // 'bracket.close': [Function] },
  156. * // output: '[a-zA-Z]',
  157. * // ast:
  158. * // { type: 'root',
  159. * // errors: [],
  160. * // nodes: [ [Object], [Object], [Object] ] },
  161. * // parsingErrors: [] }
  162. * ```
  163. * @param {String} `pattern`
  164. * @param {Object} `options`
  165. * @return {Object}
  166. * @api public
  167. */
  168. brackets.create = function(pattern, options) {
  169. var snapdragon = (options && options.snapdragon) || new Snapdragon(options);
  170. compilers(snapdragon);
  171. parsers(snapdragon);
  172. var ast = snapdragon.parse(pattern, options);
  173. ast.input = pattern;
  174. var res = snapdragon.compile(ast, options);
  175. res.input = pattern;
  176. return res;
  177. };
  178. /**
  179. * Expose `brackets` constructor, parsers and compilers
  180. */
  181. brackets.compilers = compilers;
  182. brackets.parsers = parsers;
  183. /**
  184. * Expose `brackets`
  185. * @type {Function}
  186. */
  187. module.exports = brackets;