UNPKG

tiny-readdir-glob

Version:

A simple promisified recursive readdir function, with support for globs.

41 lines (40 loc) 1.1 kB
/* MAIN */ const castArray = (value) => { return Array.isArray(value) ? value : [value]; }; const getGlobsPartition = (globs) => { const positives = []; const negatives = []; const bangsRe = /^!+/; for (const glob of globs) { const match = glob.match(bangsRe); if (match) { const bangsNr = match[0].length; const bucket = bangsNr % 2 === 0 ? positives : negatives; bucket.push(glob.slice(bangsNr)); } else { positives.push(glob); } } if (globs.length && !positives.length) { positives.push('**'); } return [uniq(positives), uniq(negatives)]; }; const isFunction = (value) => { return typeof value === 'function'; }; const isRegExp = (value) => { return value instanceof RegExp; }; const isString = (value) => { return typeof value === 'string'; }; const uniq = (values) => { if (values.length < 2) return values; return Array.from(new Set(values)); }; /* EXPORT */ export { castArray, getGlobsPartition, isFunction, isRegExp, isString, uniq };