Skip to content

Commit

Permalink
Remove FIND (*) from the defaults
Browse files Browse the repository at this point in the history
Update the EVERY (&) and SOME (|) options in the defaults to incorporate that logic for consistency
  • Loading branch information
claudiuandrei committed May 18, 2021
1 parent e18eca2 commit 6ca0f01
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 76 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "compar",
"version": "4.0.0",
"version": "5.0.0",
"description": "Extensible declarative rule matching engine",
"keywords": [
"rule engine",
Expand Down
34 changes: 24 additions & 10 deletions src/compar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ export const defaultMatchers: { [type: string]: Matcher } = {
// Regex match
'~': (match, context, [value]) => [new RegExp(value, 'i').test(context)],

// Match every condition
'&': (match, context, payload) => [payload.every(value => match(context, value)[0])],

// Match any condition
'|': (match, context, payload) => [payload.some(value => match(context, value)[0])],

// Match opposite condition
'!': (match, context, [value]) => [!match(context, value)[0]],

Expand All @@ -53,12 +47,32 @@ export const defaultMatchers: { [type: string]: Matcher } = {
'?': (match, context, [condition, value]) =>
match(context, condition)[0] ? match(context, value) : [false],

// Find
'*': (match, context, payload) => {
// Match every condition
'&': (match, context, payload) => {
// Keep the value so we can return it
let success, value

// Find the value
for (let current of payload) {
// Look for a value
;[success, value] = match(context, current)

// Return only
if (!success) {
return [false]
}
}

// Everything was succesful
return [true, value]
},

// Match any condition
'|': (match, context, payload) => {
// Find the value
for (let index = 0; index < payload.length; index += 1) {
for (let current of payload) {
// Look for a value
const [success, value] = match(context, payload[index])
const [success, value] = match(context, current)

// Return only
if (success) {
Expand Down
157 changes: 92 additions & 65 deletions test/compar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,64 +102,6 @@ describe('REGEXP', () => {
})
})

describe('EVERY', () => {
// Test equality
test('EVERY should match if all values match', () => {
// Setup the match
const [ct] = match(25, ['&', ['>', 20], ['<', 40]])

// Match result should be true
expect(ct).toEqual(true)
})

// Test equality
test('EVERY should not match if any values match', () => {
// Setup the match
const [ct] = match(25, ['&', ['>', 20], ['<', 20]])

// Match result should be true
expect(ct).toEqual(false)
})

// Test equality
test('EVERY should not match if none of the values match', () => {
// Setup the match
const [ct] = match(25, ['&', ['>', 40], ['<', 20]])

// Match result should be true
expect(ct).toEqual(false)
})
})

describe('SOME', () => {
// Test equality
test('SOME should match if all values match', () => {
// Setup the match
const [ct] = match(25, ['|', ['>', 20], ['<', 40]])

// Match result should be true
expect(ct).toEqual(true)
})

// Test equality
test('SOME should match if any values match', () => {
// Setup the match
const [ct] = match(25, ['|', ['>', 40], ['<', 30]])

// Match result should be true
expect(ct).toEqual(true)
})

// Test equality
test('SOME should not match if none of the values match', () => {
// Setup the match
const [ct] = match(25, ['|', ['>', 40], ['<', 20]])

// Match result should be true
expect(ct).toEqual(false)
})
})

describe('NOT', () => {
// Test equality
test('NOT should match an the oposite value', () => {
Expand Down Expand Up @@ -284,12 +226,97 @@ describe('CONDITIONAL', () => {
})
})

describe('FIND', () => {
describe('EVERY', () => {
// Test equality
test('EVERY should match if all values match', () => {
// Setup the match
const [ct] = match(25, ['&', ['>', 20], ['<', 40]])

// Match result should be true
expect(ct).toEqual(true)
})

// Test equality
test('EVERY should not match if any values match', () => {
// Setup the match
const [ct] = match(25, ['&', ['>', 20], ['<', 20]])

// Match result should be true
expect(ct).toEqual(false)
})

// Test equality
test('EVERY should not match if none of the values match', () => {
// Setup the match
const [ct] = match(25, ['&', ['>', 40], ['<', 20]])

// Match result should be true
expect(ct).toEqual(false)
})

// Test equality
test('EVERY should not retrieve value if not all conditions match', () => {
// Setup the match
const [ct, v] = match('John Doe', [
'&',
['?', ['=', 'Jane Doe'], ['@', 'Jane Doe']],
['?', ['=', 'John Doe'], ['@', 'John Doe']],
['@', 'No one']
])

// Match result should be true
expect(ct).toEqual(false)
expect(v).toBeUndefined()
})

// Test equality
test('EVERY should retrieve last value if all conditions match and there is a default', () => {
// Setup the match
const [ct, v] = match('Jane Doe', [
'&',
['?', ['=', 'Jane Doe'], ['@', 'Jane Doe']],
['@', 'Everyone']
])

// Match result should be true
expect(ct).toEqual(true)
expect(v).toEqual('Everyone')
})
})

describe('SOME', () => {
// Test equality
test('SOME should match if all values match', () => {
// Setup the match
const [ct] = match(25, ['|', ['>', 20], ['<', 40]])

// Match result should be true
expect(ct).toEqual(true)
})

// Test equality
test('SOME should match if any values match', () => {
// Setup the match
const [ct] = match(25, ['|', ['>', 40], ['<', 30]])

// Match result should be true
expect(ct).toEqual(true)
})

// Test equality
test('SOME should not match if none of the values match', () => {
// Setup the match
const [ct] = match(25, ['|', ['>', 40], ['<', 20]])

// Match result should be true
expect(ct).toEqual(false)
})

// Test equality
test('FIND should not retrieve value if no condition matches', () => {
test('SOME should retrieve the first when condition matches', () => {
// Setup the match
const [ct, v] = match('John Doe', [
'*',
'|',
['?', ['=', 'Jane Doe'], ['@', 'Jane Doe']],
['?', ['=', 'John Doe'], ['@', 'John Doe']],
['@', 'No one']
Expand All @@ -301,10 +328,10 @@ describe('FIND', () => {
})

// Test equality
test('FIND should retrieve default value if no condition matches and there is a default', () => {
test('SOME should retrieve default value if no condition matches and there is a default', () => {
// Setup the match
const [ct, v] = match('John Doe', [
'*',
'|',
['?', ['=', 'Jane Doe'], ['@', 'Jane Doe']],
['@', 'No one']
])
Expand All @@ -315,9 +342,9 @@ describe('FIND', () => {
})

// Test equality
test('FIND should not retrieve any value if no condition matches', () => {
test('SOME should not retrieve any value if no condition matches', () => {
// Setup the match
const [ct, v] = match('John Doe', ['*', ['?', ['=', 'Jane Doe'], ['@', 'Jane Doe']]])
const [ct, v] = match('John Doe', ['|', ['?', ['=', 'Jane Doe'], ['@', 'Jane Doe']]])

// Match result should be true
expect(ct).toEqual(false)
Expand Down

0 comments on commit 6ca0f01

Please sign in to comment.