diff --git a/libs/rule/src/index.spec.ts b/libs/rule/src/index.spec.ts index 319cf86..a1aa719 100644 --- a/libs/rule/src/index.spec.ts +++ b/libs/rule/src/index.spec.ts @@ -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', () => { diff --git a/libs/rule/src/index.ts b/libs/rule/src/index.ts index 51ac28d..1ee6842 100644 --- a/libs/rule/src/index.ts +++ b/libs/rule/src/index.ts @@ -42,14 +42,21 @@ export class ConditionOps { $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) => Array.isArray(value) && value.includes(expected) + $notIncludes = (value: Value, expected: ElementType) => !this.$includes(value, expected) $includesEach = (value: Value, expected: ElementType[]) => Array.isArray(value) && Array.isArray(expected) && expected.every((v) => value.includes(v)) + $notIncludesEach = (value: Value, expected: ElementType[]) => + !this.$includesEach(value, expected) $includesSome = (value: Value, expected: ElementType[]) => Array.isArray(value) && Array.isArray(expected) && expected.some((v) => value.includes(v)) + $notIncludesSome = (value: Value, expected: ElementType[]) => + !this.$includesSome(value, expected) } const conditionOps = new ConditionOps()