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

Commit 2eab968

Browse files
committed
adds support to keys not found in payload
1 parent 91f03aa commit 2eab968

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

objectron.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const match = (payload, pattern, callback) => {
55
const tester = (payload, pattern, current_node) => {
66

77
Object.entries(pattern).forEach(([key, value]) => {
8+
if(!(key in payload)){
9+
result.match = false;
10+
return;
11+
}
12+
813
if(value instanceof RegExp){
914
const matcher = payload[key].match(value) || [];
1015
if (matcher.length > 0) {

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.1",
3+
"version": "0.1.2",
44
"description": "TBD",
55
"main": "matcher.js",
66
"devDependencies": {

test/test_objectron.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@ const suite = require('mocha').suite;
44
const test = require('mocha').test;
55

66
suite('Objectron Core Tests', () => {
7+
test('Match with pattern keys none-existant', () => {
8+
const payload = {
9+
'type': 'message',
10+
'text': 'text',
11+
}
12+
13+
const result = match(payload, {
14+
'other': 'stuff',
15+
'text': 'text',
16+
});
17+
18+
const expected = {
19+
match: false,
20+
total: 1,
21+
matches: {
22+
'text': 'text',
23+
},
24+
groups: {}
25+
};
26+
27+
assert.isFalse(result.match);
28+
assert.deepEqual(result, expected);
29+
});
30+
731
test('Match with primitive types', () => {
832
const payload = {
933
'type': 'message',

0 commit comments

Comments
 (0)