@valbuild/core 0.12.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.
Files changed (100) hide show
  1. package/CHANGELOG.md +0 -0
  2. package/README.md +537 -0
  3. package/dist/createClass-012eebbf.esm.js +109 -0
  4. package/dist/createClass-a436dbfe.cjs.dev.js +116 -0
  5. package/dist/createClass-de7426aa.cjs.prod.js +116 -0
  6. package/dist/declarations/src/Json.d.ts +6 -0
  7. package/dist/declarations/src/expr/eval.d.ts +19 -0
  8. package/dist/declarations/src/expr/expr.d.ts +32 -0
  9. package/dist/declarations/src/expr/index.d.ts +3 -0
  10. package/dist/declarations/src/expr/parser.d.ts +8 -0
  11. package/dist/declarations/src/expr/tokenizer.d.ts +7 -0
  12. package/dist/declarations/src/fetchVal.d.ts +5 -0
  13. package/dist/declarations/src/fp/array.d.ts +8 -0
  14. package/dist/declarations/src/fp/index.d.ts +3 -0
  15. package/dist/declarations/src/fp/result.d.ts +67 -0
  16. package/dist/declarations/src/fp/util.d.ts +12 -0
  17. package/dist/declarations/src/index.d.ts +32 -0
  18. package/dist/declarations/src/initSchema.d.ts +57 -0
  19. package/dist/declarations/src/initVal.d.ts +39 -0
  20. package/dist/declarations/src/module.d.ts +38 -0
  21. package/dist/declarations/src/patch/deref.d.ts +13 -0
  22. package/dist/declarations/src/patch/index.d.ts +6 -0
  23. package/dist/declarations/src/patch/json.d.ts +11 -0
  24. package/dist/declarations/src/patch/operation.d.ts +59 -0
  25. package/dist/declarations/src/patch/ops.d.ts +40 -0
  26. package/dist/declarations/src/patch/parse.d.ts +19 -0
  27. package/dist/declarations/src/patch/patch.d.ts +6 -0
  28. package/dist/declarations/src/patch/util.d.ts +6 -0
  29. package/dist/declarations/src/schema/array.d.ts +18 -0
  30. package/dist/declarations/src/schema/boolean.d.ts +15 -0
  31. package/dist/declarations/src/schema/i18n.d.ts +21 -0
  32. package/dist/declarations/src/schema/image.d.ts +32 -0
  33. package/dist/declarations/src/schema/index.d.ts +23 -0
  34. package/dist/declarations/src/schema/literal.d.ts +17 -0
  35. package/dist/declarations/src/schema/number.d.ts +22 -0
  36. package/dist/declarations/src/schema/object.d.ts +25 -0
  37. package/dist/declarations/src/schema/oneOf.d.ts +22 -0
  38. package/dist/declarations/src/schema/richtext.d.ts +76 -0
  39. package/dist/declarations/src/schema/string.d.ts +22 -0
  40. package/dist/declarations/src/schema/union.d.ts +36 -0
  41. package/dist/declarations/src/selector/SelectorProxy.d.ts +7 -0
  42. package/dist/declarations/src/selector/array.d.ts +17 -0
  43. package/dist/declarations/src/selector/boolean.d.ts +2 -0
  44. package/dist/declarations/src/selector/file.d.ts +9 -0
  45. package/dist/declarations/src/selector/i18n.d.ts +11 -0
  46. package/dist/declarations/src/selector/index.d.ts +81 -0
  47. package/dist/declarations/src/selector/number.d.ts +2 -0
  48. package/dist/declarations/src/selector/object.d.ts +10 -0
  49. package/dist/declarations/src/selector/primitive.d.ts +9 -0
  50. package/dist/declarations/src/selector/remote.d.ts +7 -0
  51. package/dist/declarations/src/selector/string.d.ts +2 -0
  52. package/dist/declarations/src/source/file.d.ts +23 -0
  53. package/dist/declarations/src/source/i18n.d.ts +29 -0
  54. package/dist/declarations/src/source/index.d.ts +33 -0
  55. package/dist/declarations/src/source/remote.d.ts +29 -0
  56. package/dist/declarations/src/val/array.d.ts +9 -0
  57. package/dist/declarations/src/val/index.d.ts +53 -0
  58. package/dist/declarations/src/val/object.d.ts +9 -0
  59. package/dist/declarations/src/val/primitive.d.ts +7 -0
  60. package/dist/index-2f1631cf.cjs.prod.js +1031 -0
  61. package/dist/index-55916dcd.esm.js +1000 -0
  62. package/dist/index-b49a2c60.cjs.dev.js +1031 -0
  63. package/dist/ops-6fae92a1.esm.js +12 -0
  64. package/dist/ops-87cdbafc.cjs.dev.js +14 -0
  65. package/dist/ops-ae4d1bc2.cjs.prod.js +14 -0
  66. package/dist/result-26f67b40.cjs.prod.js +299 -0
  67. package/dist/result-48320acd.cjs.dev.js +299 -0
  68. package/dist/result-b96df128.esm.js +279 -0
  69. package/dist/util-030d8a1f.cjs.prod.js +29 -0
  70. package/dist/util-18613e99.esm.js +26 -0
  71. package/dist/util-b213092b.cjs.dev.js +29 -0
  72. package/dist/valbuild-core.cjs.d.ts +2 -0
  73. package/dist/valbuild-core.cjs.d.ts.map +1 -0
  74. package/dist/valbuild-core.cjs.dev.js +1276 -0
  75. package/dist/valbuild-core.cjs.js +7 -0
  76. package/dist/valbuild-core.cjs.prod.js +1276 -0
  77. package/dist/valbuild-core.esm.js +1265 -0
  78. package/expr/dist/valbuild-core-expr.cjs.d.ts +2 -0
  79. package/expr/dist/valbuild-core-expr.cjs.d.ts.map +1 -0
  80. package/expr/dist/valbuild-core-expr.cjs.dev.js +18 -0
  81. package/expr/dist/valbuild-core-expr.cjs.js +7 -0
  82. package/expr/dist/valbuild-core-expr.cjs.prod.js +18 -0
  83. package/expr/dist/valbuild-core-expr.esm.js +3 -0
  84. package/expr/package.json +4 -0
  85. package/fp/dist/valbuild-core-fp.cjs.d.ts +2 -0
  86. package/fp/dist/valbuild-core-fp.cjs.d.ts.map +1 -0
  87. package/fp/dist/valbuild-core-fp.cjs.dev.js +13 -0
  88. package/fp/dist/valbuild-core-fp.cjs.js +7 -0
  89. package/fp/dist/valbuild-core-fp.cjs.prod.js +13 -0
  90. package/fp/dist/valbuild-core-fp.esm.js +2 -0
  91. package/fp/package.json +4 -0
  92. package/package.json +43 -0
  93. package/patch/dist/valbuild-core-patch.cjs.d.ts +2 -0
  94. package/patch/dist/valbuild-core-patch.cjs.d.ts.map +1 -0
  95. package/patch/dist/valbuild-core-patch.cjs.dev.js +443 -0
  96. package/patch/dist/valbuild-core-patch.cjs.js +7 -0
  97. package/patch/dist/valbuild-core-patch.cjs.prod.js +443 -0
  98. package/patch/dist/valbuild-core-patch.esm.js +431 -0
  99. package/patch/package.json +4 -0
  100. package/tsconfig.json +7 -0
