From c968d528fc28391b720542fe1e657839dc9cf6bc Mon Sep 17 00:00:00 2001 From: Kaspersky <50753378+CasP0@users.noreply.github.com> Date: Sat, 18 Jan 2025 19:23:03 -0500 Subject: [PATCH] Add ignore option to tiny-glob Add an `ignore` option to the `Options` type and update the `walk` and `glob` functions to support it. * **index.js** - Add `ignore` option to the `Options` type. - Update `walk` function to skip files and directories matching `ignore` patterns. - Update `glob` function to pass `ignore` option to `walk` function. * **index.d.ts** - Add `ignore` option to the `Options` type. * **sync.js** - Add `ignore` option to the `Options` type. - Update `walk` function to skip files and directories matching `ignore` patterns. - Update `glob` function to pass `ignore` option to `walk` function. * **sync.d.ts** - Add `ignore` option to the `Options` type. * **readme.md** - Update `API` section to include the new `ignore` option. * **test/glob.js** - Add test cases to verify the `ignore` option functionality. --- index.d.ts | 1 + index.js | 5 ++++- readme.md | 7 +++++++ sync.d.ts | 1 + sync.js | 5 ++++- test/glob.js | 21 +++++++++++++++++++++ 6 files changed, 38 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index ef1a5d9..de00f45 100644 --- a/index.d.ts +++ b/index.d.ts @@ -4,6 +4,7 @@ type Options = { absolute?: boolean; filesOnly?: boolean; flush?: boolean; + ignore?: string[]; }; type FilePath = string; diff --git a/index.js b/index.js index b57c2ad..78d7cb8 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ async function walk(output, prefix, lexer, opts, dirname='', level=0) { const rgx = lexer.segments[level]; const dir = resolve(opts.cwd, prefix, dirname); const files = await readdir(dir); - const { dot, filesOnly } = opts; + const { dot, filesOnly, ignore } = opts; let i=0, len=files.length, file; let fullpath, relpath, stats, isMatch; @@ -21,6 +21,7 @@ async function walk(output, prefix, lexer, opts, dirname='', level=0) { fullpath = join(dir, file=files[i]); relpath = dirname ? join(dirname, file) : file; if (!dot && isHidden.test(relpath)) continue; + if (ignore && ignore.some(pattern => new RegExp(pattern).test(relpath))) continue; isMatch = lexer.regex.test(relpath); if ((stats=CACHE[relpath]) === void 0) { @@ -48,6 +49,7 @@ async function walk(output, prefix, lexer, opts, dirname='', level=0) { * @param {Boolean} [options.absolute=false] Return absolute paths * @param {Boolean} [options.filesOnly=false] Do not include folders if true * @param {Boolean} [options.flush=false] Reset cache object + * @param {Array} [options.ignore=[]] Patterns to ignore * @returns {Array} array containing matching files */ module.exports = async function (str, opts={}) { @@ -56,6 +58,7 @@ module.exports = async function (str, opts={}) { let glob = globalyzer(str); opts.cwd = opts.cwd || '.'; + opts.ignore = opts.ignore || []; if (!glob.isGlob) { try { diff --git a/readme.md b/readme.md index 15d2df6..b6947df 100644 --- a/readme.md +++ b/readme.md @@ -108,6 +108,13 @@ Default: `false` Flush the internal cache object. +#### options.ignore + +Type: `Array`
+Default: `[]` + +Patterns to ignore. + ## Windows diff --git a/sync.d.ts b/sync.d.ts index da1b061..19a9d30 100644 --- a/sync.d.ts +++ b/sync.d.ts @@ -4,6 +4,7 @@ type Options = { absolute?: boolean; filesOnly?: boolean; flush?: boolean; + ignore?: string[]; }; type FilePath = string; diff --git a/sync.js b/sync.js index d2080df..4b29c0f 100644 --- a/sync.js +++ b/sync.js @@ -10,7 +10,7 @@ function walk(output, prefix, lexer, opts, dirname='', level=0) { const rgx = lexer.segments[level]; const dir = resolve(opts.cwd, prefix, dirname); const files = fs.readdirSync(dir); - const { dot, filesOnly } = opts; + const { dot, filesOnly, ignore } = opts; let i=0, len=files.length, file; let fullpath, relpath, stats, isMatch; @@ -19,6 +19,7 @@ function walk(output, prefix, lexer, opts, dirname='', level=0) { fullpath = join(dir, file=files[i]); relpath = dirname ? join(dirname, file) : file; if (!dot && isHidden.test(relpath)) continue; + if (ignore && ignore.some(pattern => new RegExp(pattern).test(relpath))) continue; isMatch = lexer.regex.test(relpath); if ((stats=CACHE[relpath]) === void 0) { @@ -46,6 +47,7 @@ function walk(output, prefix, lexer, opts, dirname='', level=0) { * @param {Boolean} [options.absolute=false] Return absolute paths * @param {Boolean} [options.filesOnly=false] Do not include folders if true * @param {Boolean} [options.flush=false] Reset cache object + * @param {Array} [options.ignore=[]] Patterns to ignore * @returns {Array} array containing matching files */ module.exports = function (str, opts={}) { @@ -54,6 +56,7 @@ module.exports = function (str, opts={}) { let glob = globalyzer(str); opts.cwd = opts.cwd || '.'; + opts.ignore = opts.ignore || []; if (!glob.isGlob) { try { diff --git a/test/glob.js b/test/glob.js index c19e6f9..6a9c749 100644 --- a/test/glob.js +++ b/test/glob.js @@ -228,3 +228,24 @@ test('glob: deep match with higher level siblings', async t => { 'test/fixtures/deep/b/c/d' ]); }); + +test('glob: options.ignore', async t => { + t.plan(2); + + await isMatch(t, 'test/fixtures/**/*.{js,txt}', { ignore: ['**/a.js', '**/a.txt'] }, [ + 'test/fixtures/b.js', + 'test/fixtures/b.txt', + 'test/fixtures/one/b.txt', + 'test/fixtures/one/child/a.js', + 'test/fixtures/one/child/a.txt', + 'test/fixtures/two/a.txt' + ]); + + await isMatch(t, 'test/fixtures/**/*.{js,txt}', { ignore: ['**/one/**'] }, [ + 'test/fixtures/a.js', + 'test/fixtures/a.txt', + 'test/fixtures/b.js', + 'test/fixtures/b.txt', + 'test/fixtures/two/a.txt' + ]); +});