-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
100 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// @ts-ignore | ||
import regexParserFn from 'regex-parser'; | ||
|
||
type RegexParser = (str: string) => RegExp; | ||
const regexParser: RegexParser = regexParserFn; | ||
|
||
const spaceSplitter = (str: string): string[] => str.split(` `); | ||
|
||
const trimmer = (str: string): string => str.trim(); | ||
|
||
const parsePredicate = (str: string | undefined): IPredicate[] => { | ||
if (!str) { | ||
return []; | ||
} | ||
return str.split(`,`).flatMap(spaceSplitter).map(trimmer).filter(Boolean).map(regexParser).map(compose); | ||
}; | ||
|
||
const compose = (regex: RegExp): IPredicate => (predicate: string): boolean => regex.test(predicate); | ||
|
||
const truthy: IPredicate = (): boolean => true; | ||
|
||
const not = (predicate: IPredicate): IPredicate => (str: string): boolean => !predicate(str); | ||
|
||
const or = (predicate1: IPredicate, predicate2: IPredicate): IPredicate => (str: string): boolean => | ||
predicate1(str) || predicate2(str); | ||
|
||
const orReducer = (prev: IPredicate, current: IPredicate): IPredicate => { | ||
return or(prev, current); | ||
}; | ||
|
||
const and = (predicate1: IPredicate, predicate2: IPredicate): IPredicate => (str: string): boolean => | ||
predicate1(str) && predicate2(str); | ||
|
||
const andReducer = (prev: IPredicate, current: IPredicate): IPredicate => { | ||
return and(prev, current); | ||
}; | ||
|
||
export interface IPredicateBuilderOptions { | ||
include: string | undefined; | ||
exclude: string | undefined; | ||
} | ||
|
||
export type IPredicate = (str: string) => boolean; | ||
|
||
export const buildPredicate = (options: IPredicateBuilderOptions): IPredicate => { | ||
const include = parsePredicate(options.include); | ||
if (include.length === 0) { | ||
include.push(truthy); | ||
} | ||
const includePredicate = include.reduce(orReducer); | ||
const exclude = parsePredicate(options.exclude).map(not); | ||
if (exclude.length === 0) { | ||
exclude.push(truthy); | ||
} | ||
const excludePredicate = exclude.reduce(andReducer); | ||
return and(includePredicate, excludePredicate); | ||
}; |
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