@rimbu/deep 0.14.5 → 1.0.0-alpha.1

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.
Files changed (86) hide show
  1. package/{src/deep.ts → dist/bun/deep.mts} +7 -7
  2. package/{src/index.ts → dist/bun/index.mts} +5 -5
  3. package/dist/bun/internal.mts +8 -0
  4. package/{src/match.ts → dist/bun/match.mts} +6 -6
  5. package/{src/patch.ts → dist/bun/patch.mts} +9 -4
  6. package/{src/path.ts → dist/bun/path.mts} +2 -2
  7. package/{src/selector.ts → dist/bun/selector.mts} +1 -1
  8. package/dist/cjs/deep.js +590 -0
  9. package/dist/cjs/index.js +663 -0
  10. package/dist/cjs/internal.js +583 -0
  11. package/dist/cjs/match.js +376 -0
  12. package/dist/cjs/patch.js +105 -0
  13. package/dist/cjs/path.js +585 -0
  14. package/dist/cjs/protected.js +18 -0
  15. package/dist/cjs/selector.js +581 -0
  16. package/dist/cjs/tuple.js +73 -0
  17. package/dist/{module/deep.js → esm/deep.mjs} +6 -6
  18. package/dist/esm/deep.mjs.map +1 -0
  19. package/dist/{module/index.js → esm/index.mjs} +5 -5
  20. package/dist/esm/index.mjs.map +1 -0
  21. package/dist/esm/internal.mjs +5 -0
  22. package/dist/esm/internal.mjs.map +1 -0
  23. package/dist/{module/match.js → esm/match.mjs} +21 -21
  24. package/dist/esm/match.mjs.map +1 -0
  25. package/dist/{module/patch.js → esm/patch.mjs} +5 -5
  26. package/dist/esm/patch.mjs.map +1 -0
  27. package/dist/{module/path.js → esm/path.mjs} +2 -2
  28. package/dist/esm/path.mjs.map +1 -0
  29. package/dist/esm/protected.mjs +2 -0
  30. package/dist/esm/protected.mjs.map +1 -0
  31. package/dist/{module/selector.js → esm/selector.mjs} +2 -2
  32. package/dist/esm/selector.mjs.map +1 -0
  33. package/dist/{module/tuple.js → esm/tuple.mjs} +1 -1
  34. package/dist/esm/tuple.mjs.map +1 -0
  35. package/dist/types/{deep.d.ts → deep.d.mts} +6 -6
  36. package/dist/types/{index.d.ts → index.d.mts} +5 -5
  37. package/dist/types/internal.d.mts +7 -0
  38. package/dist/types/{match.d.ts → match.d.mts} +3 -3
  39. package/dist/types/{patch.d.ts → patch.d.mts} +3 -3
  40. package/dist/types/{path.d.ts → path.d.mts} +2 -2
  41. package/dist/types/{selector.d.ts → selector.d.mts} +1 -1
  42. package/package.json +29 -23
  43. package/src/deep.mts +364 -0
  44. package/src/index.mts +23 -0
  45. package/src/internal.mts +8 -0
  46. package/src/match.mts +700 -0
  47. package/src/patch.mts +262 -0
  48. package/src/path.mts +430 -0
  49. package/src/protected.mts +30 -0
  50. package/src/selector.mts +90 -0
  51. package/src/tuple.mts +197 -0
  52. package/dist/main/deep.js +0 -211
  53. package/dist/main/deep.js.map +0 -1
  54. package/dist/main/index.js +0 -19
  55. package/dist/main/index.js.map +0 -1
  56. package/dist/main/internal.js +0 -9
  57. package/dist/main/internal.js.map +0 -1
  58. package/dist/main/match.js +0 -284
  59. package/dist/main/match.js.map +0 -1
  60. package/dist/main/patch.js +0 -133
  61. package/dist/main/patch.js.map +0 -1
  62. package/dist/main/path.js +0 -126
  63. package/dist/main/path.js.map +0 -1
  64. package/dist/main/protected.js +0 -3
  65. package/dist/main/protected.js.map +0 -1
  66. package/dist/main/selector.js +0 -40
  67. package/dist/main/selector.js.map +0 -1
  68. package/dist/main/tuple.js +0 -164
  69. package/dist/main/tuple.js.map +0 -1
  70. package/dist/module/deep.js.map +0 -1
  71. package/dist/module/index.js.map +0 -1
  72. package/dist/module/internal.js +0 -4
  73. package/dist/module/internal.js.map +0 -1
  74. package/dist/module/match.js.map +0 -1
  75. package/dist/module/patch.js.map +0 -1
  76. package/dist/module/path.js.map +0 -1
  77. package/dist/module/protected.js +0 -2
  78. package/dist/module/protected.js.map +0 -1
  79. package/dist/module/selector.js.map +0 -1
  80. package/dist/module/tuple.js.map +0 -1
  81. package/dist/types/internal.d.ts +0 -7
  82. package/src/internal.ts +0 -8
  83. /package/{src/protected.ts → dist/bun/protected.mts} +0 -0
  84. /package/{src/tuple.ts → dist/bun/tuple.mts} +0 -0
  85. /package/dist/types/{protected.d.ts → protected.d.mts} +0 -0
  86. /package/dist/types/{tuple.d.ts → tuple.d.mts} +0 -0
