CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
index.js171 linesDownload Raw Back to braces
1'use strict';2 3const stringify = require('./lib/stringify');4const compile = require('./lib/compile');5const expand = require('./lib/expand');6const parse = require('./lib/parse');7 8/**9 * Expand the given pattern or create a regex-compatible string.10 *11 * ```js12 * const braces = require('braces');13 * console.log(braces('{a,b,c}', { compile: true })); //=> ['(a|b|c)']14 * console.log(braces('{a,b,c}')); //=> ['a', 'b', 'c']15 * ```16 * @param {String} `str`17 * @param {Object} `options`18 * @return {String}19 * @api public20 */21 22const braces = (input, options = {}) => {23  let output = [];24 25  if (Array.isArray(input)) {26    for (const pattern of input) {27      const result = braces.create(pattern, options);28      if (Array.isArray(result)) {29        output.push(...result);30      } else {31        output.push(result);32      }33    }34  } else {35    output = [].concat(braces.create(input, options));36  }37 38  if (options && options.expand === true && options.nodupes === true) {39    output = [...new Set(output)];40  }41  return output;42};43 44/**45 * Parse the given `str` with the given `options`.46 *47 * ```js48 * // braces.parse(pattern, [, options]);49 * const ast = braces.parse('a/{b,c}/d');50 * console.log(ast);51 * ```52 * @param {String} pattern Brace pattern to parse53 * @param {Object} options54 * @return {Object} Returns an AST55 * @api public56 */57 58braces.parse = (input, options = {}) => parse(input, options);59 60/**61 * Creates a braces string from an AST, or an AST node.62 *63 * ```js64 * const braces = require('braces');65 * let ast = braces.parse('foo/{a,b}/bar');66 * console.log(stringify(ast.nodes[2])); //=> '{a,b}'67 * ```68 * @param {String} `input` Brace pattern or AST.69 * @param {Object} `options`70 * @return {Array} Returns an array of expanded values.71 * @api public72 */73 74braces.stringify = (input, options = {}) => {75  if (typeof input === 'string') {76    return stringify(braces.parse(input, options), options);77  }78  return stringify(input, options);79};80 81/**82 * Compiles a brace pattern into a regex-compatible, optimized string.83 * This method is called by the main [braces](#braces) function by default.84 *85 * ```js86 * const braces = require('braces');87 * console.log(braces.compile('a/{b,c}/d'));88 * //=> ['a/(b|c)/d']89 * ```90 * @param {String} `input` Brace pattern or AST.91 * @param {Object} `options`92 * @return {Array} Returns an array of expanded values.93 * @api public94 */95 96braces.compile = (input, options = {}) => {97  if (typeof input === 'string') {98    input = braces.parse(input, options);99  }100  return compile(input, options);101};102 103/**104 * Expands a brace pattern into an array. This method is called by the105 * main [braces](#braces) function when `options.expand` is true. Before106 * using this method it's recommended that you read the [performance notes](#performance))107 * and advantages of using [.compile](#compile) instead.108 *109 * ```js110 * const braces = require('braces');111 * console.log(braces.expand('a/{b,c}/d'));112 * //=> ['a/b/d', 'a/c/d'];113 * ```114 * @param {String} `pattern` Brace pattern115 * @param {Object} `options`116 * @return {Array} Returns an array of expanded values.117 * @api public118 */119 120braces.expand = (input, options = {}) => {121  if (typeof input === 'string') {122    input = braces.parse(input, options);123  }124 125  let result = expand(input, options);126 127  // filter out empty strings if specified128  if (options.noempty === true) {129    result = result.filter(Boolean);130  }131 132  // filter out duplicates if specified133  if (options.nodupes === true) {134    result = [...new Set(result)];135  }136 137  return result;138};139 140/**141 * Processes a brace pattern and returns either an expanded array142 * (if `options.expand` is true), a highly optimized regex-compatible string.143 * This method is called by the main [braces](#braces) function.144 *145 * ```js146 * const braces = require('braces');147 * console.log(braces.create('user-{200..300}/project-{a,b,c}-{1..10}'))148 * //=> 'user-(20[0-9]|2[1-9][0-9]|300)/project-(a|b|c)-([1-9]|10)'149 * ```150 * @param {String} `pattern` Brace pattern151 * @param {Object} `options`152 * @return {Array} Returns an array of expanded values.153 * @api public154 */155 156braces.create = (input, options = {}) => {157  if (input === '' || input.length < 3) {158    return [input];159  }160 161  return options.expand !== true162    ? braces.compile(input, options)163    : braces.expand(input, options);164};165 166/**167 * Expose "braces"168 */169 170module.exports = braces;171