-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type ExcludeFilepathConfig = string | RegExp | ((filepath: string) => boolean); |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { ExcludeFilepathConfig } from './types'; | ||
|
||
/** | ||
* Check if filepath should be excluded based on a config | ||
*/ | ||
export function checkExcludeFilepath( | ||
filepath: string, | ||
config?: ExcludeFilepathConfig | Array<ExcludeFilepathConfig> | ||
): boolean { | ||
if (!config) { | ||
return false; | ||
} | ||
|
||
if (Array.isArray(config)) { | ||
let res = false; | ||
|
||
for (let i = 0; i <= config.length - 1 && res === false; i++) { | ||
res = checkExcludeFilepath(filepath, config[i]); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
if (typeof config === 'function') { | ||
return config(filepath); | ||
} | ||
|
||
if (typeof config === 'string') { | ||
return Boolean(filepath.match(config)); | ||
} | ||
|
||
if ('test' in config) { | ||
return config.test(filepath); | ||
} | ||
|
||
return false; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { describe, test, expect } from 'vitest'; | ||
|
||
import { checkExcludeFilepath } from '../../src/utils'; | ||
|
||
describe('utils', () => { | ||
describe('checkExcludeFilepath', () => { | ||
const testCases: Array<{ input: Parameters<typeof checkExcludeFilepath>; output: ReturnType<typeof checkExcludeFilepath>}> = [ | ||
{ | ||
input: ['./assets/vendor.js'], | ||
output: false, | ||
}, | ||
{ | ||
input: ['./assets/vendor.js', 'vendor'], | ||
output: true, | ||
}, | ||
{ | ||
input: ['./assets/vendor.js', 'unknown'], | ||
output: false, | ||
}, | ||
{ | ||
input: ['./assets/vendor.js', /vendor/], | ||
output: true, | ||
}, | ||
{ | ||
input: ['./assets/vendor.js', () => true], | ||
output: true, | ||
}, | ||
{ | ||
input: ['./assets/vendor.js', ['main', /vendor/]], | ||
output: true, | ||
}, | ||
{ | ||
input: ['./assets/vendor.js', ['main', /unknown/, () => false]], | ||
output: false, | ||
}, | ||
]; | ||
|
||
testCases.forEach(({ input, output }) => { | ||
test(`Should return "${output}" when called with: "${input.join('", "')}"`, () => { | ||
expect(checkExcludeFilepath(...input)).toEqual(output); | ||
}); | ||
}); | ||
}); | ||
}); |