@ls-stack/utils 3.44.0 → 3.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/partialEqual.cjs +101 -50
- package/dist/partialEqual.d.cts +49 -45
- package/dist/partialEqual.d.ts +49 -45
- package/dist/partialEqual.js +101 -50
- package/package.json +1 -1
package/dist/partialEqual.cjs
CHANGED
|
@@ -91,70 +91,74 @@ function deepEqual(foo, bar, maxDepth = 20) {
|
|
|
91
91
|
|
|
92
92
|
// src/partialEqual.ts
|
|
93
93
|
var has2 = Object.prototype.hasOwnProperty;
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
};
|
|
94
|
+
function createComparison(type) {
|
|
95
|
+
return {
|
|
96
|
+
"~sc": type
|
|
97
|
+
};
|
|
98
|
+
}
|
|
100
99
|
var match = {
|
|
101
100
|
hasType: {
|
|
102
|
-
string:
|
|
103
|
-
number:
|
|
104
|
-
boolean:
|
|
105
|
-
object:
|
|
106
|
-
array:
|
|
107
|
-
function:
|
|
101
|
+
string: createComparison(["hasType", "string"]),
|
|
102
|
+
number: createComparison(["hasType", "number"]),
|
|
103
|
+
boolean: createComparison(["hasType", "boolean"]),
|
|
104
|
+
object: createComparison(["hasType", "object"]),
|
|
105
|
+
array: createComparison(["hasType", "array"]),
|
|
106
|
+
function: createComparison(["hasType", "function"])
|
|
108
107
|
},
|
|
109
|
-
isInstanceOf: (constructor) =>
|
|
108
|
+
isInstanceOf: (constructor) => createComparison(["isInstanceOf", constructor]),
|
|
110
109
|
str: {
|
|
111
|
-
contains: (substring) =>
|
|
112
|
-
startsWith: (substring) =>
|
|
113
|
-
endsWith: (substring) =>
|
|
114
|
-
matchesRegex: (regex) =>
|
|
110
|
+
contains: (substring) => createComparison(["strContains", substring]),
|
|
111
|
+
startsWith: (substring) => createComparison(["strStartsWith", substring]),
|
|
112
|
+
endsWith: (substring) => createComparison(["strEndsWith", substring]),
|
|
113
|
+
matchesRegex: (regex) => createComparison(["strMatchesRegex", regex])
|
|
115
114
|
},
|
|
116
115
|
num: {
|
|
117
|
-
isGreaterThan: (value) =>
|
|
118
|
-
isGreaterThanOrEqual: (value) =>
|
|
119
|
-
isLessThan: (value) =>
|
|
120
|
-
isLessThanOrEqual: (value) =>
|
|
121
|
-
isInRange: (value) =>
|
|
116
|
+
isGreaterThan: (value) => createComparison(["numIsGreaterThan", value]),
|
|
117
|
+
isGreaterThanOrEqual: (value) => createComparison(["numIsGreaterThanOrEqual", value]),
|
|
118
|
+
isLessThan: (value) => createComparison(["numIsLessThan", value]),
|
|
119
|
+
isLessThanOrEqual: (value) => createComparison(["numIsLessThanOrEqual", value]),
|
|
120
|
+
isInRange: (value) => createComparison(["numIsInRange", value])
|
|
122
121
|
},
|
|
123
122
|
jsonString: {
|
|
124
|
-
hasPartial: (value) =>
|
|
123
|
+
hasPartial: (value) => createComparison(["jsonStringHasPartial", value])
|
|
125
124
|
},
|
|
126
|
-
equal: (value) =>
|
|
127
|
-
partialEqual: (value) =>
|
|
128
|
-
custom: (isEqual) =>
|
|
125
|
+
equal: (value) => createComparison(["deepEqual", value]),
|
|
126
|
+
partialEqual: (value) => createComparison(["partialEqual", value]),
|
|
127
|
+
custom: (isEqual) => createComparison(["custom", isEqual]),
|
|
128
|
+
keyNotBePresent: createComparison(["keyNotBePresent", null]),
|
|
129
|
+
any: (...comparisons) => createComparison(["any", comparisons.map((c) => c["~sc"])]),
|
|
130
|
+
all: (...comparisons) => createComparison(["all", comparisons.map((c) => c["~sc"])]),
|
|
129
131
|
not: {
|
|
130
132
|
hasType: {
|
|
131
|
-
string:
|
|
132
|
-
number:
|
|
133
|
-
boolean:
|
|
134
|
-
object:
|
|
135
|
-
array:
|
|
136
|
-
function:
|
|
133
|
+
string: createComparison(["not", ["hasType", "string"]]),
|
|
134
|
+
number: createComparison(["not", ["hasType", "number"]]),
|
|
135
|
+
boolean: createComparison(["not", ["hasType", "boolean"]]),
|
|
136
|
+
object: createComparison(["not", ["hasType", "object"]]),
|
|
137
|
+
array: createComparison(["not", ["hasType", "array"]]),
|
|
138
|
+
function: createComparison(["not", ["hasType", "function"]])
|
|
137
139
|
},
|
|
138
|
-
isInstanceOf: (constructor) =>
|
|
140
|
+
isInstanceOf: (constructor) => createComparison(["not", ["isInstanceOf", constructor]]),
|
|
139
141
|
str: {
|
|
140
|
-
contains: (substring) =>
|
|
141
|
-
startsWith: (substring) =>
|
|
142
|
-
endsWith: (substring) =>
|
|
143
|
-
matchesRegex: (regex) =>
|
|
142
|
+
contains: (substring) => createComparison(["not", ["strContains", substring]]),
|
|
143
|
+
startsWith: (substring) => createComparison(["not", ["strStartsWith", substring]]),
|
|
144
|
+
endsWith: (substring) => createComparison(["not", ["strEndsWith", substring]]),
|
|
145
|
+
matchesRegex: (regex) => createComparison(["not", ["strMatchesRegex", regex]])
|
|
144
146
|
},
|
|
145
147
|
num: {
|
|
146
|
-
isGreaterThan: (value) =>
|
|
147
|
-
isGreaterThanOrEqual: (value) =>
|
|
148
|
-
isLessThan: (value) =>
|
|
149
|
-
isLessThanOrEqual: (value) =>
|
|
150
|
-
isInRange: (value) =>
|
|
148
|
+
isGreaterThan: (value) => createComparison(["not", ["numIsGreaterThan", value]]),
|
|
149
|
+
isGreaterThanOrEqual: (value) => createComparison(["not", ["numIsGreaterThanOrEqual", value]]),
|
|
150
|
+
isLessThan: (value) => createComparison(["not", ["numIsLessThan", value]]),
|
|
151
|
+
isLessThanOrEqual: (value) => createComparison(["not", ["numIsLessThanOrEqual", value]]),
|
|
152
|
+
isInRange: (value) => createComparison(["not", ["numIsInRange", value]])
|
|
151
153
|
},
|
|
152
154
|
jsonString: {
|
|
153
|
-
hasPartial: (value) =>
|
|
155
|
+
hasPartial: (value) => createComparison(["not", ["jsonStringHasPartial", value]])
|
|
154
156
|
},
|
|
155
|
-
equal: (value) =>
|
|
156
|
-
partialEqual: (value) =>
|
|
157
|
-
custom: (value) =>
|
|
157
|
+
equal: (value) => createComparison(["not", ["deepEqual", value]]),
|
|
158
|
+
partialEqual: (value) => createComparison(["not", ["partialEqual", value]]),
|
|
159
|
+
custom: (value) => createComparison(["not", ["custom", value]]),
|
|
160
|
+
any: (...comparisons) => createComparison(["not", ["any", comparisons.map((c) => c["~sc"])]]),
|
|
161
|
+
all: (...comparisons) => createComparison(["not", ["all", comparisons.map((c) => c["~sc"])]])
|
|
158
162
|
}
|
|
159
163
|
};
|
|
160
164
|
function find2(iter, tar) {
|
|
@@ -162,6 +166,24 @@ function find2(iter, tar) {
|
|
|
162
166
|
if (partialEqual(key, tar)) return key;
|
|
163
167
|
}
|
|
164
168
|
}
|
|
169
|
+
function executeComparisonWithKeyContext(target, comp, keyExists) {
|
|
170
|
+
const [type, value] = comp;
|
|
171
|
+
if (type === "keyNotBePresent") {
|
|
172
|
+
return !keyExists;
|
|
173
|
+
}
|
|
174
|
+
if (type === "any") {
|
|
175
|
+
for (const childComp of value) {
|
|
176
|
+
if (executeComparisonWithKeyContext(target, childComp, keyExists)) {
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
if (type === "not") {
|
|
183
|
+
return !executeComparisonWithKeyContext(target, value, keyExists);
|
|
184
|
+
}
|
|
185
|
+
return executeComparison(target, comp);
|
|
186
|
+
}
|
|
165
187
|
function executeComparison(target, comparison) {
|
|
166
188
|
const [type, value] = comparison;
|
|
167
189
|
switch (type) {
|
|
@@ -216,6 +238,22 @@ function executeComparison(target, comparison) {
|
|
|
216
238
|
return partialEqual(target, value);
|
|
217
239
|
case "custom":
|
|
218
240
|
return value(target);
|
|
241
|
+
case "keyNotBePresent":
|
|
242
|
+
return false;
|
|
243
|
+
case "any":
|
|
244
|
+
for (const comp of value) {
|
|
245
|
+
if (executeComparison(target, comp)) {
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return false;
|
|
250
|
+
case "all":
|
|
251
|
+
for (const comp of value) {
|
|
252
|
+
if (!executeComparison(target, comp)) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return true;
|
|
219
257
|
case "not":
|
|
220
258
|
return !executeComparison(target, value);
|
|
221
259
|
default:
|
|
@@ -224,8 +262,8 @@ function executeComparison(target, comparison) {
|
|
|
224
262
|
}
|
|
225
263
|
function partialEqual(target, sub) {
|
|
226
264
|
if (sub === target) return true;
|
|
227
|
-
if (sub
|
|
228
|
-
return executeComparison(target, sub
|
|
265
|
+
if (sub && typeof sub === "object" && "~sc" in sub) {
|
|
266
|
+
return executeComparison(target, sub["~sc"]);
|
|
229
267
|
}
|
|
230
268
|
if (sub && target && sub.constructor === target.constructor) {
|
|
231
269
|
const ctor = sub.constructor;
|
|
@@ -272,8 +310,21 @@ function partialEqual(target, sub) {
|
|
|
272
310
|
if (!ctor || typeof sub === "object") {
|
|
273
311
|
for (const key in sub) {
|
|
274
312
|
if (has2.call(sub, key)) {
|
|
275
|
-
|
|
276
|
-
|
|
313
|
+
const subValue = sub[key];
|
|
314
|
+
if (subValue && typeof subValue === "object" && "~sc" in subValue && subValue["~sc"][0] === "keyNotBePresent") {
|
|
315
|
+
if (has2.call(target, key)) {
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
} else if (subValue && typeof subValue === "object" && "~sc" in subValue && subValue["~sc"][0] === "any") {
|
|
319
|
+
const targetHasKey = has2.call(target, key);
|
|
320
|
+
const targetValue = targetHasKey ? target[key] : void 0;
|
|
321
|
+
if (!executeComparisonWithKeyContext(targetValue, subValue["~sc"], targetHasKey)) {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
} else {
|
|
325
|
+
if (!has2.call(target, key) || !partialEqual(target[key], subValue)) {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
277
328
|
}
|
|
278
329
|
}
|
|
279
330
|
}
|
package/dist/partialEqual.d.cts
CHANGED
|
@@ -1,69 +1,73 @@
|
|
|
1
1
|
type ComparisonsType = [type: 'strStartsWith', value: string] | [type: 'strEndsWith', value: string] | [
|
|
2
2
|
type: 'hasType',
|
|
3
3
|
value: 'string' | 'number' | 'boolean' | 'object' | 'array' | 'function'
|
|
4
|
-
] | [type: 'strContains', value: string] | [type: 'strMatchesRegex', value: RegExp] | [type: 'deepEqual', value: any] | [type: 'numIsGreaterThan', value: number] | [type: 'numIsGreaterThanOrEqual', value: number] | [type: 'numIsLessThan', value: number] | [type: 'numIsLessThanOrEqual', value: number] | [type: 'numIsInRange', value: [number, number]] | [type: 'jsonStringHasPartial', value: any] | [type: 'partialEqual', value: any] | [type: 'custom', value: (target: unknown) => boolean] | [type: 'isInstanceOf', value: new (...args: any[]) => any] | [type: 'not', value: ComparisonsType];
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
4
|
+
] | [type: 'strContains', value: string] | [type: 'strMatchesRegex', value: RegExp] | [type: 'deepEqual', value: any] | [type: 'numIsGreaterThan', value: number] | [type: 'numIsGreaterThanOrEqual', value: number] | [type: 'numIsLessThan', value: number] | [type: 'numIsLessThanOrEqual', value: number] | [type: 'numIsInRange', value: [number, number]] | [type: 'jsonStringHasPartial', value: any] | [type: 'partialEqual', value: any] | [type: 'custom', value: (target: unknown) => boolean] | [type: 'isInstanceOf', value: new (...args: any[]) => any] | [type: 'keyNotBePresent', value: null] | [type: 'not', value: ComparisonsType] | [type: 'any', value: ComparisonsType[]] | [type: 'all', value: ComparisonsType[]];
|
|
5
|
+
type Comparison = {
|
|
6
|
+
'~sc': ComparisonsType;
|
|
7
|
+
};
|
|
9
8
|
declare const match: {
|
|
10
9
|
hasType: {
|
|
11
|
-
string:
|
|
12
|
-
number:
|
|
13
|
-
boolean:
|
|
14
|
-
object:
|
|
15
|
-
array:
|
|
16
|
-
function:
|
|
10
|
+
string: Comparison;
|
|
11
|
+
number: Comparison;
|
|
12
|
+
boolean: Comparison;
|
|
13
|
+
object: Comparison;
|
|
14
|
+
array: Comparison;
|
|
15
|
+
function: Comparison;
|
|
17
16
|
};
|
|
18
|
-
isInstanceOf: (constructor: new (...args: any[]) => any) =>
|
|
17
|
+
isInstanceOf: (constructor: new (...args: any[]) => any) => Comparison;
|
|
19
18
|
str: {
|
|
20
|
-
contains: (substring: string) =>
|
|
21
|
-
startsWith: (substring: string) =>
|
|
22
|
-
endsWith: (substring: string) =>
|
|
23
|
-
matchesRegex: (regex: RegExp) =>
|
|
19
|
+
contains: (substring: string) => Comparison;
|
|
20
|
+
startsWith: (substring: string) => Comparison;
|
|
21
|
+
endsWith: (substring: string) => Comparison;
|
|
22
|
+
matchesRegex: (regex: RegExp) => Comparison;
|
|
24
23
|
};
|
|
25
24
|
num: {
|
|
26
|
-
isGreaterThan: (value: number) =>
|
|
27
|
-
isGreaterThanOrEqual: (value: number) =>
|
|
28
|
-
isLessThan: (value: number) =>
|
|
29
|
-
isLessThanOrEqual: (value: number) =>
|
|
30
|
-
isInRange: (value: [number, number]) =>
|
|
25
|
+
isGreaterThan: (value: number) => Comparison;
|
|
26
|
+
isGreaterThanOrEqual: (value: number) => Comparison;
|
|
27
|
+
isLessThan: (value: number) => Comparison;
|
|
28
|
+
isLessThanOrEqual: (value: number) => Comparison;
|
|
29
|
+
isInRange: (value: [number, number]) => Comparison;
|
|
31
30
|
};
|
|
32
31
|
jsonString: {
|
|
33
|
-
hasPartial: (value: any) =>
|
|
32
|
+
hasPartial: (value: any) => Comparison;
|
|
34
33
|
};
|
|
35
|
-
equal: (value: any) =>
|
|
36
|
-
partialEqual: (value: any) =>
|
|
37
|
-
custom: (isEqual: (value: unknown) => boolean) =>
|
|
34
|
+
equal: (value: any) => Comparison;
|
|
35
|
+
partialEqual: (value: any) => Comparison;
|
|
36
|
+
custom: (isEqual: (value: unknown) => boolean) => Comparison;
|
|
37
|
+
keyNotBePresent: Comparison;
|
|
38
|
+
any: (...comparisons: Comparison[]) => Comparison;
|
|
39
|
+
all: (...comparisons: Comparison[]) => Comparison;
|
|
38
40
|
not: {
|
|
39
41
|
hasType: {
|
|
40
|
-
string:
|
|
41
|
-
number:
|
|
42
|
-
boolean:
|
|
43
|
-
object:
|
|
44
|
-
array:
|
|
45
|
-
function:
|
|
42
|
+
string: Comparison;
|
|
43
|
+
number: Comparison;
|
|
44
|
+
boolean: Comparison;
|
|
45
|
+
object: Comparison;
|
|
46
|
+
array: Comparison;
|
|
47
|
+
function: Comparison;
|
|
46
48
|
};
|
|
47
|
-
isInstanceOf: (constructor: new (...args: any[]) => any) =>
|
|
49
|
+
isInstanceOf: (constructor: new (...args: any[]) => any) => Comparison;
|
|
48
50
|
str: {
|
|
49
|
-
contains: (substring: string) =>
|
|
50
|
-
startsWith: (substring: string) =>
|
|
51
|
-
endsWith: (substring: string) =>
|
|
52
|
-
matchesRegex: (regex: RegExp) =>
|
|
51
|
+
contains: (substring: string) => Comparison;
|
|
52
|
+
startsWith: (substring: string) => Comparison;
|
|
53
|
+
endsWith: (substring: string) => Comparison;
|
|
54
|
+
matchesRegex: (regex: RegExp) => Comparison;
|
|
53
55
|
};
|
|
54
56
|
num: {
|
|
55
|
-
isGreaterThan: (value: number) =>
|
|
56
|
-
isGreaterThanOrEqual: (value: number) =>
|
|
57
|
-
isLessThan: (value: number) =>
|
|
58
|
-
isLessThanOrEqual: (value: number) =>
|
|
59
|
-
isInRange: (value: [number, number]) =>
|
|
57
|
+
isGreaterThan: (value: number) => Comparison;
|
|
58
|
+
isGreaterThanOrEqual: (value: number) => Comparison;
|
|
59
|
+
isLessThan: (value: number) => Comparison;
|
|
60
|
+
isLessThanOrEqual: (value: number) => Comparison;
|
|
61
|
+
isInRange: (value: [number, number]) => Comparison;
|
|
60
62
|
};
|
|
61
63
|
jsonString: {
|
|
62
|
-
hasPartial: (value: any) =>
|
|
64
|
+
hasPartial: (value: any) => Comparison;
|
|
63
65
|
};
|
|
64
|
-
equal: (value: any) =>
|
|
65
|
-
partialEqual: (value: any) =>
|
|
66
|
-
custom: (value: (target: unknown) => boolean) =>
|
|
66
|
+
equal: (value: any) => Comparison;
|
|
67
|
+
partialEqual: (value: any) => Comparison;
|
|
68
|
+
custom: (value: (target: unknown) => boolean) => Comparison;
|
|
69
|
+
any: (...comparisons: Comparison[]) => Comparison;
|
|
70
|
+
all: (...comparisons: Comparison[]) => Comparison;
|
|
67
71
|
};
|
|
68
72
|
};
|
|
69
73
|
declare function partialEqual(target: any, sub: any): boolean;
|
package/dist/partialEqual.d.ts
CHANGED
|
@@ -1,69 +1,73 @@
|
|
|
1
1
|
type ComparisonsType = [type: 'strStartsWith', value: string] | [type: 'strEndsWith', value: string] | [
|
|
2
2
|
type: 'hasType',
|
|
3
3
|
value: 'string' | 'number' | 'boolean' | 'object' | 'array' | 'function'
|
|
4
|
-
] | [type: 'strContains', value: string] | [type: 'strMatchesRegex', value: RegExp] | [type: 'deepEqual', value: any] | [type: 'numIsGreaterThan', value: number] | [type: 'numIsGreaterThanOrEqual', value: number] | [type: 'numIsLessThan', value: number] | [type: 'numIsLessThanOrEqual', value: number] | [type: 'numIsInRange', value: [number, number]] | [type: 'jsonStringHasPartial', value: any] | [type: 'partialEqual', value: any] | [type: 'custom', value: (target: unknown) => boolean] | [type: 'isInstanceOf', value: new (...args: any[]) => any] | [type: 'not', value: ComparisonsType];
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
4
|
+
] | [type: 'strContains', value: string] | [type: 'strMatchesRegex', value: RegExp] | [type: 'deepEqual', value: any] | [type: 'numIsGreaterThan', value: number] | [type: 'numIsGreaterThanOrEqual', value: number] | [type: 'numIsLessThan', value: number] | [type: 'numIsLessThanOrEqual', value: number] | [type: 'numIsInRange', value: [number, number]] | [type: 'jsonStringHasPartial', value: any] | [type: 'partialEqual', value: any] | [type: 'custom', value: (target: unknown) => boolean] | [type: 'isInstanceOf', value: new (...args: any[]) => any] | [type: 'keyNotBePresent', value: null] | [type: 'not', value: ComparisonsType] | [type: 'any', value: ComparisonsType[]] | [type: 'all', value: ComparisonsType[]];
|
|
5
|
+
type Comparison = {
|
|
6
|
+
'~sc': ComparisonsType;
|
|
7
|
+
};
|
|
9
8
|
declare const match: {
|
|
10
9
|
hasType: {
|
|
11
|
-
string:
|
|
12
|
-
number:
|
|
13
|
-
boolean:
|
|
14
|
-
object:
|
|
15
|
-
array:
|
|
16
|
-
function:
|
|
10
|
+
string: Comparison;
|
|
11
|
+
number: Comparison;
|
|
12
|
+
boolean: Comparison;
|
|
13
|
+
object: Comparison;
|
|
14
|
+
array: Comparison;
|
|
15
|
+
function: Comparison;
|
|
17
16
|
};
|
|
18
|
-
isInstanceOf: (constructor: new (...args: any[]) => any) =>
|
|
17
|
+
isInstanceOf: (constructor: new (...args: any[]) => any) => Comparison;
|
|
19
18
|
str: {
|
|
20
|
-
contains: (substring: string) =>
|
|
21
|
-
startsWith: (substring: string) =>
|
|
22
|
-
endsWith: (substring: string) =>
|
|
23
|
-
matchesRegex: (regex: RegExp) =>
|
|
19
|
+
contains: (substring: string) => Comparison;
|
|
20
|
+
startsWith: (substring: string) => Comparison;
|
|
21
|
+
endsWith: (substring: string) => Comparison;
|
|
22
|
+
matchesRegex: (regex: RegExp) => Comparison;
|
|
24
23
|
};
|
|
25
24
|
num: {
|
|
26
|
-
isGreaterThan: (value: number) =>
|
|
27
|
-
isGreaterThanOrEqual: (value: number) =>
|
|
28
|
-
isLessThan: (value: number) =>
|
|
29
|
-
isLessThanOrEqual: (value: number) =>
|
|
30
|
-
isInRange: (value: [number, number]) =>
|
|
25
|
+
isGreaterThan: (value: number) => Comparison;
|
|
26
|
+
isGreaterThanOrEqual: (value: number) => Comparison;
|
|
27
|
+
isLessThan: (value: number) => Comparison;
|
|
28
|
+
isLessThanOrEqual: (value: number) => Comparison;
|
|
29
|
+
isInRange: (value: [number, number]) => Comparison;
|
|
31
30
|
};
|
|
32
31
|
jsonString: {
|
|
33
|
-
hasPartial: (value: any) =>
|
|
32
|
+
hasPartial: (value: any) => Comparison;
|
|
34
33
|
};
|
|
35
|
-
equal: (value: any) =>
|
|
36
|
-
partialEqual: (value: any) =>
|
|
37
|
-
custom: (isEqual: (value: unknown) => boolean) =>
|
|
34
|
+
equal: (value: any) => Comparison;
|
|
35
|
+
partialEqual: (value: any) => Comparison;
|
|
36
|
+
custom: (isEqual: (value: unknown) => boolean) => Comparison;
|
|
37
|
+
keyNotBePresent: Comparison;
|
|
38
|
+
any: (...comparisons: Comparison[]) => Comparison;
|
|
39
|
+
all: (...comparisons: Comparison[]) => Comparison;
|
|
38
40
|
not: {
|
|
39
41
|
hasType: {
|
|
40
|
-
string:
|
|
41
|
-
number:
|
|
42
|
-
boolean:
|
|
43
|
-
object:
|
|
44
|
-
array:
|
|
45
|
-
function:
|
|
42
|
+
string: Comparison;
|
|
43
|
+
number: Comparison;
|
|
44
|
+
boolean: Comparison;
|
|
45
|
+
object: Comparison;
|
|
46
|
+
array: Comparison;
|
|
47
|
+
function: Comparison;
|
|
46
48
|
};
|
|
47
|
-
isInstanceOf: (constructor: new (...args: any[]) => any) =>
|
|
49
|
+
isInstanceOf: (constructor: new (...args: any[]) => any) => Comparison;
|
|
48
50
|
str: {
|
|
49
|
-
contains: (substring: string) =>
|
|
50
|
-
startsWith: (substring: string) =>
|
|
51
|
-
endsWith: (substring: string) =>
|
|
52
|
-
matchesRegex: (regex: RegExp) =>
|
|
51
|
+
contains: (substring: string) => Comparison;
|
|
52
|
+
startsWith: (substring: string) => Comparison;
|
|
53
|
+
endsWith: (substring: string) => Comparison;
|
|
54
|
+
matchesRegex: (regex: RegExp) => Comparison;
|
|
53
55
|
};
|
|
54
56
|
num: {
|
|
55
|
-
isGreaterThan: (value: number) =>
|
|
56
|
-
isGreaterThanOrEqual: (value: number) =>
|
|
57
|
-
isLessThan: (value: number) =>
|
|
58
|
-
isLessThanOrEqual: (value: number) =>
|
|
59
|
-
isInRange: (value: [number, number]) =>
|
|
57
|
+
isGreaterThan: (value: number) => Comparison;
|
|
58
|
+
isGreaterThanOrEqual: (value: number) => Comparison;
|
|
59
|
+
isLessThan: (value: number) => Comparison;
|
|
60
|
+
isLessThanOrEqual: (value: number) => Comparison;
|
|
61
|
+
isInRange: (value: [number, number]) => Comparison;
|
|
60
62
|
};
|
|
61
63
|
jsonString: {
|
|
62
|
-
hasPartial: (value: any) =>
|
|
64
|
+
hasPartial: (value: any) => Comparison;
|
|
63
65
|
};
|
|
64
|
-
equal: (value: any) =>
|
|
65
|
-
partialEqual: (value: any) =>
|
|
66
|
-
custom: (value: (target: unknown) => boolean) =>
|
|
66
|
+
equal: (value: any) => Comparison;
|
|
67
|
+
partialEqual: (value: any) => Comparison;
|
|
68
|
+
custom: (value: (target: unknown) => boolean) => Comparison;
|
|
69
|
+
any: (...comparisons: Comparison[]) => Comparison;
|
|
70
|
+
all: (...comparisons: Comparison[]) => Comparison;
|
|
67
71
|
};
|
|
68
72
|
};
|
|
69
73
|
declare function partialEqual(target: any, sub: any): boolean;
|
package/dist/partialEqual.js
CHANGED
|
@@ -4,70 +4,74 @@ import {
|
|
|
4
4
|
|
|
5
5
|
// src/partialEqual.ts
|
|
6
6
|
var has = Object.prototype.hasOwnProperty;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
7
|
+
function createComparison(type) {
|
|
8
|
+
return {
|
|
9
|
+
"~sc": type
|
|
10
|
+
};
|
|
11
|
+
}
|
|
13
12
|
var match = {
|
|
14
13
|
hasType: {
|
|
15
|
-
string:
|
|
16
|
-
number:
|
|
17
|
-
boolean:
|
|
18
|
-
object:
|
|
19
|
-
array:
|
|
20
|
-
function:
|
|
14
|
+
string: createComparison(["hasType", "string"]),
|
|
15
|
+
number: createComparison(["hasType", "number"]),
|
|
16
|
+
boolean: createComparison(["hasType", "boolean"]),
|
|
17
|
+
object: createComparison(["hasType", "object"]),
|
|
18
|
+
array: createComparison(["hasType", "array"]),
|
|
19
|
+
function: createComparison(["hasType", "function"])
|
|
21
20
|
},
|
|
22
|
-
isInstanceOf: (constructor) =>
|
|
21
|
+
isInstanceOf: (constructor) => createComparison(["isInstanceOf", constructor]),
|
|
23
22
|
str: {
|
|
24
|
-
contains: (substring) =>
|
|
25
|
-
startsWith: (substring) =>
|
|
26
|
-
endsWith: (substring) =>
|
|
27
|
-
matchesRegex: (regex) =>
|
|
23
|
+
contains: (substring) => createComparison(["strContains", substring]),
|
|
24
|
+
startsWith: (substring) => createComparison(["strStartsWith", substring]),
|
|
25
|
+
endsWith: (substring) => createComparison(["strEndsWith", substring]),
|
|
26
|
+
matchesRegex: (regex) => createComparison(["strMatchesRegex", regex])
|
|
28
27
|
},
|
|
29
28
|
num: {
|
|
30
|
-
isGreaterThan: (value) =>
|
|
31
|
-
isGreaterThanOrEqual: (value) =>
|
|
32
|
-
isLessThan: (value) =>
|
|
33
|
-
isLessThanOrEqual: (value) =>
|
|
34
|
-
isInRange: (value) =>
|
|
29
|
+
isGreaterThan: (value) => createComparison(["numIsGreaterThan", value]),
|
|
30
|
+
isGreaterThanOrEqual: (value) => createComparison(["numIsGreaterThanOrEqual", value]),
|
|
31
|
+
isLessThan: (value) => createComparison(["numIsLessThan", value]),
|
|
32
|
+
isLessThanOrEqual: (value) => createComparison(["numIsLessThanOrEqual", value]),
|
|
33
|
+
isInRange: (value) => createComparison(["numIsInRange", value])
|
|
35
34
|
},
|
|
36
35
|
jsonString: {
|
|
37
|
-
hasPartial: (value) =>
|
|
36
|
+
hasPartial: (value) => createComparison(["jsonStringHasPartial", value])
|
|
38
37
|
},
|
|
39
|
-
equal: (value) =>
|
|
40
|
-
partialEqual: (value) =>
|
|
41
|
-
custom: (isEqual) =>
|
|
38
|
+
equal: (value) => createComparison(["deepEqual", value]),
|
|
39
|
+
partialEqual: (value) => createComparison(["partialEqual", value]),
|
|
40
|
+
custom: (isEqual) => createComparison(["custom", isEqual]),
|
|
41
|
+
keyNotBePresent: createComparison(["keyNotBePresent", null]),
|
|
42
|
+
any: (...comparisons) => createComparison(["any", comparisons.map((c) => c["~sc"])]),
|
|
43
|
+
all: (...comparisons) => createComparison(["all", comparisons.map((c) => c["~sc"])]),
|
|
42
44
|
not: {
|
|
43
45
|
hasType: {
|
|
44
|
-
string:
|
|
45
|
-
number:
|
|
46
|
-
boolean:
|
|
47
|
-
object:
|
|
48
|
-
array:
|
|
49
|
-
function:
|
|
46
|
+
string: createComparison(["not", ["hasType", "string"]]),
|
|
47
|
+
number: createComparison(["not", ["hasType", "number"]]),
|
|
48
|
+
boolean: createComparison(["not", ["hasType", "boolean"]]),
|
|
49
|
+
object: createComparison(["not", ["hasType", "object"]]),
|
|
50
|
+
array: createComparison(["not", ["hasType", "array"]]),
|
|
51
|
+
function: createComparison(["not", ["hasType", "function"]])
|
|
50
52
|
},
|
|
51
|
-
isInstanceOf: (constructor) =>
|
|
53
|
+
isInstanceOf: (constructor) => createComparison(["not", ["isInstanceOf", constructor]]),
|
|
52
54
|
str: {
|
|
53
|
-
contains: (substring) =>
|
|
54
|
-
startsWith: (substring) =>
|
|
55
|
-
endsWith: (substring) =>
|
|
56
|
-
matchesRegex: (regex) =>
|
|
55
|
+
contains: (substring) => createComparison(["not", ["strContains", substring]]),
|
|
56
|
+
startsWith: (substring) => createComparison(["not", ["strStartsWith", substring]]),
|
|
57
|
+
endsWith: (substring) => createComparison(["not", ["strEndsWith", substring]]),
|
|
58
|
+
matchesRegex: (regex) => createComparison(["not", ["strMatchesRegex", regex]])
|
|
57
59
|
},
|
|
58
60
|
num: {
|
|
59
|
-
isGreaterThan: (value) =>
|
|
60
|
-
isGreaterThanOrEqual: (value) =>
|
|
61
|
-
isLessThan: (value) =>
|
|
62
|
-
isLessThanOrEqual: (value) =>
|
|
63
|
-
isInRange: (value) =>
|
|
61
|
+
isGreaterThan: (value) => createComparison(["not", ["numIsGreaterThan", value]]),
|
|
62
|
+
isGreaterThanOrEqual: (value) => createComparison(["not", ["numIsGreaterThanOrEqual", value]]),
|
|
63
|
+
isLessThan: (value) => createComparison(["not", ["numIsLessThan", value]]),
|
|
64
|
+
isLessThanOrEqual: (value) => createComparison(["not", ["numIsLessThanOrEqual", value]]),
|
|
65
|
+
isInRange: (value) => createComparison(["not", ["numIsInRange", value]])
|
|
64
66
|
},
|
|
65
67
|
jsonString: {
|
|
66
|
-
hasPartial: (value) =>
|
|
68
|
+
hasPartial: (value) => createComparison(["not", ["jsonStringHasPartial", value]])
|
|
67
69
|
},
|
|
68
|
-
equal: (value) =>
|
|
69
|
-
partialEqual: (value) =>
|
|
70
|
-
custom: (value) =>
|
|
70
|
+
equal: (value) => createComparison(["not", ["deepEqual", value]]),
|
|
71
|
+
partialEqual: (value) => createComparison(["not", ["partialEqual", value]]),
|
|
72
|
+
custom: (value) => createComparison(["not", ["custom", value]]),
|
|
73
|
+
any: (...comparisons) => createComparison(["not", ["any", comparisons.map((c) => c["~sc"])]]),
|
|
74
|
+
all: (...comparisons) => createComparison(["not", ["all", comparisons.map((c) => c["~sc"])]])
|
|
71
75
|
}
|
|
72
76
|
};
|
|
73
77
|
function find(iter, tar) {
|
|
@@ -75,6 +79,24 @@ function find(iter, tar) {
|
|
|
75
79
|
if (partialEqual(key, tar)) return key;
|
|
76
80
|
}
|
|
77
81
|
}
|
|
82
|
+
function executeComparisonWithKeyContext(target, comp, keyExists) {
|
|
83
|
+
const [type, value] = comp;
|
|
84
|
+
if (type === "keyNotBePresent") {
|
|
85
|
+
return !keyExists;
|
|
86
|
+
}
|
|
87
|
+
if (type === "any") {
|
|
88
|
+
for (const childComp of value) {
|
|
89
|
+
if (executeComparisonWithKeyContext(target, childComp, keyExists)) {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
if (type === "not") {
|
|
96
|
+
return !executeComparisonWithKeyContext(target, value, keyExists);
|
|
97
|
+
}
|
|
98
|
+
return executeComparison(target, comp);
|
|
99
|
+
}
|
|
78
100
|
function executeComparison(target, comparison) {
|
|
79
101
|
const [type, value] = comparison;
|
|
80
102
|
switch (type) {
|
|
@@ -129,6 +151,22 @@ function executeComparison(target, comparison) {
|
|
|
129
151
|
return partialEqual(target, value);
|
|
130
152
|
case "custom":
|
|
131
153
|
return value(target);
|
|
154
|
+
case "keyNotBePresent":
|
|
155
|
+
return false;
|
|
156
|
+
case "any":
|
|
157
|
+
for (const comp of value) {
|
|
158
|
+
if (executeComparison(target, comp)) {
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return false;
|
|
163
|
+
case "all":
|
|
164
|
+
for (const comp of value) {
|
|
165
|
+
if (!executeComparison(target, comp)) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return true;
|
|
132
170
|
case "not":
|
|
133
171
|
return !executeComparison(target, value);
|
|
134
172
|
default:
|
|
@@ -137,8 +175,8 @@ function executeComparison(target, comparison) {
|
|
|
137
175
|
}
|
|
138
176
|
function partialEqual(target, sub) {
|
|
139
177
|
if (sub === target) return true;
|
|
140
|
-
if (sub
|
|
141
|
-
return executeComparison(target, sub
|
|
178
|
+
if (sub && typeof sub === "object" && "~sc" in sub) {
|
|
179
|
+
return executeComparison(target, sub["~sc"]);
|
|
142
180
|
}
|
|
143
181
|
if (sub && target && sub.constructor === target.constructor) {
|
|
144
182
|
const ctor = sub.constructor;
|
|
@@ -185,8 +223,21 @@ function partialEqual(target, sub) {
|
|
|
185
223
|
if (!ctor || typeof sub === "object") {
|
|
186
224
|
for (const key in sub) {
|
|
187
225
|
if (has.call(sub, key)) {
|
|
188
|
-
|
|
189
|
-
|
|
226
|
+
const subValue = sub[key];
|
|
227
|
+
if (subValue && typeof subValue === "object" && "~sc" in subValue && subValue["~sc"][0] === "keyNotBePresent") {
|
|
228
|
+
if (has.call(target, key)) {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
} else if (subValue && typeof subValue === "object" && "~sc" in subValue && subValue["~sc"][0] === "any") {
|
|
232
|
+
const targetHasKey = has.call(target, key);
|
|
233
|
+
const targetValue = targetHasKey ? target[key] : void 0;
|
|
234
|
+
if (!executeComparisonWithKeyContext(targetValue, subValue["~sc"], targetHasKey)) {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
} else {
|
|
238
|
+
if (!has.call(target, key) || !partialEqual(target[key], subValue)) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
190
241
|
}
|
|
191
242
|
}
|
|
192
243
|
}
|