-
-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): update ignores to advanced pattern (#2035)
- Loading branch information
1 parent
d0e5d6a
commit 0fb0d63
Showing
6 changed files
with
30 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,10 @@ | ||
/** | ||
* Creates a predicate to test storage keys `foo:bar:baz` against configured `ignores` patterns | ||
*/ | ||
export function makeIgnored (ignores: string[], experimental = false): (key: string) => boolean { | ||
// filter empty | ||
ignores = ignores.map(e => e) | ||
|
||
// 2.7+ supports free regexp + slashes | ||
if (experimental) { | ||
const rxAll = ['/\\.', '/-', ...ignores].map(p => new RegExp(p)) | ||
return function isIgnored (key: string): boolean { | ||
const path = '/' + key.replaceAll(':', '/') | ||
return rxAll.some(rx => rx.test(path)) | ||
} | ||
} | ||
|
||
// 2.6 prefixed by unstorage delimiters | ||
const rxAll = ['\\.', '-', ...ignores].map(p => new RegExp(`^${p}|:${p}`)) | ||
export function makeIgnored (ignores: string[]): (key: string) => boolean { | ||
const rxAll = ['/\\.', '/-', ...ignores.filter(p => p)].map(p => new RegExp(p)) | ||
return function isIgnored (key: string): boolean { | ||
return rxAll.some(rx => rx.test(key)) | ||
const path = '/' + key.replaceAll(':', '/') | ||
return rxAll.some(rx => rx.test(path)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,46 @@ | ||
import { describe, test, expect } from 'vitest' | ||
import { makeIgnored } from '../../src/runtime/utils/config' | ||
|
||
function run (key: string, pattern: string, result: boolean, experimental: boolean) { | ||
const predicate = makeIgnored([pattern], experimental) | ||
const label: string = experimental ? 'new format' : 'old format' | ||
test(label, () => { | ||
expect(predicate(key)).toBe(result) | ||
}) | ||
} | ||
|
||
function runOld (pattern: string, key: string, result: boolean) { | ||
return run(key, pattern, result, false) | ||
} | ||
|
||
function runNew (pattern: string, key: string, result: boolean) { | ||
return run(key, pattern, result, true) | ||
function run (pattern: string, key: string, result: boolean) { | ||
const isIgnored = makeIgnored([pattern]) | ||
expect(isIgnored(key)).toBe(result) | ||
} | ||
|
||
export const testIgnores = () => { | ||
describe('Ignores Options', () => { | ||
describe('any file or folder that contains the word "hidden"', () => { | ||
test('ignore any file or folder that contains the word "hidden"', () => { | ||
const key = 'source:content:my-hidden-folder:index.md' | ||
runOld('.+hidden', key, true) | ||
runNew('hidden', key, true) | ||
run('hidden', key, true) | ||
}) | ||
|
||
describe('any file or folder prefixed with the word "hidden"', () => { | ||
test('ignore any file or folder prefixed with the word "hidden"', () => { | ||
const key = 'source:content:hidden-folder:index.md' | ||
runOld('hidden', key, true) | ||
runNew('/hidden', key, true) | ||
run('/hidden', key, true) | ||
}) | ||
|
||
describe('any folder that exactly matches the word "hidden"', () => { | ||
test('ignore any folder that exactly matches the word "hidden"', () => { | ||
const key = 'source:content:hidden:index.md' | ||
runOld('hidden:', key, true) | ||
runNew('/hidden/', key, true) | ||
run('/hidden/', key, true) | ||
}) | ||
|
||
describe('any file path matching "/path/to/file"', () => { | ||
test('ignore any file path matching "/path/to/file"', () => { | ||
const key = 'source:content:path:to:file.md' | ||
runOld('path:to:file', key, true) | ||
runNew('/path/to/file', key, true) | ||
run('/path/to/file', key, true) | ||
}) | ||
|
||
describe('any file with the extension ".bak"', () => { | ||
test('ignore any file with the extension ".bak"', () => { | ||
const key = 'source:content:path:to:ignored.bak' | ||
runOld('.+\\.bak$', key, true) | ||
runNew('\\.bak$', key, true) | ||
run('\\.bak$', key, true) | ||
}) | ||
|
||
describe('any file with a leading dash', () => { | ||
test('ignore any file with a leading dash', () => { | ||
const key = 'source:content:path:to:-dash.txt' | ||
runOld('', key, true) | ||
runNew('', key, true) | ||
run('', key, true) | ||
}) | ||
|
||
describe('any file with a leading dot', () => { | ||
test('ignore any file with a leading dot', () => { | ||
const key = 'source:content:path:to:.dot.txt' | ||
runOld('', key, true) | ||
runNew('', key, true) | ||
run('', key, true) | ||
}) | ||
}) | ||
} |