From 0488df3795e90a844dc00458dc16c1fc0a69e1fd Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Sun, 8 Sep 2024 23:46:11 +0200 Subject: [PATCH] add `caseSensitiveMatch` option --- README.md | 1 + src/index.ts | 9 ++++++--- test/index.test.ts | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e2b16ac..72b2c96 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ globSync({ patterns: ['src/**/*.ts', '!**/*.d.ts'], deep: 3 }); - `absolute`: Whether to return absolute paths. Defaults to `false`. - `dot`: Whether to allow entries starting with a dot. Defaults to `false`. - `deep`: Maximum depth of a directory. Defaults to `Infinity`. +- `caseSensitiveMatch`: Whether to match in case-sensitive mode. Defaults to `true`. - `expandDirectories`: Whether to expand directories. Disable to best match `fast-glob`. Defaults to `true`. - `onlyDirectories`: Enable to only return directories. Disables `onlyFiles` if set. Defaults to `false`. - `onlyFiles`: Enable to only return files. Defaults to `true`. diff --git a/src/index.ts b/src/index.ts index 9b8d5c1..b0585b9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ export interface GlobOptions { ignore?: string[]; dot?: boolean; deep?: number; + caseSensitiveMatch?: boolean; expandDirectories?: boolean; onlyDirectories?: boolean; onlyFiles?: boolean; @@ -105,8 +106,8 @@ function getRelativePath(path: string, cwd: string, root: string) { } function processPath(path: string, cwd: string, root: string, isDirectory: boolean, absolute?: boolean) { - const relativePath = absolute ? path.slice(root.length + 1) : path; - //if (absolute) console.log({ path, cwd, root, relativePath }); + const relativePath = (absolute ? path.slice(root.length + 1) : path) || './'; + if (root === cwd) { return isDirectory ? relativePath.slice(0, -1) : relativePath; } @@ -127,11 +128,13 @@ function crawl(options: GlobOptions, cwd: string, sync: boolean) { const matcher = picomatch(processed.match, { dot: options.dot, + nocase: options.caseSensitiveMatch === false, ignore: processed.ignore }); const exclude = picomatch(processed.ignore, { - dot: options.dot + dot: options.dot, + nocase: options.caseSensitiveMatch === false }); const fdirOptions: Partial = { diff --git a/test/index.test.ts b/test/index.test.ts index 3a3ace7..ef4b1b6 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -63,6 +63,21 @@ test('ignore option', async () => { assert.deepEqual(files.sort(), ['a/a.txt']); }); +test('caseSensitiveMatch', async () => { + const files = await glob({ patterns: ['**/A.TXT'], caseSensitiveMatch: false, cwd }); + assert.deepEqual(files.sort(), ['a/a.txt', 'b/a.txt']); +}); + +test('caseSensitiveMatch (default)', async () => { + const files = await glob({ patterns: ['**/A.TXT'], cwd }); + assert.deepEqual(files.sort(), []); +}); + +test('caseSensitiveMatch with ignore', async () => { + const files = await glob({ patterns: ['**/A.TXT'], ignore: ['B/**'], caseSensitiveMatch: false, cwd }); + assert.deepEqual(files.sort(), ['a/a.txt']); +}); + test('onlyDirectories option', async () => { const files = await glob({ patterns: ['a'], onlyDirectories: true, cwd }); assert.deepEqual(files.sort(), ['a/']);