generated from JS-DevTools/template-node-typescript
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a
createFilter()
function, which works the same as `filePathF…
…ilter()` but allows options to be set
- Loading branch information
1 parent
aec1d0c
commit 38b9c84
Showing
5 changed files
with
102 additions
and
36 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,53 @@ | ||
import { normalize } from "./normalize"; | ||
import { AnyFilter, FilterCriterion, FilterFunction, Filters, Options } from "./types"; | ||
import { _filters, PathFilter } from "./util"; | ||
|
||
/** | ||
* Creates a `FilterFunction` that matches file paths based on the specified criteria. | ||
* | ||
* @param criteria - One or more glob patterns, regular expressions, or filter functions | ||
* @returns A `FilterFunction` that matches file paths based on the specified criteria | ||
*/ | ||
export function createFilter(options: Options, criteria: AnyFilter): FilterFunction; | ||
|
||
/** | ||
* Creates a `FilterFunction` that matches file paths based on the specified criteria. | ||
* | ||
* @param criteria - One or more glob patterns, regular expressions, or filter functions | ||
* @returns A `FilterFunction` that matches file paths based on the specified criteria | ||
*/ | ||
export function createFilter(options: Options, ...criteria: FilterCriterion[]): FilterFunction; | ||
|
||
/** | ||
* Creates a `FilterFunction` that matches file paths based on the specified criteria. | ||
* | ||
* @param filters - An object with `include` and `exclude` filter criteria | ||
* @returns A `FilterFunction` that matches file paths based on the specified criteria | ||
*/ | ||
export function createFilter(options: Options, filters: Filters): FilterFunction; | ||
|
||
export function createFilter(options: Options, ...args: unknown[]): FilterFunction { | ||
let criteria = args.length <= 1 ? args[0] as AnyFilter : args as FilterCriterion[]; | ||
let filters = normalize(criteria, options); | ||
|
||
(pathFilter as PathFilter)[_filters] = filters; | ||
return pathFilter; | ||
|
||
// tslint:disable-next-line: no-shadowed-variable | ||
function pathFilter(...args: unknown[]): boolean { | ||
// Does the file path match any of the exclude filters? | ||
let exclude = filters.exclude.some((filter) => filter(...args)); | ||
if (exclude) { | ||
return false; | ||
} | ||
|
||
if (filters.include.length === 0) { | ||
// Include everything that's not excluded | ||
return true; | ||
} | ||
|
||
// Does the file path match any of the include filters? | ||
let include = filters.include.some((filter) => filter(...args)); | ||
return include; | ||
} | ||
} |
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