@ls-stack/utils 3.44.1 → 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.
@@ -125,6 +125,9 @@ var match = {
125
125
  equal: (value) => createComparison(["deepEqual", value]),
126
126
  partialEqual: (value) => createComparison(["partialEqual", value]),
127
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"])]),
128
131
  not: {
129
132
  hasType: {
130
133
  string: createComparison(["not", ["hasType", "string"]]),
@@ -153,7 +156,9 @@ var match = {
153
156
  },
154
157
  equal: (value) => createComparison(["not", ["deepEqual", value]]),
155
158
  partialEqual: (value) => createComparison(["not", ["partialEqual", value]]),
156
- custom: (value) => createComparison(["not", ["custom", 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"])]])
157
162
  }
158
163
  };
159
164
  function find2(iter, tar) {
@@ -161,6 +166,24 @@ function find2(iter, tar) {
161
166
  if (partialEqual(key, tar)) return key;
162
167
  }
163
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
+ }
164
187
  function executeComparison(target, comparison) {
165
188
  const [type, value] = comparison;
166
189
  switch (type) {
@@ -215,6 +238,22 @@ function executeComparison(target, comparison) {
215
238
  return partialEqual(target, value);
216
239
  case "custom":
217
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;
218
257
  case "not":
219
258
  return !executeComparison(target, value);
220
259
  default:
@@ -271,8 +310,21 @@ function partialEqual(target, sub) {
271
310
  if (!ctor || typeof sub === "object") {
272
311
  for (const key in sub) {
273
312
  if (has2.call(sub, key)) {
274
- if (!has2.call(target, key) || !partialEqual(target[key], sub[key])) {
275
- 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
+ }
276
328
  }
277
329
  }
278
330
  }
@@ -1,7 +1,7 @@
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];
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
5
  type Comparison = {
6
6
  '~sc': ComparisonsType;
7
7
  };
@@ -34,6 +34,9 @@ declare const match: {
34
34
  equal: (value: any) => Comparison;
35
35
  partialEqual: (value: any) => Comparison;
36
36
  custom: (isEqual: (value: unknown) => boolean) => Comparison;
37
+ keyNotBePresent: Comparison;
38
+ any: (...comparisons: Comparison[]) => Comparison;
39
+ all: (...comparisons: Comparison[]) => Comparison;
37
40
  not: {
38
41
  hasType: {
39
42
  string: Comparison;
@@ -63,6 +66,8 @@ declare const match: {
63
66
  equal: (value: any) => Comparison;
64
67
  partialEqual: (value: any) => Comparison;
65
68
  custom: (value: (target: unknown) => boolean) => Comparison;
69
+ any: (...comparisons: Comparison[]) => Comparison;
70
+ all: (...comparisons: Comparison[]) => Comparison;
66
71
  };
67
72
  };
68
73
  declare function partialEqual(target: any, sub: any): boolean;
@@ -1,7 +1,7 @@
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];
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
5
  type Comparison = {
6
6
  '~sc': ComparisonsType;
7
7
  };
@@ -34,6 +34,9 @@ declare const match: {
34
34
  equal: (value: any) => Comparison;
35
35
  partialEqual: (value: any) => Comparison;
36
36
  custom: (isEqual: (value: unknown) => boolean) => Comparison;
37
+ keyNotBePresent: Comparison;
38
+ any: (...comparisons: Comparison[]) => Comparison;
39
+ all: (...comparisons: Comparison[]) => Comparison;
37
40
  not: {
38
41
  hasType: {
39
42
  string: Comparison;
@@ -63,6 +66,8 @@ declare const match: {
63
66
  equal: (value: any) => Comparison;
64
67
  partialEqual: (value: any) => Comparison;
65
68
  custom: (value: (target: unknown) => boolean) => Comparison;
69
+ any: (...comparisons: Comparison[]) => Comparison;
70
+ all: (...comparisons: Comparison[]) => Comparison;
66
71
  };
67
72
  };
68
73
  declare function partialEqual(target: any, sub: any): boolean;
@@ -38,6 +38,9 @@ var match = {
38
38
  equal: (value) => createComparison(["deepEqual", value]),
39
39
  partialEqual: (value) => createComparison(["partialEqual", value]),
40
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"])]),
41
44
  not: {
42
45
  hasType: {
43
46
  string: createComparison(["not", ["hasType", "string"]]),
@@ -66,7 +69,9 @@ var match = {
66
69
  },
67
70
  equal: (value) => createComparison(["not", ["deepEqual", value]]),
68
71
  partialEqual: (value) => createComparison(["not", ["partialEqual", value]]),
69
- custom: (value) => createComparison(["not", ["custom", 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"])]])
70
75
  }
71
76
  };
72
77
  function find(iter, tar) {
@@ -74,6 +79,24 @@ function find(iter, tar) {
74
79
  if (partialEqual(key, tar)) return key;
75
80
  }
76
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
+ }
77
100
  function executeComparison(target, comparison) {
78
101
  const [type, value] = comparison;
79
102
  switch (type) {
@@ -128,6 +151,22 @@ function executeComparison(target, comparison) {
128
151
  return partialEqual(target, value);
129
152
  case "custom":
130
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;
131
170
  case "not":
132
171
  return !executeComparison(target, value);
133
172
  default:
@@ -184,8 +223,21 @@ function partialEqual(target, sub) {
184
223
  if (!ctor || typeof sub === "object") {
185
224
  for (const key in sub) {
186
225
  if (has.call(sub, key)) {
187
- if (!has.call(target, key) || !partialEqual(target[key], sub[key])) {
188
- 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
+ }
189
241
  }
190
242
  }
191
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.1",
4
+ "version": "3.45.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist",