-
Notifications
You must be signed in to change notification settings - Fork 3
/
condition.ts
21 lines (19 loc) · 878 Bytes
/
condition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { OperatorFn, Operators } from "../../types"
const operators: Operators = {
difference: (a: string, b: string, sensitive: boolean) =>
sensitive ? a !== b : a.toLowerCase() !== b.toLowerCase(),
equal: (a: string, b: string, sensitive: boolean) =>
sensitive ? a === b : a.toLowerCase() === b.toLowerCase(),
contains: (a: string, b: string, sensitive: boolean) =>
sensitive ? a.includes(b) : a.toLowerCase().includes(b.toLowerCase()),
regex: (a: string, b: string, sensitive: boolean) =>
new RegExp(b, sensitive ? "" : "i").test(a),
}
export const applyConditions = (value: string, conditions: any[]): boolean => {
return conditions.some((condition) => {
const operation: OperatorFn | undefined = operators[condition.operation]
return operation
? operation(value, condition.value, condition.sensitive || false)
: false
})
}