UNPKG

zeptomatch-explode

Version:

A little utility for exploding a zeptomatch-flavored glob into its dynamic and static parts.

27 lines (26 loc) 1.05 kB
/* IMPORT */ /* HELPERS */ const SIMPLE_RE = /(\/?)([ a-zA-Z0-9._-]*)(?:\{([ a-zA-Z0-9._-]+(?:,[ a-zA-Z0-9._-]+)*)\})?([ a-zA-Z0-9._-]*)(\/(?=.))/gsy; /* MAIN */ //TODO: Account for more things, like classes, ranges, escapes, optionals, etc. const explodeStart = (glob) => { let index = 0; let length = glob.length; let statics = []; while (index < length) { SIMPLE_RE.lastIndex = index; const match = SIMPLE_RE.exec(glob); if (!match) break; const [_, slash, prefix, multiple, suffix] = match; if (!prefix && !multiple && !suffix) break; const values = multiple ? multiple.split(',').map(value => `${slash}${prefix}${value}${suffix}`) : [`${slash}${prefix}${suffix}`]; statics = statics.length ? statics.flatMap(prefix => values.map(value => `${prefix}/${value}`)) : values; index = SIMPLE_RE.lastIndex; } const dynamic = index ? glob.slice(index) : glob; return { statics, dynamic }; }; /* EXPORT */ export default explodeStart;