react / wstein / node_modules / browserify / node_modules / glob / node_modules / minimatch / node_modules / brace-expansion / index.js
80551 viewsvar concatMap = require('concat-map');1var balanced = require('balanced-match');23module.exports = expandTop;45var escSlash = '\0SLASH'+Math.random()+'\0';6var escOpen = '\0OPEN'+Math.random()+'\0';7var escClose = '\0CLOSE'+Math.random()+'\0';8var escComma = '\0COMMA'+Math.random()+'\0';9var escPeriod = '\0PERIOD'+Math.random()+'\0';1011function numeric(str) {12return parseInt(str, 10) == str13? parseInt(str, 10)14: str.charCodeAt(0);15}1617function escapeBraces(str) {18return str.split('\\\\').join(escSlash)19.split('\\{').join(escOpen)20.split('\\}').join(escClose)21.split('\\,').join(escComma)22.split('\\.').join(escPeriod);23}2425function unescapeBraces(str) {26return str.split(escSlash).join('\\')27.split(escOpen).join('{')28.split(escClose).join('}')29.split(escComma).join(',')30.split(escPeriod).join('.');31}323334// Basically just str.split(","), but handling cases35// where we have nested braced sections, which should be36// treated as individual members, like {a,{b,c},d}37function parseCommaParts(str) {38if (!str)39return [''];4041var parts = [];42var m = balanced('{', '}', str);4344if (!m)45return str.split(',');4647var pre = m.pre;48var body = m.body;49var post = m.post;50var p = pre.split(',');5152p[p.length-1] += '{' + body + '}';53var postParts = parseCommaParts(post);54if (post.length) {55p[p.length-1] += postParts.shift();56p.push.apply(p, postParts);57}5859parts.push.apply(parts, p);6061return parts;62}6364function expandTop(str) {65if (!str)66return [];6768return expand(escapeBraces(str), true).map(unescapeBraces);69}7071function identity(e) {72return e;73}7475function embrace(str) {76return '{' + str + '}';77}78function isPadded(el) {79return /^-?0\d/.test(el);80}8182function lte(i, y) {83return i <= y;84}85function gte(i, y) {86return i >= y;87}8889function expand(str, isTop) {90var expansions = [];9192var m = balanced('{', '}', str);93if (!m || /\$$/.test(m.pre)) return [str];9495var isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);96var isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);97var isSequence = isNumericSequence || isAlphaSequence;98var isOptions = /^(.*,)+(.+)?$/.test(m.body);99if (!isSequence && !isOptions) {100// {a},b}101if (m.post.match(/,.*}/)) {102str = m.pre + '{' + m.body + escClose + m.post;103return expand(str);104}105return [str];106}107108var n;109if (isSequence) {110n = m.body.split(/\.\./);111} else {112n = parseCommaParts(m.body);113if (n.length === 1) {114// x{{a,b}}y ==> x{a}y x{b}y115n = expand(n[0], false).map(embrace);116if (n.length === 1) {117var post = m.post.length118? expand(m.post, false)119: [''];120return post.map(function(p) {121return m.pre + n[0] + p;122});123}124}125}126127// at this point, n is the parts, and we know it's not a comma set128// with a single entry.129130// no need to expand pre, since it is guaranteed to be free of brace-sets131var pre = m.pre;132var post = m.post.length133? expand(m.post, false)134: [''];135136var N;137138if (isSequence) {139var x = numeric(n[0]);140var y = numeric(n[1]);141var width = Math.max(n[0].length, n[1].length)142var incr = n.length == 3143? Math.abs(numeric(n[2]))144: 1;145var test = lte;146var reverse = y < x;147if (reverse) {148incr *= -1;149test = gte;150}151var pad = n.some(isPadded);152153N = [];154155for (var i = x; test(i, y); i += incr) {156var c;157if (isAlphaSequence) {158c = String.fromCharCode(i);159if (c === '\\')160c = '';161} else {162c = String(i);163if (pad) {164var need = width - c.length;165if (need > 0) {166var z = new Array(need + 1).join('0');167if (i < 0)168c = '-' + z + c.slice(1);169else170c = z + c;171}172}173}174N.push(c);175}176} else {177N = concatMap(n, function(el) { return expand(el, false) });178}179180for (var j = 0; j < N.length; j++) {181for (var k = 0; k < post.length; k++) {182var expansion = pre + N[j] + post[k];183if (!isTop || isSequence || expansion)184expansions.push(expansion);185}186}187188return expansions;189}190191192193