|
| 1 | +/** |
| 2 | + * Compares a set of match rules contained with an object to determine if |
| 3 | + * the latter conforms to the matching rules |
| 4 | + * |
| 5 | + * @param {*} payload Object to test |
| 6 | + * @param {*} pattern Object containing the matching rules |
| 7 | + * @param {*} callback Function to call when the traversal and matching is complete |
| 8 | + */ |
1 | 9 | const match = (payload, pattern, callback) => {
|
2 |
| - let result = { match: true, total: 0, matches: {}, groups: {}}; |
3 |
| - let node = result.matches; |
| 10 | + const result = { match: true, total: 0, matches: {}, groups: {} } |
| 11 | + const node = result.matches |
4 | 12 |
|
5 |
| - const tester = (payload, pattern, current_node) => { |
6 |
| - |
7 |
| - Object.entries(pattern).forEach(([key, value]) => { |
8 |
| - if(!(key in payload)){ |
9 |
| - result.match = false; |
10 |
| - return; |
11 |
| - } |
12 |
| - |
13 |
| - if(value instanceof RegExp){ |
14 |
| - const matcher = payload[key].match(value) || []; |
15 |
| - if (matcher.length > 0) { |
16 |
| - result.groups = {...result.groups, ...matcher.groups}; |
17 |
| - current_node[key] = payload[key]; |
18 |
| - result.total += 1; |
19 |
| - } else { |
20 |
| - result.match = false; |
21 |
| - } |
22 |
| - } else if (value instanceof Array) { |
23 |
| - current_node[key] = []; |
24 |
| - value.forEach((element, index) => { |
25 |
| - if(element instanceof RegExp){ |
26 |
| - const matcher = payload[key][index].match(element) || []; |
27 |
| - if (matcher.length > 0) { |
28 |
| - result.groups = { ...result.groups, ...matcher.groups}; |
29 |
| - current_node[key] = payload[key]; |
30 |
| - result.total += 1; |
31 |
| - } else { |
32 |
| - result.match = false; |
33 |
| - } |
34 |
| - } else if (element instanceof Object) { |
35 |
| - current_node[key][index] = {}; |
36 |
| - tester(payload[key][index], element, current_node[key][index]); |
37 |
| - } else if (payload[key].includes(element)) { |
38 |
| - current_node[key][index] = element; |
39 |
| - result.total += 1; |
40 |
| - } else { |
41 |
| - result.match = false; |
42 |
| - } |
43 |
| - }); |
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 |
| - } |
51 |
| - } else if (value instanceof Object) { |
52 |
| - current_node[key] = {}; |
53 |
| - tester(payload[key], value, current_node[key]); |
| 13 | + /** |
| 14 | + * Recursive functions that will traverse the tree |
| 15 | + * @param {*} payload Object to test |
| 16 | + * @param {*} pattern Object containing the matching rules |
| 17 | + * @param {*} currentNode Contains the nested objects |
| 18 | + */ |
| 19 | + const tester = (payload, pattern, currentNode) => { |
| 20 | + /** |
| 21 | + * Loop over the elements of the testing object |
| 22 | + */ |
| 23 | + Object.entries(pattern).forEach(([key, value]) => { |
| 24 | + if (!(key in payload)) { |
| 25 | + result.match = false |
| 26 | + return |
| 27 | + } |
| 28 | + /** |
| 29 | + * Level 1 depth RegExp handling |
| 30 | + * executes a regular match then concatenates the matches |
| 31 | + * with the result object |
| 32 | + */ |
| 33 | + if (value instanceof RegExp) { |
| 34 | + const matcher = payload[key].match(value) || [] |
| 35 | + if (matcher.length > 0) { |
| 36 | + result.groups = { ...result.groups, ...matcher.groups } |
| 37 | + currentNode[key] = payload[key] |
| 38 | + result.total += 1 |
| 39 | + } else { |
| 40 | + result.match = false |
| 41 | + } |
| 42 | + /** |
| 43 | + * Level 1 depth array handling |
| 44 | + */ |
| 45 | + } else if (value instanceof Array) { |
| 46 | + currentNode[key] = [] |
| 47 | + value.forEach((element, index) => { |
| 48 | + /** |
| 49 | + * Level N depth RegExp handling |
| 50 | + */ |
| 51 | + if (element instanceof RegExp) { |
| 52 | + const matcher = payload[key][index].match(element) || [] |
| 53 | + if (matcher.length > 0) { |
| 54 | + result.groups = { ...result.groups, ...matcher.groups } |
| 55 | + currentNode[key] = payload[key] |
| 56 | + result.total += 1 |
54 | 57 | } else {
|
55 |
| - if (payload[key] != value){ |
56 |
| - result.match = false; |
57 |
| - } else { |
58 |
| - current_node[key] = payload[key]; |
59 |
| - result.total += 1; |
60 |
| - } |
| 58 | + result.match = false |
61 | 59 | }
|
| 60 | + /** |
| 61 | + * Level N depth Object and Array handling |
| 62 | + */ |
| 63 | + } else if (element instanceof Object) { |
| 64 | + currentNode[key][index] = {} |
| 65 | + tester(payload[key][index], element, currentNode[key][index]) |
| 66 | + } else if (payload[key].includes(element)) { |
| 67 | + currentNode[key][index] = element |
| 68 | + result.total += 1 |
| 69 | + } else { |
| 70 | + result.match = false |
| 71 | + } |
62 | 72 | })
|
63 |
| - } |
| 73 | + /** |
| 74 | + * Level 1 depth tester functions handling |
| 75 | + */ |
| 76 | + } else if (value instanceof Function) { |
| 77 | + if (!value(payload[key])) { |
| 78 | + result.match = false |
| 79 | + } else { |
| 80 | + currentNode[key] = payload[key] |
| 81 | + result.total += 1 |
| 82 | + } |
| 83 | + } else if (value instanceof Object) { |
| 84 | + currentNode[key] = {} |
| 85 | + tester(payload[key], value, currentNode[key]) |
| 86 | + } else { |
| 87 | + if (payload[key] !== value) { |
| 88 | + result.match = false |
| 89 | + } else { |
| 90 | + currentNode[key] = payload[key] |
| 91 | + result.total += 1 |
| 92 | + } |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
64 | 96 |
|
65 |
| - tester(payload, pattern, node); |
| 97 | + // Invoke the tester method - execution starts here |
| 98 | + tester(payload, pattern, node) |
66 | 99 |
|
67 |
| - if(callback && result.match){ |
68 |
| - callback(result); |
69 |
| - } |
| 100 | + // Invoke callback |
| 101 | + if (callback && result.match) { |
| 102 | + callback(result) |
| 103 | + } |
70 | 104 |
|
71 |
| - return result; |
| 105 | + return result |
72 | 106 | }
|
73 | 107 |
|
74 |
| -module.exports = match; |
| 108 | +module.exports = match |
0 commit comments