123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- 'use strict';
-
-
-
- var toRegex = require('to-regex');
- var unique = require('array-unique');
- var extend = require('extend-shallow');
- var define = require('define-property');
-
-
-
- var compilers = require('./lib/compilers');
- var parsers = require('./lib/parsers');
- var Braces = require('./lib/braces');
- var utils = require('./lib/utils');
- var MAX_LENGTH = 1024 * 64;
- var cache = {};
-
-
-
- function braces(pattern, options) {
- var key = utils.createKey(String(pattern), options);
- var arr = [];
-
- var disabled = options && options.cache === false;
- if (!disabled && cache.hasOwnProperty(key)) {
- return cache[key];
- }
-
- if (Array.isArray(pattern)) {
- for (var i = 0; i < pattern.length; i++) {
- arr.push.apply(arr, braces.create(pattern[i], options));
- }
- } else {
- arr = braces.create(pattern, options);
- }
-
- if (options && options.nodupes === true) {
- arr = unique(arr);
- }
-
- if (!disabled) {
- cache[key] = arr;
- }
- return arr;
- }
-
-
-
- braces.expand = function(pattern, options) {
- return braces.create(pattern, extend({}, options, {expand: true}));
- };
-
-
-
- braces.optimize = function(pattern, options) {
- return braces.create(pattern, options);
- };
-
-
-
- braces.create = function(pattern, options) {
- if (typeof pattern !== 'string') {
- throw new TypeError('expected a string');
- }
-
- if (pattern.length >= MAX_LENGTH) {
- throw new Error('expected pattern to be less than ' + MAX_LENGTH + ' characters');
- }
-
- function create() {
- if (pattern === '' || pattern.length < 3) {
- return [pattern];
- }
-
- if (utils.isEmptySets(pattern)) {
- return [];
- }
-
- if (utils.isQuotedString(pattern)) {
- return [pattern.slice(1, -1)];
- }
-
- var proto = new Braces(options);
- var result = !options || options.expand !== true
- ? proto.optimize(pattern, options)
- : proto.expand(pattern, options);
-
-
- var arr = result.output;
-
-
- if (options && options.noempty === true) {
- arr = arr.filter(Boolean);
- }
-
-
- if (options && options.nodupes === true) {
- arr = unique(arr);
- }
-
- define(arr, 'result', result);
- return arr;
- }
-
- return memoize('create', pattern, options, create);
- };
-
-
-
- braces.makeRe = function(pattern, options) {
- if (typeof pattern !== 'string') {
- throw new TypeError('expected a string');
- }
-
- if (pattern.length >= MAX_LENGTH) {
- throw new Error('expected pattern to be less than ' + MAX_LENGTH + ' characters');
- }
-
- function makeRe() {
- var arr = braces(pattern, options);
- var opts = extend({strictErrors: false}, options);
- return toRegex(arr, opts);
- }
-
- return memoize('makeRe', pattern, options, makeRe);
- };
-
-
-
- braces.parse = function(pattern, options) {
- var proto = new Braces(options);
- return proto.parse(pattern, options);
- };
-
-
-
- braces.compile = function(ast, options) {
- var proto = new Braces(options);
- return proto.compile(ast, options);
- };
-
-
-
- braces.clearCache = function() {
- cache = braces.cache = {};
- };
-
-
-
- function memoize(type, pattern, options, fn) {
- var key = utils.createKey(type + ':' + pattern, options);
- var disabled = options && options.cache === false;
- if (disabled) {
- braces.clearCache();
- return fn(pattern, options);
- }
-
- if (cache.hasOwnProperty(key)) {
- return cache[key];
- }
-
- var res = fn(pattern, options);
- cache[key] = res;
- return res;
- }
-
-
-
- braces.Braces = Braces;
- braces.compilers = compilers;
- braces.parsers = parsers;
- braces.cache = cache;
-
-
-
- module.exports = braces;
|