Skip to content

Commit

Permalink
feat: Add checkExcludePath
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Jun 10, 2024
1 parent 97398d8 commit c671041
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ExcludeFilepathConfig = string | RegExp | ((filepath: string) => boolean);
37 changes: 37 additions & 0 deletions src/utils.ts
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;
}
44 changes: 44 additions & 0 deletions test/unit/utils.test.ts
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);
});
});
});
});

0 comments on commit c671041

Please sign in to comment.