@@ -0,0 +1,431 @@
1
+ import { b as _typeof, c as _slicedToArray, a as _createClass, _ as _classCallCheck, d as _toConsumableArray } from '../../dist/createClass-012eebbf.esm.js';
2
+ import { f as isNonEmpty, e as err, o as ok, m as map, g as flatMap, i as isErr, h as flatMapReduce, j as filterOrElse, k as mapErr, l as map$1, n as all, p as flatten, q as allT } from '../../dist/result-b96df128.esm.js';
3
+ import { p as pipe } from '../../dist/util-18613e99.esm.js';
4
+ import { P as PatchError } from '../../dist/ops-6fae92a1.esm.js';
5
+ export { P as PatchError } from '../../dist/ops-6fae92a1.esm.js';
6
+
7
+ function isNotRoot(path) {
8
+ return isNonEmpty(path);
9
+ }
10
+ function deepEqual(a, b) {
11
+ if (a === b) {
12
+ return true;
13
+ }
14
+ if (_typeof(a) === "object" && _typeof(b) === "object" && a !== null && b !== null) {
15
+ if (Array.isArray(a) && Array.isArray(b)) {
16
+ if (a.length !== b.length) return false;
17
+ for (var i = 0; i < a.length; ++i) {
18
+ if (!deepEqual(a[i], b[i])) return false;
19
+ }
20
+ return true;
21
+ } else if (!Array.isArray(a) && !Array.isArray(b)) {
22
+ var aEntries = Object.entries(a);
23
+ // If the objects have a different amount of keys, they cannot be equal
24
+ if (aEntries.length !== Object.keys(b).length) return false;
25
+ for (var _i = 0, _aEntries = aEntries; _i < _aEntries.length; _i++) {
26
+ var _aEntries$_i = _slicedToArray(_aEntries[_i], 2),
27
+ key = _aEntries$_i[0],
28
+ aValue = _aEntries$_i[1];
29
+ // b must be a JSON object, so the only way for the bValue to be
30
+ // undefined is if the key is unset
31
+ var bValue = b[key];
32
+ if (bValue === undefined) return false;
33
+ if (!deepEqual(aValue, bValue)) return false;
34
+ }
35
+ return true;
36
+ }
37
+ }
38
+ return false;
39
+ }
40
+ function deepClone(value) {
41
+ if (Array.isArray(value)) {
42
+ return value.map(deepClone);
43
+ } else if (_typeof(value) === "object" && value !== null) {
44
+ return Object.fromEntries(Object.entries(value).map(function (_ref) {
45
+ var _ref2 = _slicedToArray(_ref, 2),
46
+ key = _ref2[0],
47
+ value = _ref2[1];
48
+ return [key, deepClone(value)];
49
+ }));
50
+ } else {
51
+ return value;
52
+ }
53
+ }
54
+ function parseAndValidateArrayIndex(value) {
55
+ if (!/^(0|[1-9][0-9]*)$/g.test(value)) {
56
+ return err(new PatchError("Invalid array index \"".concat(value, "\"")));
57
+ }
58
+ return ok(Number(value));
59
+ }
60
+
61
+ function parseAndValidateArrayInsertIndex(key, nodes) {
62
+ if (key === "-") {
63
+ return ok(nodes.length);
64
+ }
65
+ return pipe(parseAndValidateArrayIndex(key), filterOrElse(function (index) {
66
+ return index <= nodes.length;
67
+ }, function () {
68
+ return new PatchError("Array index out of bounds");
69
+ }));
70
+ }
71
+ function parseAndValidateArrayInboundsIndex(key, nodes) {
72
+ return pipe(parseAndValidateArrayIndex(key), filterOrElse(function (index) {
73
+ return index < nodes.length;
74
+ }, function () {
75
+ return new PatchError("Array index out of bounds");
76
+ }));
77
+ }
78
+ function replaceInNode(node, key, value) {
79
+ if (Array.isArray(node)) {
80
+ return pipe(parseAndValidateArrayInboundsIndex(key, node), map(function (index) {
81
+ var replaced = node[index];
82
+ node[index] = value;
83
+ return replaced;
84
+ }));
85
+ } else if (_typeof(node) === "object" && node !== null) {
86
+ // Prototype pollution protection
87
+ if (Object.prototype.hasOwnProperty.call(node, key)) {
88
+ var _replaced = node[key];
89
+ node[key] = value;
90
+ return ok(_replaced);
91
+ } else {
92
+ return err(new PatchError("Cannot replace object element which does not exist"));
93
+ }
94
+ }
95
+ return err(new PatchError("Cannot replace in non-object/array"));
96
+ }
97
+ function replaceAtPath(document, path, value) {
98
+ if (isNotRoot(path)) {
99
+ return pipe(getPointerFromPath(document, path), flatMap(function (_ref) {
100
+ var _ref2 = _slicedToArray(_ref, 2),
101
+ node = _ref2[0],
102
+ key = _ref2[1];
103
+ return replaceInNode(node, key, value);
104
+ }), map(function (replaced) {
105
+ return [document, replaced];
106
+ }));
107
+ } else {
108
+ return ok([value, document]);
109
+ }
110
+ }
111
+ function getFromNode(node, key) {
112
+ if (Array.isArray(node)) {
113
+ return pipe(parseAndValidateArrayIndex(key), flatMap(function (index) {
114
+ if (index >= node.length) {
115
+ return err(new PatchError("Array index out of bounds"));
116
+ } else {
117
+ return ok(node[index]);
118
+ }
119
+ }));
120
+ } else if (_typeof(node) === "object" && node !== null) {
121
+ // Prototype pollution protection
122
+ if (Object.prototype.hasOwnProperty.call(node, key)) {
123
+ return ok(node[key]);
124
+ } else {
125
+ return ok(undefined);
126
+ }
127
+ }
128
+ return err(new PatchError("Cannot access non-object/array"));
129
+ }
130
+ function getPointerFromPath(node, path) {
131
+ var targetNode = node;
132
+ var key = path[0];
133
+ for (var i = 0; i < path.length - 1; ++i, key = path[i]) {
134
+ var childNode = getFromNode(targetNode, key);
135
+ if (isErr(childNode)) {
136
+ return childNode;
137
+ }
138
+ if (childNode.value === undefined) {
139
+ return err(new PatchError("Path refers to non-existing object/array"));
140
+ }
141
+ targetNode = childNode.value;
142
+ }
143
+ return ok([targetNode, key]);
144
+ }
145
+ function getAtPath(node, path) {
146
+ return pipe(path, flatMapReduce(function (node, key) {
147
+ return pipe(getFromNode(node, key), filterOrElse(function (childNode) {
148
+ return childNode !== undefined;
149
+ }, function () {
150
+ return new PatchError("Path refers to non-existing object/array");
151
+ }));
152
+ }, node));
153
+ }
154
+ function removeFromNode(node, key) {
155
+ if (Array.isArray(node)) {
156
+ return pipe(parseAndValidateArrayInboundsIndex(key, node), map(function (index) {
157
+ var _node$splice = node.splice(index, 1),
158
+ _node$splice2 = _slicedToArray(_node$splice, 1),
159
+ removed = _node$splice2[0];
160
+ return removed;
161
+ }));
162
+ } else if (_typeof(node) === "object" && node !== null) {
163
+ // Prototype pollution protection
164
+ if (Object.prototype.hasOwnProperty.call(node, key)) {
165
+ var removed = node[key];
166
+ delete node[key];
167
+ return ok(removed);
168
+ }
169
+ }
170
+ return err(new PatchError("Cannot remove from non-object/array"));
171
+ }
172
+ function removeAtPath(document, path) {
173
+ return pipe(getPointerFromPath(document, path), flatMap(function (_ref3) {
174
+ var _ref4 = _slicedToArray(_ref3, 2),
175
+ node = _ref4[0],
176
+ key = _ref4[1];
177
+ return removeFromNode(node, key);
178
+ }));
179
+ }
180
+ function addToNode(node, key, value) {
181
+ if (Array.isArray(node)) {
182
+ return pipe(parseAndValidateArrayInsertIndex(key, node), map(function (index) {
183
+ node.splice(index, 0, value);
184
+ return undefined;
185
+ }));
186
+ } else if (_typeof(node) === "object" && node !== null) {
187
+ var _replaced2;
188
+ // Prototype pollution protection
189
+ if (Object.prototype.hasOwnProperty.call(node, key)) {
190
+ _replaced2 = node[key];
191
+ }
192
+ node[key] = value;
193
+ return ok(_replaced2);
194
+ }
195
+ return err(new PatchError("Cannot add to non-object/array"));
196
+ }
197
+ function addAtPath(document, path, value) {
198
+ if (isNotRoot(path)) {
199
+ return pipe(getPointerFromPath(document, path), flatMap(function (_ref5) {
200
+ var _ref6 = _slicedToArray(_ref5, 2),
201
+ node = _ref6[0],
202
+ key = _ref6[1];
203
+ return addToNode(node, key, value);
204
+ }), map(function (replaced) {
205
+ return [document, replaced];
206
+ }));
207
+ } else {
208
+ return ok([value, document]);
209
+ }
210
+ }
211
+ function pickDocument(_ref7) {
212
+ var _ref8 = _slicedToArray(_ref7, 1),
213
+ document = _ref8[0];
214
+ return document;
215
+ }
216
+ var JSONOps = /*#__PURE__*/function () {
217
+ function JSONOps() {
218
+ _classCallCheck(this, JSONOps);
219
+ }
220
+ _createClass(JSONOps, [{
221
+ key: "get",
222
+ value: function get(document, path) {
223
+ return getAtPath(document, path);
224
+ }
225
+ }, {
226
+ key: "add",
227
+ value: function add(document, path, value) {
228
+ return pipe(addAtPath(document, path, value), map(pickDocument));
229
+ }
230
+ }, {
231
+ key: "remove",
232
+ value: function remove(document, path) {
233
+ return pipe(removeAtPath(document, path), map(function () {
234
+ return document;
235
+ }));
236
+ }
237
+ }, {
238
+ key: "replace",
239
+ value: function replace(document, path, value) {
240
+ return pipe(replaceAtPath(document, path, value), map(pickDocument));
241
+ }
242
+ }, {
243
+ key: "move",
244
+ value: function move(document, from, path) {
245
+ return pipe(removeAtPath(document, from), flatMap(function (removed) {
246
+ return addAtPath(document, path, removed);
247
+ }), map(pickDocument));
248
+ }
249
+ }, {
250
+ key: "copy",
251
+ value: function copy(document, from, path) {
252
+ return pipe(getAtPath(document, from), flatMap(function (value) {
253
+ return addAtPath(document, path, deepClone(value));
254
+ }), map(pickDocument));
255
+ }
256
+ }, {
257
+ key: "test",
258
+ value: function test(document, path, value) {
259
+ return pipe(getAtPath(document, path), map(function (documentValue) {
260
+ return deepEqual(value, documentValue);
261
+ }));
262
+ }
263
+ }]);
264
+ return JSONOps;
265
+ }();
266
+
267
+ function parseJSONPointerReferenceToken(value) {
268
+ if (value.endsWith("~")) {
269
+ return undefined;
270
+ }
271
+ try {
272
+ return value.replace(/~./, function (escaped) {
273
+ switch (escaped) {
274
+ case "~0":
275
+ return "~";
276
+ case "~1":
277
+ return "/";
278
+ }
279
+ throw new Error();
280
+ });
281
+ } catch (e) {
282
+ return undefined;
283
+ }
284
+ }
285
+ function parseJSONPointer(pointer) {
286
+ if (pointer === "/") return ok([]);
287
+ if (!pointer.startsWith("/")) return err("JSON pointer must start with /");
288
+ var tokens = pointer.substring(1).split("/").map(parseJSONPointerReferenceToken);
289
+ if (tokens.every(function (token) {
290
+ return token !== undefined;
291
+ })) {
292
+ return ok(tokens);
293
+ } else {
294
+ return err("Invalid JSON pointer escape sequence");
295
+ }
296
+ }
297
+ function formatJSONPointerReferenceToken(key) {
298
+ return key.replace(/~/g, "~0").replace(/\//g, "~1");
299
+ }
300
+ function formatJSONPointer(path) {
301
+ return "/".concat(path.map(formatJSONPointerReferenceToken).join("/"));
302
+ }
303
+
304
+ /**
305
+ * A signifies an issue that makes a PatchJSON or an OperationJSON invalid.
306
+ * Unlike PatchError, a StaticPatchIssue indicates an issue with the patch
307
+ * document itself; it is independent of any document which the patch or
308
+ * might be applied to.
309
+ */
310
+
311
+ function prefixIssuePath(prefix, _ref) {
312
+ var path = _ref.path,
313
+ message = _ref.message;
314
+ return {
315
+ path: [prefix].concat(_toConsumableArray(path)),
316
+ message: message
317
+ };
318
+ }
319
+ function createIssueAtPath(path) {
320
+ return function (message) {
321
+ return {
322
+ path: path,
323
+ message: message
324
+ };
325
+ };
326
+ }
327
+ function isProperPathPrefix(prefix, path) {
328
+ if (prefix.length >= path.length) {
329
+ // A proper prefix cannot be longer or have the same length as the path
330
+ return false;
331
+ }
332
+ for (var i = 0; i < prefix.length; ++i) {
333
+ if (prefix[i] !== path[i]) {
334
+ return false;
335
+ }
336
+ }
337
+ return true;
338
+ }
339
+ function parseOperation(operation) {
340
+ var path = parseJSONPointer(operation.path);
341
+ switch (operation.op) {
342
+ case "add":
343
+ case "replace":
344
+ case "test":
345
+ return pipe(path, mapErr(function (error) {
346
+ return [createIssueAtPath(["path"])(error)];
347
+ }), map(function (path) {
348
+ return {
349
+ op: operation.op,
350
+ path: path,
351
+ value: operation.value
352
+ };
353
+ }));
354
+ case "remove":
355
+ return pipe(path, filterOrElse(isNonEmpty, function () {
356
+ return "Cannot remove root";
357
+ }), mapErr(function (error) {
358
+ return [createIssueAtPath(["path"])(error)];
359
+ }), map(function (path) {
360
+ return {
361
+ op: operation.op,
362
+ path: path
363
+ };
364
+ }));
365
+ case "move":
366
+ return pipe(allT([pipe(parseJSONPointer(operation.from), filterOrElse(isNonEmpty, function () {
367
+ return "Cannot move root";
368
+ }), mapErr(createIssueAtPath(["from"]))), pipe(path, mapErr(createIssueAtPath(["path"])))]), filterOrElse(function (_ref2) {
369
+ var _ref3 = _slicedToArray(_ref2, 2),
370
+ from = _ref3[0],
371
+ path = _ref3[1];
372
+ return !isProperPathPrefix(from, path);
373
+ }, function () {
374
+ return [createIssueAtPath(["from"])("Cannot be a proper prefix of path")];
375
+ }), map(function (_ref4) {
376
+ var _ref5 = _slicedToArray(_ref4, 2),
377
+ from = _ref5[0],
378
+ path = _ref5[1];
379
+ return {
380
+ op: operation.op,
381
+ from: from,
382
+ path: path
383
+ };
384
+ }));
385
+ case "copy":
386
+ return pipe(allT([pipe(parseJSONPointer(operation.from), mapErr(createIssueAtPath(["from"]))), pipe(path, mapErr(createIssueAtPath(["path"])))]), map(function (_ref6) {
387
+ var _ref7 = _slicedToArray(_ref6, 2),
388
+ from = _ref7[0],
389
+ path = _ref7[1];
390
+ return {
391
+ op: operation.op,
392
+ from: from,
393
+ path: path
394
+ };
395
+ }));
396
+ }
397
+ }
398
+ function parsePatch(patch) {
399
+ return pipe(patch.map(parseOperation).map(mapErr(map$1(function (error, index) {
400
+ return prefixIssuePath(index.toString(), error);
401
+ }))), all, mapErr(flatten));
402
+ }
403
+
404
+ function apply(document, ops, op) {
405
+ switch (op.op) {
406
+ case "add":
407
+ return ops.add(document, op.path, op.value);
408
+ case "remove":
409
+ return ops.remove(document, op.path);
410
+ case "replace":
411
+ return ops.replace(document, op.path, op.value);
412
+ case "move":
413
+ return ops.move(document, op.from, op.path);
414
+ case "copy":
415
+ return ops.copy(document, op.from, op.path);
416
+ case "test":
417
+ {
418
+ if (!ops.test(document, op.path, op.value)) {
419
+ return err(new PatchError("Test failed"));
420
+ }
421
+ return ok(document);
422
+ }
423
+ }
424
+ }
425
+ function applyPatch(document, ops, patch) {
426
+ return pipe(patch, flatMapReduce(function (doc, op) {
427
+ return apply(doc, ops, op);
428
+ }, document));
429
+ }
430
+
431
+ export { JSONOps, applyPatch, deepClone, deepEqual, formatJSONPointer, isNotRoot, parseAndValidateArrayIndex, parseJSONPointer, parsePatch };
@@ -0,0 +1,4 @@
1
+ {
2
+ "main": "dist/valbuild-core-patch.cjs.js",
3
+ "module": "dist/valbuild-core-patch.esm.js"
4
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "compilerOptions": {
3
+ "strict": true,
4
+ "isolatedModules": true,
5
+ "noEmit": true
6
+ }
7
+ }