Skip to content

Commit

Permalink
feat: add more rules
Browse files Browse the repository at this point in the history
  • Loading branch information
thezzisu committed Dec 23, 2024
1 parent c8e8328 commit d2547f2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
33 changes: 33 additions & 0 deletions libs/rule/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,39 @@ describe('condition', () => {
// @ts-expect-error: invalid condition
assert.equal(evaluateCondition(1, { $includesSome: [1] }), false)
})

it('should work with $notStartsWith', () => {
assert.equal(evaluateCondition('abc', { $notStartsWith: 'a' }), false)
assert.equal(evaluateCondition('abc', { $notStartsWith: 'b' }), true)
assert.equal(evaluateCondition(1, { $notStartsWith: '1' }), false)
})

it('should work with $notEndsWith', () => {
assert.equal(evaluateCondition('abc', { $notEndsWith: 'c' }), false)
assert.equal(evaluateCondition('abc', { $notEndsWith: 'b' }), true)
assert.equal(evaluateCondition(1, { $notEndsWith: '1' }), false)
})

it('should work with $notIncludes', () => {
assert.equal(evaluateCondition([1, 2, 3], { $notIncludes: 1 }), false)
assert.equal(evaluateCondition([1, 2, 3], { $notIncludes: 4 }), true)
// @ts-expect-error: invalid condition
assert.equal(evaluateCondition(1, { $notIncludes: 1 }), true)
})

it('should work with $notIncludesEach', () => {
assert.equal(evaluateCondition([1, 2, 3], { $notIncludesEach: [1, 2] }), false)
assert.equal(evaluateCondition([1, 2, 3], { $notIncludesEach: [1, 4] }), true)
// @ts-expect-error: invalid condition
assert.equal(evaluateCondition(1, { $notIncludesEach: [1] }), true)
})

it('should work with $notIncludesSome', () => {
assert.equal(evaluateCondition([1, 2, 3], { $notIncludesSome: [1, 4] }), false)
assert.equal(evaluateCondition([1, 2, 3], { $notIncludesSome: [4, 5] }), true)
// @ts-expect-error: invalid condition
assert.equal(evaluateCondition(1, { $notIncludesSome: [1] }), true)
})
})

describe('matcher', () => {
Expand Down
7 changes: 7 additions & 0 deletions libs/rule/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,21 @@ export class ConditionOps<Value> {
$nin = (value: Value, expected: Value[]) => Array.isArray(expected) && !expected.includes(value)

$startsWith = (value: Value, expected: string) => `${value}`.startsWith(expected)
$notStartsWith = (value: Value, expected: string) => !this.$startsWith(value, expected)
$endsWith = (value: Value, expected: string) => `${value}`.endsWith(expected)
$notEndsWith = (value: Value, expected: string) => !this.$endsWith(value, expected)

$includes = (value: Value, expected: ElementType<Value>) =>
Array.isArray(value) && value.includes(expected)
$notIncludes = (value: Value, expected: ElementType<Value>) => !this.$includes(value, expected)
$includesEach = (value: Value, expected: ElementType<Value>[]) =>
Array.isArray(value) && Array.isArray(expected) && expected.every((v) => value.includes(v))
$notIncludesEach = (value: Value, expected: ElementType<Value>[]) =>
!this.$includesEach(value, expected)
$includesSome = (value: Value, expected: ElementType<Value>[]) =>
Array.isArray(value) && Array.isArray(expected) && expected.some((v) => value.includes(v))
$notIncludesSome = (value: Value, expected: ElementType<Value>[]) =>
!this.$includesSome(value, expected)
}

const conditionOps = new ConditionOps()
Expand Down

0 comments on commit d2547f2

Please sign in to comment.