-
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.
refactor: simplify filters by moving back to picomatch
- Loading branch information
Showing
4 changed files
with
44 additions
and
44 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import path from 'path'; | ||
import picomatch from 'picomatch'; | ||
|
||
import { type StatsModule } from '~core/data/types'; | ||
|
||
type ModuleFilters = { | ||
include?: string; | ||
exclude?: string; | ||
}; | ||
|
||
/** | ||
* Filter the modules based on the include and exclude glob patterns. | ||
* Note, you can provide multiple patterns using the comma separator. | ||
* This also only searches the relative module path from project root, avoiding false positives. | ||
*/ | ||
export function globFilterModules( | ||
items: StatsModule[], | ||
projectRoot: string, | ||
options: ModuleFilters | ||
) { | ||
if (!options.include && !options.exclude) { | ||
return items; | ||
} | ||
|
||
const matcher = picomatch(options.include ? splitPattern(options.include) : '**', { | ||
cwd: '', | ||
dot: true, | ||
nocase: true, | ||
contains: true, | ||
ignore: !options.exclude ? undefined : splitPattern(options.exclude), | ||
}); | ||
|
||
return items.filter((item) => matcher(path.relative(projectRoot, item.path))); | ||
} | ||
|
||
/** | ||
* Split the comma separated string into an array of separate patterns. | ||
* This splits on any combination of `,` and whitespaces. | ||
*/ | ||
function splitPattern(pattern: string) { | ||
return pattern.split(/\s*,\s*/); | ||
} |