@@ -0,0 +1,376 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/match.mts
21
+ var match_exports = {};
22
+ __export(match_exports, {
23
+ match: () => match
24
+ });
25
+ module.exports = __toCommonJS(match_exports);
26
+ var import_base = require("@rimbu/base");
27
+ function match(source, matcher, failureLog) {
28
+ return matchEntry(source, source, source, matcher, failureLog);
29
+ }
30
+ function matchEntry(source, parent, root, matcher, failureLog) {
31
+ if (Object.is(source, matcher)) {
32
+ return true;
33
+ }
34
+ if (matcher === null || matcher === void 0) {
35
+ failureLog?.push(
36
+ `value ${JSON.stringify(source)} did not match matcher ${matcher}`
37
+ );
38
+ return false;
39
+ }
40
+ if (typeof source === "function") {
41
+ const result = Object.is(source, matcher);
42
+ if (!result) {
43
+ failureLog?.push(
44
+ `both value and matcher are functions, but they do not have the same reference`
45
+ );
46
+ }
47
+ return result;
48
+ }
49
+ if (typeof matcher === "function") {
50
+ const matcherResult = matcher(source, parent, root);
51
+ if (typeof matcherResult === "boolean") {
52
+ if (!matcherResult) {
53
+ failureLog?.push(
54
+ `function matcher returned false for value ${JSON.stringify(source)}`
55
+ );
56
+ }
57
+ return matcherResult;
58
+ }
59
+ return matchEntry(source, parent, root, matcherResult, failureLog);
60
+ }
61
+ if ((0, import_base.isPlainObj)(source)) {
62
+ return matchPlainObj(source, parent, root, matcher, failureLog);
63
+ }
64
+ if (Array.isArray(source)) {
65
+ return matchArr(source, parent, root, matcher, failureLog);
66
+ }
67
+ failureLog?.push(
68
+ `value ${JSON.stringify(
69
+ source
70
+ )} does not match given matcher ${JSON.stringify(matcher)}`
71
+ );
72
+ return false;
73
+ }
74
+ function matchArr(source, parent, root, matcher, failureLog) {
75
+ if (Array.isArray(matcher)) {
76
+ const length = source.length;
77
+ if (length !== matcher.length) {
78
+ failureLog?.push(
79
+ `array lengths are not equal: value length ${source.length} !== matcher length ${matcher.length}`
80
+ );
81
+ return false;
82
+ }
83
+ let index = -1;
84
+ while (++index < length) {
85
+ if (!matchEntry(source[index], source, root, matcher[index], failureLog)) {
86
+ failureLog?.push(
87
+ `index ${index} does not match with value ${JSON.stringify(
88
+ source[index]
89
+ )} and matcher ${matcher[index]}`
90
+ );
91
+ return false;
92
+ }
93
+ }
94
+ return true;
95
+ }
96
+ if (typeof matcher === "object" && null !== matcher) {
97
+ if (`every` in matcher) {
98
+ return matchCompound(
99
+ source,
100
+ parent,
101
+ root,
102
+ ["every", ...matcher.every],
103
+ failureLog
104
+ );
105
+ }
106
+ if (`some` in matcher) {
107
+ return matchCompound(
108
+ source,
109
+ parent,
110
+ root,
111
+ ["some", ...matcher.some],
112
+ failureLog
113
+ );
114
+ }
115
+ if (`none` in matcher) {
116
+ return matchCompound(
117
+ source,
118
+ parent,
119
+ root,
120
+ ["none", ...matcher.none],
121
+ failureLog
122
+ );
123
+ }
124
+ if (`single` in matcher) {
125
+ return matchCompound(
126
+ source,
127
+ parent,
128
+ root,
129
+ ["single", ...matcher.single],
130
+ failureLog
131
+ );
132
+ }
133
+ if (`someItem` in matcher) {
134
+ return matchTraversal(
135
+ source,
136
+ root,
137
+ "someItem",
138
+ matcher.someItem,
139
+ failureLog
140
+ );
141
+ }
142
+ if (`everyItem` in matcher) {
143
+ return matchTraversal(
144
+ source,
145
+ root,
146
+ "everyItem",
147
+ matcher.everyItem,
148
+ failureLog
149
+ );
150
+ }
151
+ if (`noneItem` in matcher) {
152
+ return matchTraversal(
153
+ source,
154
+ root,
155
+ "noneItem",
156
+ matcher.noneItem,
157
+ failureLog
158
+ );
159
+ }
160
+ if (`singleItem` in matcher) {
161
+ return matchTraversal(
162
+ source,
163
+ root,
164
+ "singleItem",
165
+ matcher.singleItem,
166
+ failureLog
167
+ );
168
+ }
169
+ }
170
+ for (const index in matcher) {
171
+ const matcherAtIndex = matcher[index];
172
+ if (!(index in source)) {
173
+ failureLog?.push(
174
+ `index ${index} does not exist in source ${JSON.stringify(
175
+ source
176
+ )} but should match matcher ${JSON.stringify(matcherAtIndex)}`
177
+ );
178
+ return false;
179
+ }
180
+ const result = matchEntry(
181
+ source[index],
182
+ source,
183
+ root,
184
+ matcherAtIndex,
185
+ failureLog
186
+ );
187
+ if (!result) {
188
+ failureLog?.push(
189
+ `index ${index} does not match with value ${JSON.stringify(
190
+ source[index]
191
+ )} and matcher ${JSON.stringify(matcherAtIndex)}`
192
+ );
193
+ return false;
194
+ }
195
+ }
196
+ return true;
197
+ }
198
+ function matchPlainObj(source, parent, root, matcher, failureLog) {
199
+ if (Array.isArray(matcher)) {
200
+ return matchCompound(source, parent, root, matcher, failureLog);
201
+ }
202
+ for (const key in matcher) {
203
+ if (!(key in source)) {
204
+ failureLog?.push(
205
+ `key ${key} is specified in matcher but not present in value ${JSON.stringify(
206
+ source
207
+ )}`
208
+ );
209
+ return false;
210
+ }
211
+ const result = matchEntry(
212
+ source[key],
213
+ source,
214
+ root,
215
+ matcher[key],
216
+ failureLog
217
+ );
218
+ if (!result) {
219
+ failureLog?.push(
220
+ `key ${key} does not match in value ${JSON.stringify(
221
+ source[key]
222
+ )} with matcher ${JSON.stringify(matcher[key])}`
223
+ );
224
+ return false;
225
+ }
226
+ }
227
+ return true;
228
+ }
229
+ function matchCompound(source, parent, root, compound, failureLog) {
230
+ const matchType = compound[0];
231
+ const length = compound.length;
232
+ let index = 0;
233
+ switch (matchType) {
234
+ case "every": {
235
+ while (++index < length) {
236
+ const result = matchEntry(
237
+ source,
238
+ parent,
239
+ root,
240
+ compound[index],
241
+ failureLog
242
+ );
243
+ if (!result) {
244
+ failureLog?.push(
245
+ `in compound "every": match at index ${index} failed`
246
+ );
247
+ return false;
248
+ }
249
+ }
250
+ return true;
251
+ }
252
+ case "none": {
253
+ while (++index < length) {
254
+ const result = matchEntry(
255
+ source,
256
+ parent,
257
+ root,
258
+ compound[index],
259
+ failureLog
260
+ );
261
+ if (result) {
262
+ failureLog?.push(
263
+ `in compound "none": match at index ${index} succeeded`
264
+ );
265
+ return false;
266
+ }
267
+ }
268
+ return true;
269
+ }
270
+ case "single": {
271
+ let onePassed = false;
272
+ while (++index < length) {
273
+ const result = matchEntry(
274
+ source,
275
+ parent,
276
+ root,
277
+ compound[index],
278
+ failureLog
279
+ );
280
+ if (result) {
281
+ if (onePassed) {
282
+ failureLog?.push(
283
+ `in compound "single": multiple matches succeeded`
284
+ );
285
+ return false;
286
+ }
287
+ onePassed = true;
288
+ }
289
+ }
290
+ if (!onePassed) {
291
+ failureLog?.push(`in compound "single": no matches succeeded`);
292
+ }
293
+ return onePassed;
294
+ }
295
+ case "some": {
296
+ while (++index < length) {
297
+ const result = matchEntry(
298
+ source,
299
+ parent,
300
+ root,
301
+ compound[index],
302
+ failureLog
303
+ );
304
+ if (result) {
305
+ return true;
306
+ }
307
+ }
308
+ failureLog?.push(`in compound "some": no matches succeeded`);
309
+ return false;
310
+ }
311
+ }
312
+ }
313
+ function matchTraversal(source, root, matchType, matcher, failureLog) {
314
+ let index = -1;
315
+ const length = source.length;
316
+ switch (matchType) {
317
+ case "someItem": {
318
+ while (++index < length) {
319
+ if (matchEntry(source[index], source, root, matcher, failureLog)) {
320
+ return true;
321
+ }
322
+ }
323
+ failureLog?.push(
324
+ `in array traversal "someItem": no items matched given matcher`
325
+ );
326
+ return false;
327
+ }
328
+ case "everyItem": {
329
+ while (++index < length) {
330
+ if (!matchEntry(source[index], source, root, matcher, failureLog)) {
331
+ failureLog?.push(
332
+ `in array traversal "everyItem": at least one item did not match given matcher`
333
+ );
334
+ return false;
335
+ }
336
+ }
337
+ return true;
338
+ }
339
+ case "noneItem": {
340
+ while (++index < length) {
341
+ if (matchEntry(source[index], source, root, matcher, failureLog)) {
342
+ failureLog?.push(
343
+ `in array traversal "noneItem": at least one item matched given matcher`
344
+ );
345
+ return false;
346
+ }
347
+ }
348
+ return true;
349
+ }
350
+ case "singleItem": {
351
+ let singleMatched = false;
352
+ while (++index < length) {
353
+ if (matchEntry(source[index], source, root, matcher, failureLog)) {
354
+ if (singleMatched) {
355
+ failureLog?.push(
356
+ `in array traversal "singleItem": more than one item matched given matcher`
357
+ );
358
+ return false;
359
+ }
360
+ singleMatched = true;
361
+ }
362
+ }
363
+ if (!singleMatched) {
364
+ failureLog?.push(
365
+ `in array traversal "singleItem": no item matched given matcher`
366
+ );
367
+ return false;
368
+ }
369
+ return true;
370
+ }
371
+ }
372
+ }
373
+ // Annotate the CommonJS export names for ESM import in node:
374
+ 0 && (module.exports = {
375
+ match
376
+ });
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/patch.mts
21
+ var patch_exports = {};
22
+ __export(patch_exports, {
23
+ patch: () => patch
24
+ });
25
+ module.exports = __toCommonJS(patch_exports);
26
+ var import_base = require("@rimbu/base");
27
+ function patch(value, patchItem) {
28
+ return patchEntry(value, value, value, patchItem);
29
+ }
30
+ function patchEntry(value, parent, root, patchItem) {
31
+ if (Object.is(value, patchItem)) {
32
+ return value;
33
+ }
34
+ if (typeof value === "function") {
35
+ return patchItem;
36
+ }
37
+ if (typeof patchItem === "function") {
38
+ const item = patchItem(value, parent, root);
39
+ return patchEntry(value, parent, root, item);
40
+ }
41
+ if ((0, import_base.isPlainObj)(value)) {
42
+ return patchPlainObj(value, root, patchItem);
43
+ }
44
+ if (Array.isArray(value)) {
45
+ return patchArr(value, root, patchItem);
46
+ }
47
+ return patchItem;
48
+ }
49
+ function patchPlainObj(value, root, patchItem) {
50
+ if (!Array.isArray(patchItem)) {
51
+ return patchItem;
52
+ }
53
+ const result = { ...value };
54
+ let anyChange = false;
55
+ for (const entry of patchItem) {
56
+ const currentRoot = value === root ? { ...result } : root;
57
+ const parent = { ...result };
58
+ for (const key in entry) {
59
+ const currentValue = result[key];
60
+ const newValue = patchEntry(
61
+ currentValue,
62
+ parent,
63
+ currentRoot,
64
+ entry[key]
65
+ );
66
+ if (!Object.is(currentValue, newValue)) {
67
+ anyChange = true;
68
+ result[key] = newValue;
69
+ }
70
+ }
71
+ }
72
+ if (anyChange) {
73
+ return result;
74
+ }
75
+ return value;
76
+ }
77
+ function patchArr(value, root, patchItem) {
78
+ if (Array.isArray(patchItem)) {
79
+ return patchItem;
80
+ }
81
+ const result = [...value];
82
+ let anyChange = false;
83
+ for (const index in patchItem) {
84
+ const numIndex = index;
85
+ const currentValue = result[numIndex];
86
+ const newValue = patchEntry(
87
+ currentValue,
88
+ value,
89
+ root,
90
+ patchItem[index]
91
+ );
92
+ if (!Object.is(newValue, currentValue)) {
93
+ anyChange = true;
94
+ result[numIndex] = newValue;
95
+ }
96
+ }
97
+ if (anyChange) {
98
+ return result;
99
+ }
100
+ return value;
101
+ }
102
+ // Annotate the CommonJS export names for ESM import in node:
103
+ 0 && (module.exports = {
104
+ patch
105
+ });