@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.
@@ -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
- var Comparisons = class {
95
- type;
96
- constructor(type) {
97
- this.type = type;
98
- }
99
- };
94
+ function createComparison(type) {
95
+ return {
96
+ "~sc": type
97
+ };
98
+ }
100
99
  var match = {
101
100
  hasType: {
102
- string: new Comparisons(["hasType", "string"]),
103
- number: new Comparisons(["hasType", "number"]),
104
- boolean: new Comparisons(["hasType", "boolean"]),
105
- object: new Comparisons(["hasType", "object"]),
106
- array: new Comparisons(["hasType", "array"]),
107
- function: new Comparisons(["hasType", "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) => new Comparisons(["isInstanceOf", constructor]),
108
+ isInstanceOf: (constructor) => createComparison(["isInstanceOf", constructor]),
110
109
  str: {
111
- contains: (substring) => new Comparisons(["strContains", substring]),
112
- startsWith: (substring) => new Comparisons(["strStartsWith", substring]),
113
- endsWith: (substring) => new Comparisons(["strEndsWith", substring]),
114
- matchesRegex: (regex) => new Comparisons(["strMatchesRegex", 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) => new Comparisons(["numIsGreaterThan", value]),
118
- isGreaterThanOrEqual: (value) => new Comparisons(["numIsGreaterThanOrEqual", value]),
119
- isLessThan: (value) => new Comparisons(["numIsLessThan", value]),
120
- isLessThanOrEqual: (value) => new Comparisons(["numIsLessThanOrEqual", value]),
121
- isInRange: (value) => new Comparisons(["numIsInRange", 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) => new Comparisons(["jsonStringHasPartial", value])
123
+ hasPartial: (value) => createComparison(["jsonStringHasPartial", value])
125
124
  },
126
- equal: (value) => new Comparisons(["deepEqual", value]),
127
- partialEqual: (value) => new Comparisons(["partialEqual", value]),
128
- custom: (isEqual) => new Comparisons(["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: new Comparisons(["not", ["hasType", "string"]]),
132
- number: new Comparisons(["not", ["hasType", "number"]]),
133
- boolean: new Comparisons(["not", ["hasType", "boolean"]]),
134
- object: new Comparisons(["not", ["hasType", "object"]]),
135
- array: new Comparisons(["not", ["hasType", "array"]]),
136
- function: new Comparisons(["not", ["hasType", "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) => new Comparisons(["not", ["isInstanceOf", constructor]]),
140
+ isInstanceOf: (constructor) => createComparison(["not", ["isInstanceOf", constructor]]),
139
141
  str: {
140
- contains: (substring) => new Comparisons(["not", ["strContains", substring]]),
141
- startsWith: (substring) => new Comparisons(["not", ["strStartsWith", substring]]),
142
- endsWith: (substring) => new Comparisons(["not", ["strEndsWith", substring]]),
143
- matchesRegex: (regex) => new Comparisons(["not", ["strMatchesRegex", 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) => new Comparisons(["not", ["numIsGreaterThan", value]]),
147
- isGreaterThanOrEqual: (value) => new Comparisons(["not", ["numIsGreaterThanOrEqual", value]]),
148
- isLessThan: (value) => new Comparisons(["not", ["numIsLessThan", value]]),
149
- isLessThanOrEqual: (value) => new Comparisons(["not", ["numIsLessThanOrEqual", value]]),
150
- isInRange: (value) => new Comparisons(["not", ["numIsInRange", 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) => new Comparisons(["not", ["jsonStringHasPartial", value]])
155
+ hasPartial: (value) => createComparison(["not", ["jsonStringHasPartial", value]])
154
156
  },
155
- equal: (value) => new Comparisons(["not", ["deepEqual", value]]),
156
- partialEqual: (value) => new Comparisons(["not", ["partialEqual", value]]),
157
- custom: (value) => new Comparisons(["not", ["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 instanceof Comparisons) {
228
- return executeComparison(target, sub.type);
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
- if (!has2.call(target, key) || !partialEqual(target[key], sub[key])) {
276
- return false;
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
  }
@@ -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
- declare class Comparisons {
6
- type: ComparisonsType;
7
- constructor(type: ComparisonsType);
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: Comparisons;
12
- number: Comparisons;
13
- boolean: Comparisons;
14
- object: Comparisons;
15
- array: Comparisons;
16
- function: Comparisons;
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) => Comparisons;
17
+ isInstanceOf: (constructor: new (...args: any[]) => any) => Comparison;
19
18
  str: {
20
- contains: (substring: string) => Comparisons;
21
- startsWith: (substring: string) => Comparisons;
22
- endsWith: (substring: string) => Comparisons;
23
- matchesRegex: (regex: RegExp) => Comparisons;
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) => Comparisons;
27
- isGreaterThanOrEqual: (value: number) => Comparisons;
28
- isLessThan: (value: number) => Comparisons;
29
- isLessThanOrEqual: (value: number) => Comparisons;
30
- isInRange: (value: [number, number]) => Comparisons;
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) => Comparisons;
32
+ hasPartial: (value: any) => Comparison;
34
33
  };
35
- equal: (value: any) => Comparisons;
36
- partialEqual: (value: any) => Comparisons;
37
- custom: (isEqual: (value: unknown) => boolean) => Comparisons;
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: Comparisons;
41
- number: Comparisons;
42
- boolean: Comparisons;
43
- object: Comparisons;
44
- array: Comparisons;
45
- function: Comparisons;
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) => Comparisons;
49
+ isInstanceOf: (constructor: new (...args: any[]) => any) => Comparison;
48
50
  str: {
49
- contains: (substring: string) => Comparisons;
50
- startsWith: (substring: string) => Comparisons;
51
- endsWith: (substring: string) => Comparisons;
52
- matchesRegex: (regex: RegExp) => Comparisons;
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) => Comparisons;
56
- isGreaterThanOrEqual: (value: number) => Comparisons;
57
- isLessThan: (value: number) => Comparisons;
58
- isLessThanOrEqual: (value: number) => Comparisons;
59
- isInRange: (value: [number, number]) => Comparisons;
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) => Comparisons;
64
+ hasPartial: (value: any) => Comparison;
63
65
  };
64
- equal: (value: any) => Comparisons;
65
- partialEqual: (value: any) => Comparisons;
66
- custom: (value: (target: unknown) => boolean) => Comparisons;
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;
@@ -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
- declare class Comparisons {
6
- type: ComparisonsType;
7
- constructor(type: ComparisonsType);
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: Comparisons;
12
- number: Comparisons;
13
- boolean: Comparisons;
14
- object: Comparisons;
15
- array: Comparisons;
16
- function: Comparisons;
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) => Comparisons;
17
+ isInstanceOf: (constructor: new (...args: any[]) => any) => Comparison;
19
18
  str: {
20
- contains: (substring: string) => Comparisons;
21
- startsWith: (substring: string) => Comparisons;
22
- endsWith: (substring: string) => Comparisons;
23
- matchesRegex: (regex: RegExp) => Comparisons;
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) => Comparisons;
27
- isGreaterThanOrEqual: (value: number) => Comparisons;
28
- isLessThan: (value: number) => Comparisons;
29
- isLessThanOrEqual: (value: number) => Comparisons;
30
- isInRange: (value: [number, number]) => Comparisons;
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) => Comparisons;
32
+ hasPartial: (value: any) => Comparison;
34
33
  };
35
- equal: (value: any) => Comparisons;
36
- partialEqual: (value: any) => Comparisons;
37
- custom: (isEqual: (value: unknown) => boolean) => Comparisons;
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: Comparisons;
41
- number: Comparisons;
42
- boolean: Comparisons;
43
- object: Comparisons;
44
- array: Comparisons;
45
- function: Comparisons;
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) => Comparisons;
49
+ isInstanceOf: (constructor: new (...args: any[]) => any) => Comparison;
48
50
  str: {
49
- contains: (substring: string) => Comparisons;
50
- startsWith: (substring: string) => Comparisons;
51
- endsWith: (substring: string) => Comparisons;
52
- matchesRegex: (regex: RegExp) => Comparisons;
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) => Comparisons;
56
- isGreaterThanOrEqual: (value: number) => Comparisons;
57
- isLessThan: (value: number) => Comparisons;
58
- isLessThanOrEqual: (value: number) => Comparisons;
59
- isInRange: (value: [number, number]) => Comparisons;
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) => Comparisons;
64
+ hasPartial: (value: any) => Comparison;
63
65
  };
64
- equal: (value: any) => Comparisons;
65
- partialEqual: (value: any) => Comparisons;
66
- custom: (value: (target: unknown) => boolean) => Comparisons;
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;
@@ -4,70 +4,74 @@ import {
4
4
 
5
5
  // src/partialEqual.ts
6
6
  var has = Object.prototype.hasOwnProperty;
7
- var Comparisons = class {
8
- type;
9
- constructor(type) {
10
- this.type = type;
11
- }
12
- };
7
+ function createComparison(type) {
8
+ return {
9
+ "~sc": type
10
+ };
11
+ }
13
12
  var match = {
14
13
  hasType: {
15
- string: new Comparisons(["hasType", "string"]),
16
- number: new Comparisons(["hasType", "number"]),
17
- boolean: new Comparisons(["hasType", "boolean"]),
18
- object: new Comparisons(["hasType", "object"]),
19
- array: new Comparisons(["hasType", "array"]),
20
- function: new Comparisons(["hasType", "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) => new Comparisons(["isInstanceOf", constructor]),
21
+ isInstanceOf: (constructor) => createComparison(["isInstanceOf", constructor]),
23
22
  str: {
24
- contains: (substring) => new Comparisons(["strContains", substring]),
25
- startsWith: (substring) => new Comparisons(["strStartsWith", substring]),
26
- endsWith: (substring) => new Comparisons(["strEndsWith", substring]),
27
- matchesRegex: (regex) => new Comparisons(["strMatchesRegex", 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) => new Comparisons(["numIsGreaterThan", value]),
31
- isGreaterThanOrEqual: (value) => new Comparisons(["numIsGreaterThanOrEqual", value]),
32
- isLessThan: (value) => new Comparisons(["numIsLessThan", value]),
33
- isLessThanOrEqual: (value) => new Comparisons(["numIsLessThanOrEqual", value]),
34
- isInRange: (value) => new Comparisons(["numIsInRange", 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) => new Comparisons(["jsonStringHasPartial", value])
36
+ hasPartial: (value) => createComparison(["jsonStringHasPartial", value])
38
37
  },
39
- equal: (value) => new Comparisons(["deepEqual", value]),
40
- partialEqual: (value) => new Comparisons(["partialEqual", value]),
41
- custom: (isEqual) => new Comparisons(["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: new Comparisons(["not", ["hasType", "string"]]),
45
- number: new Comparisons(["not", ["hasType", "number"]]),
46
- boolean: new Comparisons(["not", ["hasType", "boolean"]]),
47
- object: new Comparisons(["not", ["hasType", "object"]]),
48
- array: new Comparisons(["not", ["hasType", "array"]]),
49
- function: new Comparisons(["not", ["hasType", "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) => new Comparisons(["not", ["isInstanceOf", constructor]]),
53
+ isInstanceOf: (constructor) => createComparison(["not", ["isInstanceOf", constructor]]),
52
54
  str: {
53
- contains: (substring) => new Comparisons(["not", ["strContains", substring]]),
54
- startsWith: (substring) => new Comparisons(["not", ["strStartsWith", substring]]),
55
- endsWith: (substring) => new Comparisons(["not", ["strEndsWith", substring]]),
56
- matchesRegex: (regex) => new Comparisons(["not", ["strMatchesRegex", 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) => new Comparisons(["not", ["numIsGreaterThan", value]]),
60
- isGreaterThanOrEqual: (value) => new Comparisons(["not", ["numIsGreaterThanOrEqual", value]]),
61
- isLessThan: (value) => new Comparisons(["not", ["numIsLessThan", value]]),
62
- isLessThanOrEqual: (value) => new Comparisons(["not", ["numIsLessThanOrEqual", value]]),
63
- isInRange: (value) => new Comparisons(["not", ["numIsInRange", 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) => new Comparisons(["not", ["jsonStringHasPartial", value]])
68
+ hasPartial: (value) => createComparison(["not", ["jsonStringHasPartial", value]])
67
69
  },
68
- equal: (value) => new Comparisons(["not", ["deepEqual", value]]),
69
- partialEqual: (value) => new Comparisons(["not", ["partialEqual", value]]),
70
- custom: (value) => new Comparisons(["not", ["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 instanceof Comparisons) {
141
- return executeComparison(target, sub.type);
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
- if (!has.call(target, key) || !partialEqual(target[key], sub[key])) {
189
- return false;
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
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Universal TypeScript utilities for browser and Node.js",
4
- "version": "3.44.0",
4
+ "version": "3.45.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist",