Skip to content
This repository was archived by the owner on Jan 2, 2023. It is now read-only.

Commit 31ed5f3

Browse files
committed
adds support to function tests
1 parent 7ba00b0 commit 31ed5f3

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ const match = (payload, pattern, callback) => {
4141
result.match = false;
4242
}
4343
});
44+
} else if (value instanceof Function) {
45+
if (!value(payload[key])){
46+
result.match = false;
47+
} else {
48+
current_node[key] = payload[key];
49+
result.total += 1;
50+
}
4451
} else if (value instanceof Object) {
4552
current_node[key] = {};
4653
tester(payload[key], value, current_node[key]);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@menadevs/objectron",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "Compares a set of match rules contained with an object to determine if the latter conforms to the matching rules",
55
"main": "index.js",
66
"devDependencies": {

test/test_objectron.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,43 @@ suite('Objectron Core Tests', () => {
2828
assert.deepEqual(result, expected);
2929
});
3030

31+
test('Match with function', () => {
32+
const payload = {
33+
'type': 'message',
34+
'text': 'text',
35+
'int': 1,
36+
'bool': true,
37+
'float': 1.1,
38+
'items': [1, 1, 1, 1],
39+
}
40+
41+
const result = match(payload, {
42+
'type': (val) => val === 'message',
43+
'text': (val) => val.length == 4,
44+
'int': (val) => val + 1 == 2,
45+
'bool': (val) => !!!!!!!!val, // STAHP!
46+
'float': (val) => val - 1.1 == 0,
47+
'items': (val) => val.length == 4,
48+
});
49+
50+
const expected = {
51+
match: true,
52+
total: 6,
53+
matches: {
54+
'type': 'message',
55+
'text': 'text',
56+
'int': 1,
57+
'bool': true,
58+
'float': 1.1,
59+
'items': [1, 1, 1, 1],
60+
},
61+
groups: {}
62+
};
63+
64+
assert.isTrue(result.match);
65+
assert.deepEqual(result, expected);
66+
});
67+
3168
test('Match with primitive types', () => {
3269
const payload = {
3370
'type': 'message',

0 commit comments

Comments
 (0)