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