@orioro/util 0.0.0 → 0.2.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 (165) hide show
  1. package/README.md +1 -0
  2. package/babel.config.cjs +13 -0
  3. package/coverage/clover.xml +488 -45
  4. package/coverage/coverage-final.json +28 -2
  5. package/coverage/lcov-report/ValidationError.ts.html +184 -0
  6. package/coverage/lcov-report/base.css +19 -7
  7. package/coverage/lcov-report/block-navigation.js +87 -0
  8. package/coverage/lcov-report/favicon.png +0 -0
  9. package/coverage/lcov-report/index.html +248 -58
  10. package/coverage/lcov-report/prettify.js +1 -0
  11. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  12. package/coverage/lcov-report/sorter.js +52 -14
  13. package/coverage/lcov-report/src/debug/deepFreeze.ts.html +157 -0
  14. package/coverage/lcov-report/src/debug/index.html +146 -0
  15. package/coverage/lcov-report/src/debug/index.ts.html +91 -0
  16. package/coverage/lcov-report/src/debug/wait.ts.html +127 -0
  17. package/coverage/lcov-report/src/index.html +131 -0
  18. package/coverage/lcov-report/src/interpolate/index.html +116 -0
  19. package/coverage/lcov-report/src/interpolate/index.ts.html +277 -0
  20. package/coverage/lcov-report/src/maybeFn.ts.html +94 -0
  21. package/coverage/lcov-report/src/promise/index.html +146 -0
  22. package/coverage/lcov-report/src/promise/index.ts.html +91 -0
  23. package/coverage/lcov-report/src/promise/promiseReduce.ts.html +130 -0
  24. package/coverage/lcov-report/src/promise/resolveNestedPromises.ts.html +271 -0
  25. package/coverage/lcov-report/src/switchValue.ts.html +253 -0
  26. package/coverage/lcov-report/src/typeOf.ts.html +328 -0
  27. package/coverage/lcov-report/src/validate/ValidationError.ts.html +184 -0
  28. package/coverage/lcov-report/src/validate/async/index.html +131 -0
  29. package/coverage/lcov-report/src/validate/async/index.ts.html +241 -0
  30. package/coverage/lcov-report/src/validate/async/parseValidator.ts.html +136 -0
  31. package/coverage/lcov-report/src/validate/async/validateAsyncFn.ts.html +208 -0
  32. package/coverage/lcov-report/src/validate/async/validators/and.ts.html +154 -0
  33. package/coverage/lcov-report/src/validate/async/validators/index.html +146 -0
  34. package/coverage/lcov-report/src/validate/async/validators/index.ts.html +91 -0
  35. package/coverage/lcov-report/src/validate/async/validators/logical.ts.html +253 -0
  36. package/coverage/lcov-report/src/validate/async/validators/or.ts.html +151 -0
  37. package/coverage/lcov-report/src/validate/async/validators/shape.ts.html +565 -0
  38. package/coverage/lcov-report/src/validate/common/ValidationError.ts.html +184 -0
  39. package/coverage/lcov-report/src/validate/common/index.html +116 -0
  40. package/coverage/lcov-report/src/validate/common/util/defaultErrorMessage.ts.html +163 -0
  41. package/coverage/lcov-report/src/validate/common/util/index.html +161 -0
  42. package/coverage/lcov-report/src/validate/common/util/index.ts.html +94 -0
  43. package/coverage/lcov-report/src/validate/common/util/parseValidator.ts.html +316 -0
  44. package/coverage/lcov-report/src/validate/common/util/parseValidatorInput.ts.html +316 -0
  45. package/coverage/lcov-report/src/validate/common/util/resolveValidationResult.ts.html +277 -0
  46. package/coverage/lcov-report/src/validate/common/util/validatorParser.ts.html +316 -0
  47. package/coverage/lcov-report/src/validate/common/validators/index.html +131 -0
  48. package/coverage/lcov-report/src/validate/common/validators/index.ts.html +88 -0
  49. package/coverage/lcov-report/src/validate/common/validators/type.ts.html +388 -0
  50. package/coverage/lcov-report/src/validate/fmtValidationResult.ts.html +268 -0
  51. package/coverage/lcov-report/src/validate/index.html +116 -0
  52. package/coverage/lcov-report/src/validate/index.ts.html +94 -0
  53. package/coverage/lcov-report/src/validate/makeValidate.ts.html +634 -0
  54. package/coverage/lcov-report/src/validate/specUtil/commonTests.js.html +1324 -0
  55. package/coverage/lcov-report/src/validate/specUtil/index.html +116 -0
  56. package/coverage/lcov-report/src/validate/sync/index.html +131 -0
  57. package/coverage/lcov-report/src/validate/sync/index.ts.html +244 -0
  58. package/coverage/lcov-report/src/validate/sync/parseValidator.ts.html +136 -0
  59. package/coverage/lcov-report/src/validate/sync/validateSyncFn.ts.html +223 -0
  60. package/coverage/lcov-report/src/validate/sync/validators/and.ts.html +148 -0
  61. package/coverage/lcov-report/src/validate/sync/validators/index.html +146 -0
  62. package/coverage/lcov-report/src/validate/sync/validators/index.ts.html +91 -0
  63. package/coverage/lcov-report/src/validate/sync/validators/logical.ts.html +226 -0
  64. package/coverage/lcov-report/src/validate/sync/validators/or.ts.html +130 -0
  65. package/coverage/lcov-report/src/validate/sync/validators/shape.ts.html +523 -0
  66. package/coverage/lcov-report/src/validate/sync/validators/type.ts.html +154 -0
  67. package/coverage/lcov-report/src/validate/syncValidators/and.ts.html +157 -0
  68. package/coverage/lcov-report/src/validate/syncValidators/index.html +176 -0
  69. package/coverage/lcov-report/src/validate/syncValidators/index.ts.html +97 -0
  70. package/coverage/lcov-report/src/validate/syncValidators/or.ts.html +127 -0
  71. package/coverage/lcov-report/src/validate/syncValidators/shape.ts.html +559 -0
  72. package/coverage/lcov-report/src/validate/syncValidators/string.ts.html +163 -0
  73. package/coverage/lcov-report/src/validate/syncValidators/type.ts.html +154 -0
  74. package/coverage/lcov-report/src/validate/util/defaultErrorMessage.ts.html +169 -0
  75. package/coverage/lcov-report/src/validate/util/index.html +146 -0
  76. package/coverage/lcov-report/src/validate/util/index.ts.html +91 -0
  77. package/coverage/lcov-report/src/validate/util/resolveValidationResult.ts.html +253 -0
  78. package/coverage/lcov-report/src/validate/validate.ts.html +220 -0
  79. package/coverage/lcov-report/src/validate/validateAsync.ts.html +220 -0
  80. package/coverage/lcov-report/src/validate/validators/and.ts.html +157 -0
  81. package/coverage/lcov-report/src/validate/validators/index.html +176 -0
  82. package/coverage/lcov-report/src/validate/validators/index.ts.html +97 -0
  83. package/coverage/lcov-report/src/validate/validators/or.ts.html +127 -0
  84. package/coverage/lcov-report/src/validate/validators/shape.ts.html +541 -0
  85. package/coverage/lcov-report/src/validate/validators/type.ts.html +154 -0
  86. package/coverage/lcov-report/src/validate_/ValidationError.ts.html +184 -0
  87. package/coverage/lcov-report/src/validate_/fmtValidationResult.ts.html +268 -0
  88. package/coverage/lcov-report/src/validate_/index.html +161 -0
  89. package/coverage/lcov-report/src/validate_/makeValidate.ts.html +634 -0
  90. package/coverage/lcov-report/src/validate_/validate.ts.html +220 -0
  91. package/coverage/lcov-report/switchValue.ts.html +253 -0
  92. package/coverage/lcov-report/typeOf.ts.html +331 -0
  93. package/coverage/lcov-report/validate.ts.html +757 -0
  94. package/coverage/lcov.info +1045 -74
  95. package/dist/index.mjs +1437 -73
  96. package/jest.config.js +6 -0
  97. package/package.json +27 -27
  98. package/rollup.config.mjs +6 -0
  99. package/src/PromiseLikeEventEmitter/index.ts +35 -0
  100. package/src/array/arrayChunk.ts +7 -0
  101. package/src/array/index.ts +1 -0
  102. package/src/debug/debugFn/index.ts +48 -0
  103. package/src/debug/debugFn/util.ts +27 -0
  104. package/src/debug/deepFreeze.ts +26 -0
  105. package/src/debug/index.ts +3 -0
  106. package/src/debug/wait.ts +14 -0
  107. package/src/index.ts +9 -0
  108. package/src/interpolate/index.spec.ts +20 -0
  109. package/src/interpolate/index.ts +64 -0
  110. package/src/maybeFn.ts +3 -0
  111. package/src/promise/batchFn.spec.ts +92 -0
  112. package/src/promise/batchFn.ts +176 -0
  113. package/src/promise/index.ts +3 -0
  114. package/src/promise/promiseReduce.ts +15 -0
  115. package/src/promise/resolveNestedPromises.spec.ts +205 -0
  116. package/src/promise/resolveNestedPromises.ts +83 -0
  117. package/src/promise/types.ts +2 -0
  118. package/src/resolvePaths/index.spec.ts +42 -0
  119. package/src/resolvePaths/index.ts +21 -0
  120. package/src/switchValue.spec.ts +30 -0
  121. package/src/switchValue.ts +59 -0
  122. package/src/typeOf.spec.ts +47 -0
  123. package/src/typeOf.ts +81 -0
  124. package/src/validate/__snapshots__/index.spec.ts.snap +9 -0
  125. package/src/validate/async/index.spec.ts +236 -0
  126. package/src/validate/async/index.ts +52 -0
  127. package/src/validate/async/validateAsyncFn.ts +41 -0
  128. package/src/validate/async/validators/index.ts +2 -0
  129. package/src/validate/async/validators/logical.ts +56 -0
  130. package/src/validate/async/validators/shape.ts +160 -0
  131. package/src/validate/async/validators/tmpand.ts +24 -0
  132. package/src/validate/async/validators/tmpor.ts +21 -0
  133. package/src/validate/common/ValidationError.ts +33 -0
  134. package/src/validate/common/util/defaultErrorMessage.ts +26 -0
  135. package/src/validate/common/util/index.ts +3 -0
  136. package/src/validate/common/util/parseValidatorInput.ts +77 -0
  137. package/src/validate/common/util/resolveValidationResult.ts +64 -0
  138. package/src/validate/common/validators/index.ts +1 -0
  139. package/src/validate/common/validators/type.ts +101 -0
  140. package/src/validate/index.spec.ts +5 -0
  141. package/src/validate/index.ts +3 -0
  142. package/src/validate/specUtil/commonTests.js +413 -0
  143. package/src/validate/sync/index.spec.ts +81 -0
  144. package/src/validate/sync/index.ts +53 -0
  145. package/src/validate/sync/validateSyncFn.ts +46 -0
  146. package/src/validate/sync/validators/index.ts +2 -0
  147. package/src/validate/sync/validators/logical.ts +47 -0
  148. package/src/validate/sync/validators/shape.ts +146 -0
  149. package/src/validate/types/async.ts +20 -0
  150. package/src/validate/types/common.ts +70 -0
  151. package/src/validate/types/index.ts +3 -0
  152. package/src/validate/types/sync.ts +20 -0
  153. package/tsconfig.json +11 -0
  154. package/array/index.js +0 -11
  155. package/coverage/lcov-report/array/index.html +0 -93
  156. package/coverage/lcov-report/array/index.js.html +0 -98
  157. package/coverage/lcov-report/coverage/coverage-final.json.html +0 -95
  158. package/coverage/lcov-report/coverage/index.html +0 -93
  159. package/coverage/lcov-report/coverage/lcov-report/index.html +0 -106
  160. package/coverage/lcov-report/coverage/lcov-report/prettify.js.html +0 -68
  161. package/coverage/lcov-report/coverage/lcov-report/sorter.js.html +0 -539
  162. package/coverage/lcov-report/fn/index.html +0 -93
  163. package/coverage/lcov-report/fn/index.js.html +0 -215
  164. package/dist/index.js +0 -116
  165. package/fn/index.js +0 -50
package/dist/index.mjs CHANGED
@@ -1,111 +1,1475 @@
1
- function _typeof(obj) {
2
- if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
3
- _typeof = function (obj) {
4
- return typeof obj;
1
+ import EventEmitter from 'eventemitter3';
2
+ import copy from 'fast-copy';
3
+ import { getProperty, setProperty } from 'dot-prop';
4
+ import traverse from 'traverse';
5
+
6
+ function _typeof(o) {
7
+ "@babel/helpers - typeof";
8
+
9
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
10
+ return typeof o;
11
+ } : function (o) {
12
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
13
+ }, _typeof(o);
14
+ }
15
+
16
+ /******************************************************************************
17
+ Copyright (c) Microsoft Corporation.
18
+
19
+ Permission to use, copy, modify, and/or distribute this software for any
20
+ purpose with or without fee is hereby granted.
21
+
22
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
23
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
25
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
26
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
27
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28
+ PERFORMANCE OF THIS SOFTWARE.
29
+ ***************************************************************************** */
30
+ /* global Reflect, Promise, SuppressedError, Symbol */
31
+
32
+ var _extendStatics = function extendStatics(d, b) {
33
+ _extendStatics = Object.setPrototypeOf || {
34
+ __proto__: []
35
+ } instanceof Array && function (d, b) {
36
+ d.__proto__ = b;
37
+ } || function (d, b) {
38
+ for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
39
+ };
40
+ return _extendStatics(d, b);
41
+ };
42
+ function __extends(d, b) {
43
+ if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
44
+ _extendStatics(d, b);
45
+ function __() {
46
+ this.constructor = d;
47
+ }
48
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
49
+ }
50
+ var _assign = function __assign() {
51
+ _assign = Object.assign || function __assign(t) {
52
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
53
+ s = arguments[i];
54
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
55
+ }
56
+ return t;
57
+ };
58
+ return _assign.apply(this, arguments);
59
+ };
60
+ function __rest(s, e) {
61
+ var t = {};
62
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
63
+ if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
64
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
65
+ }
66
+ return t;
67
+ }
68
+ function __awaiter(thisArg, _arguments, P, generator) {
69
+ function adopt(value) {
70
+ return value instanceof P ? value : new P(function (resolve) {
71
+ resolve(value);
72
+ });
73
+ }
74
+ return new (P || (P = Promise))(function (resolve, reject) {
75
+ function fulfilled(value) {
76
+ try {
77
+ step(generator.next(value));
78
+ } catch (e) {
79
+ reject(e);
80
+ }
81
+ }
82
+ function rejected(value) {
83
+ try {
84
+ step(generator["throw"](value));
85
+ } catch (e) {
86
+ reject(e);
87
+ }
88
+ }
89
+ function step(result) {
90
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
91
+ }
92
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
93
+ });
94
+ }
95
+ function __generator(thisArg, body) {
96
+ var _ = {
97
+ label: 0,
98
+ sent: function sent() {
99
+ if (t[0] & 1) throw t[1];
100
+ return t[1];
101
+ },
102
+ trys: [],
103
+ ops: []
104
+ },
105
+ f,
106
+ y,
107
+ t,
108
+ g;
109
+ return g = {
110
+ next: verb(0),
111
+ "throw": verb(1),
112
+ "return": verb(2)
113
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
114
+ return this;
115
+ }), g;
116
+ function verb(n) {
117
+ return function (v) {
118
+ return step([n, v]);
5
119
  };
6
- } else {
7
- _typeof = function (obj) {
8
- return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
120
+ }
121
+ function step(op) {
122
+ if (f) throw new TypeError("Generator is already executing.");
123
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
124
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
125
+ if (y = 0, t) op = [op[0] & 2, t.value];
126
+ switch (op[0]) {
127
+ case 0:
128
+ case 1:
129
+ t = op;
130
+ break;
131
+ case 4:
132
+ _.label++;
133
+ return {
134
+ value: op[1],
135
+ done: false
136
+ };
137
+ case 5:
138
+ _.label++;
139
+ y = op[1];
140
+ op = [0];
141
+ continue;
142
+ case 7:
143
+ op = _.ops.pop();
144
+ _.trys.pop();
145
+ continue;
146
+ default:
147
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
148
+ _ = 0;
149
+ continue;
150
+ }
151
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
152
+ _.label = op[1];
153
+ break;
154
+ }
155
+ if (op[0] === 6 && _.label < t[1]) {
156
+ _.label = t[1];
157
+ t = op;
158
+ break;
159
+ }
160
+ if (t && _.label < t[2]) {
161
+ _.label = t[2];
162
+ _.ops.push(op);
163
+ break;
164
+ }
165
+ if (t[2]) _.ops.pop();
166
+ _.trys.pop();
167
+ continue;
168
+ }
169
+ op = body.call(thisArg, _);
170
+ } catch (e) {
171
+ op = [6, e];
172
+ y = 0;
173
+ } finally {
174
+ f = t = 0;
175
+ }
176
+ if (op[0] & 5) throw op[1];
177
+ return {
178
+ value: op[0] ? op[1] : void 0,
179
+ done: true
9
180
  };
10
181
  }
182
+ }
183
+ function __spreadArray(to, from, pack) {
184
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
185
+ if (ar || !(i in from)) {
186
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
187
+ ar[i] = from[i];
188
+ }
189
+ }
190
+ return to.concat(ar || Array.prototype.slice.call(from));
191
+ }
192
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
193
+ var e = new Error(message);
194
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
195
+ };
11
196
 
12
- return _typeof(obj);
197
+ var PromiseLikeEventEmitter = /** @class */function (_super) {
198
+ __extends(PromiseLikeEventEmitter, _super);
199
+ function PromiseLikeEventEmitter() {
200
+ var _this = _super.call(this) || this;
201
+ _this.promise = new Promise(function (resolve, reject) {
202
+ _this.resolve = resolve;
203
+ _this.reject = reject;
204
+ });
205
+ return _this;
206
+ }
207
+ PromiseLikeEventEmitter.prototype.then = function (onFulfilled, onRejected) {
208
+ return this.promise.then(onFulfilled, onRejected);
209
+ };
210
+ PromiseLikeEventEmitter.prototype["catch"] = function (onRejected) {
211
+ return this.promise["catch"](onRejected);
212
+ };
213
+ PromiseLikeEventEmitter.prototype["finally"] = function (onFinally) {
214
+ return this.promise["finally"](onFinally);
215
+ };
216
+ return PromiseLikeEventEmitter;
217
+ }(EventEmitter);
218
+
219
+ function wait(ms, result) {
220
+ if (ms === void 0) {
221
+ ms = 1000;
222
+ }
223
+ return __awaiter(this, void 0, void 0, function () {
224
+ return __generator(this, function (_a) {
225
+ return [2 /*return*/, new Promise(function (resolve, reject) {
226
+ setTimeout(function () {
227
+ if (result instanceof Error) {
228
+ reject(result);
229
+ } else {
230
+ resolve(result);
231
+ }
232
+ }, ms);
233
+ })];
234
+ });
235
+ });
13
236
  }
14
237
 
15
- function _toConsumableArray(arr) {
16
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
238
+ function deepFreeze(object) {
239
+ Object.freeze(object);
240
+ if (Array.isArray(object)) {
241
+ // Handle the array case
242
+ object.forEach(function (item) {
243
+ if (item && _typeof(item) === 'object' && !Object.isFrozen(item)) {
244
+ deepFreeze(item);
245
+ }
246
+ });
247
+ } else {
248
+ // Handle the object case
249
+ Object.keys(object).forEach(function (key) {
250
+ var value = object[key];
251
+ if (value && _typeof(value) === 'object' && !Object.isFrozen(value)) {
252
+ deepFreeze(value);
253
+ }
254
+ });
255
+ }
256
+ return object;
17
257
  }
18
258
 
19
- function _arrayWithoutHoles(arr) {
20
- if (Array.isArray(arr)) {
21
- for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
259
+ function stringifyReplacer(key, value) {
260
+ if (typeof value === 'function') {
261
+ // Convert functions to a consistent string representation
262
+ return "function:".concat(value.toString());
263
+ }
264
+ return value;
265
+ }
266
+ function simpleHash(str) {
267
+ var hash = 0;
268
+ for (var i = 0; i < str.length; i++) {
269
+ var _char = str.charCodeAt(i);
270
+ hash = (hash << 5) - hash + _char;
271
+ hash |= 0; // Convert to 32bit integer
272
+ }
273
+ // Ensure the hash is positive to avoid '-' signals
274
+ hash = Math.abs(hash);
275
+ return hash.toString(36); // Convert to base36 to ensure the result is alphanumeric
276
+ }
277
+ function generateDeterministicId(args) {
278
+ // Convert arguments to a JSON string using the custom replacer
279
+ var argsString = JSON.stringify(args, stringifyReplacer);
280
+ // Generate a simple hash of the string
281
+ return simpleHash(argsString);
282
+ }
283
+
284
+ function debugFn(fnName, fn, logCall, logResult) {
285
+ return function () {
286
+ var args = [];
287
+ for (var _i = 0; _i < arguments.length; _i++) {
288
+ args[_i] = arguments[_i];
289
+ }
290
+ var callId = generateDeterministicId(__spreadArray([fnName, fn], args, true));
291
+ if (typeof logCall === 'function') {
292
+ // Deep clone args, so that we may detect mutations later on
293
+ logCall({
294
+ type: 'call',
295
+ callId: callId,
296
+ args: copy(args),
297
+ fnName: fnName
298
+ });
299
+ }
300
+ var result = fn.apply(void 0, args);
301
+ if (typeof logResult === 'function') {
302
+ logResult({
303
+ type: 'result',
304
+ callId: callId,
305
+ args: args,
306
+ result: result,
307
+ fnName: fnName
308
+ });
309
+ }
310
+ return result;
311
+ };
312
+ }
22
313
 
23
- return arr2;
314
+ /**
315
+ * /\$\{\s*([\w$.]+)\s*\}/g
316
+ * ![](docs/resources/interpolation_regexp.png)
317
+ *
318
+ * RegExp used for matching interpolation expressions.
319
+ * Allows a non-interrupted sequence of alphanumeric chars ([A-Za-z0-9_]),
320
+ * dollar signs ($) and dots (.) wrapped in curly braces ({})
321
+ * with or without any number of whitespace chars (' ') between braces and the
322
+ * value identifier.
323
+ *
324
+ * Some resources on RegExp safety concerning RegExp Denial of Service (ReDOS)
325
+ * through Catastrophic backtracking, for future study and reference:
326
+ *
327
+ * - [Catastrophic backtracking](https://www.regular-expressions.info/catastrophic.html)
328
+ * - [Regular expression visualizer](https://github.com/CJex/regulex)
329
+ * - [Validator.js](https://github.com/validatorjs/validator.js)
330
+ * - [Stack Overflow interesting question](https://stackoverflow.com/questions/63127145/safe-regex-patterns-from-redos-attack)
331
+ * - [Catastrophic backtracking - JavaScript Info](https://javascript.info/regexp-catastrophic-backtracking#preventing-backtracking)
332
+ * - [Google re2 library](https://github.com/google/re2)
333
+ * - [Google re2 for Node.js - re2](https://github.com/uhop/node-re2/)
334
+ *
335
+ * @const {RegExp} INTERPOLATION_REGEXP
336
+ */
337
+ var INTERPOLATION_REGEXP = /\$\{\s*([\w$.]+)\s*\}/g;
338
+ /**
339
+ * @function $stringInterpolate
340
+ * @param {String} template Basic JS template string like `${value.path}` value
341
+ * interpolation. It is possible to access nested properties
342
+ * through dot `.` notation. Keywords between braces are
343
+ * only interpreted as paths to the value. No logic
344
+ * supported: loops, conditionals, etc.
345
+ * @param {Object | Array} data Data context to be used for interpolation
346
+ */
347
+ function interpolate(template, data, _a) {
348
+ var _b = _a === void 0 ? {} : _a,
349
+ _c = _b.maxLength,
350
+ maxLength = _c === void 0 ? 10000 : _c;
351
+ if (template.length > maxLength) {
352
+ throw new Error("Template exceeds maxLength ".concat(maxLength));
24
353
  }
354
+ return template.replace(INTERPOLATION_REGEXP, function (_, path) {
355
+ var value = getProperty(data, path);
356
+ switch (_typeof(value)) {
357
+ case 'number':
358
+ {
359
+ return value + '';
360
+ }
361
+ case 'string':
362
+ {
363
+ return value;
364
+ }
365
+ default:
366
+ {
367
+ console.warn("Attempting to use non interpolatable value in interpolate ".concat(value));
368
+ return '';
369
+ }
370
+ }
371
+ });
25
372
  }
26
373
 
27
- function _iterableToArray(iter) {
28
- if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
374
+ function maybeFn(input) {
375
+ var args = [];
376
+ for (var _i = 1; _i < arguments.length; _i++) {
377
+ args[_i - 1] = arguments[_i];
378
+ }
379
+ return typeof input === 'function' ? input.apply(void 0, args) : input;
29
380
  }
30
381
 
31
- function _nonIterableSpread() {
32
- throw new TypeError("Invalid attempt to spread non-iterable instance");
382
+ function promiseReduce(inputArr, reducerFn, initial) {
383
+ return inputArr.reduce(function (prevPromise, input, index) {
384
+ return prevPromise.then(function (acc) {
385
+ return reducerFn(acc, input, index);
386
+ });
387
+ }, Promise.resolve(initial));
33
388
  }
34
389
 
35
- var mingo = require('mingo');
390
+ var DEFAULT_OPTIONS = {
391
+ mode: 'eager',
392
+ maxDepth: 30,
393
+ clone: copy
394
+ };
395
+ function _echo(input) {
396
+ return input;
397
+ }
398
+ function resolveNestedPromises(node, options, ctx) {
399
+ if (options === void 0) {
400
+ options = {};
401
+ }
402
+ if (ctx === void 0) {
403
+ ctx = {
404
+ root: node,
405
+ depth: 0
406
+ };
407
+ }
408
+ return __awaiter(this, void 0, void 0, function () {
409
+ var opts, promises, _cloneFn, clonedNode, resolved;
410
+ return __generator(this, function (_a) {
411
+ opts = _assign(_assign({}, DEFAULT_OPTIONS), options);
412
+ promises = [];
413
+ _cloneFn = opts.clone || _echo;
414
+ clonedNode = _cloneFn(node);
415
+ resolved = traverse(clonedNode).forEach(function (value) {
416
+ var _this = this;
417
+ var depth = ctx.depth + this.path.length;
418
+ if (depth > opts.maxDepth) {
419
+ throw new Error("Max depth exceeded: { depth: ".concat(depth, ", maxDepth: ").concat(opts.maxDepth, " }"));
420
+ }
421
+ if (value instanceof Promise) {
422
+ var updatePromise = value.then(function (resolvedValue) {
423
+ return __awaiter(_this, void 0, void 0, function () {
424
+ var _a, _b;
425
+ return __generator(this, function (_c) {
426
+ switch (_c.label) {
427
+ case 0:
428
+ // Arrow function;
429
+ // this refers to outside this ctx
430
+ _a = this.update;
431
+ if (!(opts.mode === 'eager')) return [3 /*break*/, 2];
432
+ return [4 /*yield*/, resolveNestedPromises(resolvedValue, opts, _assign(_assign({}, ctx), {
433
+ // pass the base depth of the current value
434
+ depth: depth
435
+ }))];
436
+ case 1:
437
+ _b = _c.sent();
438
+ return [3 /*break*/, 3];
439
+ case 2:
440
+ _b = resolvedValue;
441
+ _c.label = 3;
442
+ case 3:
443
+ // Arrow function;
444
+ // this refers to outside this ctx
445
+ _a.apply(this, [_b]);
446
+ return [2 /*return*/];
447
+ }
448
+ });
449
+ });
450
+ });
451
+ promises = __spreadArray(__spreadArray([], promises, true), [updatePromise], false);
452
+ }
453
+ });
454
+ return [2 /*return*/, Promise.all(promises).then(function () {
455
+ return resolved;
456
+ })];
457
+ });
458
+ });
459
+ }
36
460
 
37
- var _test = function _test(criteria) {
38
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
39
- args[_key - 1] = arguments[_key];
461
+ function arrayChunk(array, chunkSize) {
462
+ var chunks = [];
463
+ for (var i = 0; i < array.length; i += chunkSize) {
464
+ chunks.push(array.slice(i, i + chunkSize));
40
465
  }
466
+ return chunks;
467
+ }
468
+
469
+ var SKIPPED = Symbol();
470
+ function batchFn(fn, _a) {
471
+ var _b = _a === void 0 ? {} : _a,
472
+ _c = _b.batchSize,
473
+ batchSize = _c === void 0 ? 10 : _c,
474
+ skip = _b.skip;
475
+ return function batchExec(items) {
476
+ var _this = this;
477
+ var batches = arrayChunk(items, batchSize);
478
+ var promise = new PromiseLikeEventEmitter();
479
+ // const events = new EventEmitter<EventTypes>()
480
+ var progressCount = 0;
481
+ var resultsPromise = promiseReduce(batches, function (acc, batchItems, index) {
482
+ return __awaiter(_this, void 0, void 0, function () {
483
+ var batch, batchResults;
484
+ var _this = this;
485
+ return __generator(this, function (_a) {
486
+ switch (_a.label) {
487
+ case 0:
488
+ batch = {
489
+ index: index,
490
+ items: batchItems
491
+ };
492
+ promise.emit('batchStart', {
493
+ batch: batch
494
+ });
495
+ return [4 /*yield*/, Promise.all(batchItems.map(function (item) {
496
+ return __awaiter(_this, void 0, void 0, function () {
497
+ var itemSkip, itemResult, _a, err_1;
498
+ return __generator(this, function (_b) {
499
+ switch (_b.label) {
500
+ case 0:
501
+ _b.trys.push([0, 5,, 6]);
502
+ promise.emit('itemStart', {
503
+ batch: batch,
504
+ item: item
505
+ });
506
+ return [4 /*yield*/, typeof skip === 'function' && skip(item)];
507
+ case 1:
508
+ itemSkip = _b.sent();
509
+ if (itemSkip) {
510
+ promise.emit('itemSkip', {
511
+ batch: batch,
512
+ item: item
513
+ });
514
+ }
515
+ if (!itemSkip) return [3 /*break*/, 2];
516
+ _a = SKIPPED;
517
+ return [3 /*break*/, 4];
518
+ case 2:
519
+ return [4 /*yield*/, fn(item)];
520
+ case 3:
521
+ _a = _b.sent();
522
+ _b.label = 4;
523
+ case 4:
524
+ itemResult = _a;
525
+ progressCount += 1;
526
+ promise.emit('progress', {
527
+ type: itemSkip ? 'skip' : 'data',
528
+ batch: batch,
529
+ progress: progressCount / items.length,
530
+ item: item,
531
+ result: itemResult
532
+ });
533
+ return [2 /*return*/, itemResult];
534
+ case 5:
535
+ err_1 = _b.sent();
536
+ progressCount += 1;
537
+ promise.emit('progress', {
538
+ type: 'error',
539
+ batch: batch,
540
+ progress: progressCount / items.length,
541
+ item: item,
542
+ result: err_1
543
+ });
544
+ return [2 /*return*/, err_1];
545
+ case 6:
546
+ return [2 /*return*/];
547
+ }
548
+ });
549
+ });
550
+ }))];
551
+ case 1:
552
+ batchResults = _a.sent();
553
+ promise.emit('batchProgress', {
554
+ batch: batch,
555
+ progress: batch.index + 1 / batches.length,
556
+ results: batchResults
557
+ });
558
+ return [2 /*return*/, __spreadArray(__spreadArray([], acc, true), batchResults, true)];
559
+ }
560
+ });
561
+ });
562
+ }, []);
563
+ resultsPromise.then(function (results) {
564
+ promise.resolve(results);
565
+ promise.emit('results', {
566
+ results: results
567
+ });
568
+ }, function (err) {
569
+ promise.reject(err);
570
+ promise.emit('error', err);
571
+ });
572
+ return promise;
573
+ };
574
+ }
575
+ function parseBatchedResults(inputs, results) {
576
+ return results.reduce(function (acc, result, index) {
577
+ if (result === SKIPPED) {
578
+ return _assign(_assign({}, acc), {
579
+ skipped: __spreadArray(__spreadArray([], acc.skipped, true), [inputs[index]], false)
580
+ });
581
+ } else if (result instanceof Error) {
582
+ return _assign(_assign({}, acc), {
583
+ errors: __spreadArray(__spreadArray([], acc.errors, true), [result], false)
584
+ });
585
+ } else {
586
+ return _assign(_assign({}, acc), {
587
+ results: __spreadArray(__spreadArray([], acc.results, true), [result], false)
588
+ });
589
+ }
590
+ }, {
591
+ results: [],
592
+ errors: [],
593
+ skipped: []
594
+ });
595
+ }
596
+
597
+ function resolvePaths(sourceObj, paths) {
598
+ return paths.reduce(function (acc, path) {
599
+ var _a = Array.isArray(path) ? path : [path, path],
600
+ targetPath = _a[0],
601
+ resolver = _a[1];
602
+ var value = typeof resolver === 'string' ? getProperty(sourceObj, resolver) : resolver(sourceObj);
603
+ return setProperty(acc, targetPath, value);
604
+ }, {});
605
+ }
41
606
 
42
- if (_typeof(criteria) === 'object') {
43
- var query = new mingo.Query(criteria);
44
- return query.test(args[0]);
45
- } else if (typeof criteria === 'function') {
46
- return criteria.apply(void 0, args);
607
+ function testCriteria(criteria, input) {
608
+ switch (_typeof(criteria)) {
609
+ case 'function':
610
+ {
611
+ return criteria(input);
612
+ }
613
+ case 'object':
614
+ {
615
+ if (criteria instanceof RegExp) {
616
+ return criteria.test(input);
617
+ } else {
618
+ throw new Error('Unsupported criteria');
619
+ }
620
+ }
621
+ case 'string':
622
+ case 'symbol':
623
+ {
624
+ return criteria === input;
625
+ }
626
+ }
627
+ }
628
+ function switchValue(input, cases) {
629
+ if (Array.isArray(cases)) {
630
+ var matching = cases.find(function (case_) {
631
+ return case_.length === 1 || testCriteria(case_[0], input);
632
+ });
633
+ return matching ? matching.length === 1 ? matching[0] : matching[1] : undefined;
47
634
  } else {
48
- return criteria;
635
+ return cases[input] || cases["default"];
49
636
  }
50
- };
637
+ }
638
+ function switchExec(input, cases) {
639
+ var args = [];
640
+ for (var _i = 2; _i < arguments.length; _i++) {
641
+ args[_i - 2] = arguments[_i];
642
+ }
643
+ var fn = switchValue(input, cases);
644
+ return fn.apply(void 0, args);
645
+ }
646
+
647
+ /*!
648
+ * is-plain-object <https://github.com/jonschlinkert/is-plain-object>
649
+ *
650
+ * Copyright (c) 2014-2017, Jon Schlinkert.
651
+ * Released under the MIT License.
652
+ */
653
+
654
+ function isObject(o) {
655
+ return Object.prototype.toString.call(o) === '[object Object]';
656
+ }
657
+ function isPlainObject(o) {
658
+ var ctor, prot;
659
+ if (isObject(o) === false) return false;
51
660
 
52
- var _fnApply = function _fnApply(fnSpec) {
53
- for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
54
- args[_key2 - 1] = arguments[_key2];
661
+ // If has modified constructor
662
+ ctor = o.constructor;
663
+ if (ctor === undefined) return true;
664
+
665
+ // If has modified prototype
666
+ prot = ctor.prototype;
667
+ if (isObject(prot) === false) return false;
668
+
669
+ // If constructor does not have an Object-specific method
670
+ if (prot.hasOwnProperty('isPrototypeOf') === false) {
671
+ return false;
55
672
  }
56
673
 
57
- return typeof fnSpec === 'function' ? fnSpec.apply(void 0, args) : fnSpec.fn.apply(fnSpec, args);
58
- };
674
+ // Most likely a plain Object
675
+ return true;
676
+ }
59
677
 
60
- var applyMatching = function applyMatching(fnSpecList) {
61
- for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
62
- args[_key3 - 1] = arguments[_key3];
678
+ //
679
+ // The only role of this function is to let typescript
680
+ // be aware of the available types
681
+ //
682
+ function typeMap(typeMap) {
683
+ return typeMap;
684
+ }
685
+ function makeTypeOf(typeMap) {
686
+ return function typeOf(value) {
687
+ for (var typeName in typeMap) {
688
+ if (typeMap[typeName](value)) {
689
+ return typeName;
690
+ }
691
+ }
692
+ return null;
693
+ };
694
+ }
695
+ function basicType(typeName) {
696
+ return function (value) {
697
+ return _typeof(value) === typeName;
698
+ };
699
+ }
700
+ var DEFAULT_TYPES = typeMap({
701
+ //
702
+ // Primitives
703
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#primitive_values
704
+ //
705
+ "null": function _null(value) {
706
+ return value === null;
707
+ },
708
+ undefined: basicType('undefined'),
709
+ "boolean": basicType('boolean'),
710
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
711
+ number: function number(value) {
712
+ return typeof value === 'number' && !Number.isNaN(value);
713
+ },
714
+ bigint: basicType('bigint'),
715
+ string: basicType('string'),
716
+ symbol: basicType('symbol'),
717
+ "function": basicType('function'),
718
+ object: isPlainObject,
719
+ array: function array(value) {
720
+ return Array.isArray(value);
721
+ },
722
+ regexp: function regexp(value) {
723
+ return value instanceof RegExp;
724
+ },
725
+ nan: function nan(value) {
726
+ return Number.isNaN(value);
727
+ },
728
+ date: function date(value) {
729
+ return value instanceof Date;
730
+ },
731
+ map: function map(value) {
732
+ return value instanceof Map;
733
+ },
734
+ weakmap: function weakmap(value) {
735
+ return value instanceof WeakMap;
736
+ },
737
+ set: function set(value) {
738
+ return value instanceof Set;
739
+ },
740
+ weakset: function weakset(value) {
741
+ return value instanceof WeakSet;
742
+ },
743
+ promise: function promise(value) {
744
+ return value instanceof Promise;
745
+ },
746
+ error: function error(value) {
747
+ return value instanceof Error;
63
748
  }
749
+ });
750
+ var typeOf = makeTypeOf(DEFAULT_TYPES);
64
751
 
65
- var matching = fnSpecList.find(function (fnSpec) {
66
- return _test.apply(void 0, [fnSpec.criteria].concat(args));
67
- });
752
+ var TYPE_RE = /([a-z0-9]+)(\!)?$/i;
753
+ var TYPE_SEPARATOR_RE = /\s*\|\s*/g;
754
+ function parseSingleType(typeInput) {
755
+ var match = typeInput.match(TYPE_RE);
756
+ if (!match) {
757
+ throw new Error("Invalid type ".concat(typeInput));
758
+ } else {
759
+ var typeName = match[1],
760
+ requiredSignal = match[2];
761
+ return {
762
+ type: typeName,
763
+ required: requiredSignal === '!'
764
+ };
765
+ }
766
+ }
767
+ function serializeExpectedTypes(expectedTypes) {
768
+ return expectedTypes.map(function (expectedType) {
769
+ return "".concat(expectedType.type).concat(expectedType.required ? '!' : '');
770
+ }).join(' | ');
771
+ }
772
+ function parseExpectedTypes(expectedTypesInput) {
773
+ switch (typeOf(expectedTypesInput)) {
774
+ case 'string':
775
+ {
776
+ return expectedTypesInput.split(TYPE_SEPARATOR_RE).map(function (typeInput) {
777
+ return parseSingleType(typeInput);
778
+ });
779
+ }
780
+ case 'array':
781
+ {
782
+ return expectedTypesInput.map(function (expectedType) {
783
+ return typeof expectedType === 'string' ? parseSingleType(expectedType) : expectedType;
784
+ });
785
+ }
786
+ case 'object':
787
+ {
788
+ return [expectedTypesInput];
789
+ }
790
+ default:
791
+ {
792
+ throw new Error("Invalid expectedType ".concat(expectedTypesInput));
793
+ }
794
+ }
795
+ }
796
+ function typeValidator(expectedTypesInput) {
797
+ var expectedTypes = parseExpectedTypes(expectedTypesInput);
798
+ return function validateType(input) {
799
+ var inputType = typeOf(input);
800
+ var nullOrUndefined = function nullOrUndefined() {
801
+ return expectedTypes.some(function (expectedType) {
802
+ return expectedType.type === 'undefined' || expectedType.type === 'null' || expectedType.required === false;
803
+ });
804
+ };
805
+ var isValid = switchExec(inputType, {
806
+ "null": nullOrUndefined,
807
+ undefined: nullOrUndefined,
808
+ "default": function _default() {
809
+ return expectedTypes.some(function (expectedType) {
810
+ return expectedType.type === inputType;
811
+ });
812
+ }
813
+ });
814
+ if (isValid) {
815
+ return true;
816
+ } else {
817
+ return {
818
+ expectedTypes: serializeExpectedTypes(expectedTypes),
819
+ input: input
820
+ };
821
+ }
822
+ };
823
+ }
824
+
825
+ //
826
+ // And operator (serial)
827
+ //
828
+ function and$1(validators) {
829
+ return function validateAnd(input, _a) {
830
+ var validateSync = _a.validateSync;
831
+ var result = validators.reduce(function (acc, validator) {
832
+ return acc !== true ? acc : validateSync(validator, input);
833
+ }, true);
834
+ if (result === true) {
835
+ return true;
836
+ } else {
837
+ return result;
838
+ }
839
+ };
840
+ }
841
+ //
842
+ // Or operator (serial)
843
+ //
844
+ function or$1(validators) {
845
+ return function validateOr(input, _a) {
846
+ var validateSync = _a.validateSync;
847
+ var someIsValid = validators.some(function (validator) {
848
+ return validateSync(validator, input) === true;
849
+ });
850
+ return someIsValid;
851
+ };
852
+ }
853
+ //
854
+ // Not
855
+ //
856
+ function not$1(validator) {
857
+ return function validateNot(input, _a) {
858
+ var validateSync = _a.validateSync;
859
+ return validateSync(validator, input) !== true;
860
+ };
861
+ }
862
+
863
+ function _shapeGeneralValidator$1(_a) {
864
+ var input = _a.input,
865
+ baseType = _a.baseType,
866
+ collectNestedErrors = _a.collectNestedErrors,
867
+ validateSync = _a.validateSync;
868
+ var baseTypeValidation = validateSync(typeValidator({
869
+ type: baseType,
870
+ required: true
871
+ }), input);
872
+ if (baseTypeValidation !== true) {
873
+ return baseTypeValidation;
874
+ }
875
+ var nestedErrors = collectNestedErrors();
876
+ return nestedErrors.length === 0 ? true : {
877
+ input: input,
878
+ nestedErrors: nestedErrors
879
+ };
880
+ }
881
+ function obj$1(objShape) {
882
+ return function validateObject(input, _a) {
883
+ var validateSync = _a.validateSync;
884
+ return _shapeGeneralValidator$1({
885
+ input: input,
886
+ baseType: 'object',
887
+ validateSync: validateSync,
888
+ collectNestedErrors: function collectNestedErrors() {
889
+ return Object.entries(objShape).reduce(function (acc, _a) {
890
+ var path = _a[0],
891
+ pathValidator = _a[1];
892
+ var pathInput = getProperty(input, path);
893
+ var pathResult = validateSync(pathValidator, pathInput);
894
+ return pathResult === true ? acc : __spreadArray(__spreadArray([], acc, true), [_assign(_assign({}, pathResult), {
895
+ path: path
896
+ })], false);
897
+ }, []);
898
+ }
899
+ });
900
+ };
901
+ }
902
+ function objOf$1(ofType) {
903
+ return function validateObjOf(input, _a) {
904
+ var validateSync = _a.validateSync;
905
+ return _shapeGeneralValidator$1({
906
+ input: input,
907
+ baseType: 'object',
908
+ validateSync: validateSync,
909
+ collectNestedErrors: function collectNestedErrors() {
910
+ return Object.entries(input).reduce(function (acc, _a) {
911
+ var key = _a[0],
912
+ keyInput = _a[1];
913
+ var indexResult = validateSync(ofType, keyInput);
914
+ return indexResult === true ? acc : __spreadArray(__spreadArray([], acc, true), [_assign(_assign({}, indexResult), {
915
+ path: key
916
+ })], false);
917
+ }, []);
918
+ }
919
+ });
920
+ };
921
+ }
922
+ function tuple$1(tupleShape) {
923
+ return function validateTuple(input, _a) {
924
+ var validateSync = _a.validateSync;
925
+ return _shapeGeneralValidator$1({
926
+ input: input,
927
+ baseType: 'array',
928
+ validateSync: validateSync,
929
+ collectNestedErrors: function collectNestedErrors() {
930
+ return tupleShape.reduce(function (acc, indexValidator, index) {
931
+ var indexInput = input[index];
932
+ var indexResult = validateSync(indexValidator, indexInput);
933
+ return indexResult === true ? acc : __spreadArray(__spreadArray([], acc, true), [_assign(_assign({}, indexResult), {
934
+ path: index + ''
935
+ })], false);
936
+ }, []);
937
+ }
938
+ });
939
+ };
940
+ }
941
+ function arrayOf$1(ofType) {
942
+ return function validateArrayOf(input, _a) {
943
+ var validateSync = _a.validateSync;
944
+ return _shapeGeneralValidator$1({
945
+ input: input,
946
+ baseType: 'array',
947
+ validateSync: validateSync,
948
+ collectNestedErrors: function collectNestedErrors() {
949
+ return input.reduce(function (acc, indexInput, index) {
950
+ var indexResult = validateSync(ofType, indexInput);
951
+ return indexResult === true ? acc : __spreadArray(__spreadArray([], acc, true), [_assign(_assign({}, indexResult), {
952
+ path: index + ''
953
+ })], false);
954
+ }, []);
955
+ }
956
+ });
957
+ };
958
+ }
68
959
 
69
- if (matching) {
70
- return _fnApply.apply(void 0, [matching].concat(args));
960
+ function defaultErrorMessage(_a) {
961
+ var input = _a.input,
962
+ message = _a.message,
963
+ nestedErrors = _a.nestedErrors,
964
+ expectedTypes = _a.expectedTypes;
965
+ if (message) {
966
+ return message;
967
+ }
968
+ if (nestedErrors) {
969
+ return "Invalid input: `".concat(JSON.stringify(input), "`.\n").concat(nestedErrors.map(function (error) {
970
+ return " - ".concat(error.path, ": ").concat(error.message);
971
+ }).join('\n'));
972
+ } else if (expectedTypes) {
973
+ var inputType = typeOf(input);
974
+ return "Invalid input: '".concat(input, "'. Expected type(s) `").concat(expectedTypes, "`, but got type `").concat(inputType === null ? 'unknown' : inputType, "`");
71
975
  } else {
72
- throw new Error('No matching spec');
976
+ return "Invalid input: '".concat(input, "'.");
73
977
  }
74
- };
978
+ }
75
979
 
76
- var applyMatchingReduce = function applyMatchingReduce(fnSpecList, arg) {
77
- var injectArgs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
78
- return fnSpecList.reduce(function (acc, fnSpec) {
79
- return _test(fnSpec.criteria, acc) ? _fnApply.apply(void 0, [fnSpec, acc].concat(_toConsumableArray(injectArgs))) : acc;
80
- }, arg);
81
- };
980
+ function resolveValidationResult(_a) {
981
+ var input = _a.input,
982
+ result = _a.result,
983
+ errorMessage = _a.errorMessage;
984
+ if (result === true) {
985
+ return true;
986
+ } else {
987
+ var _falseLikeResult = function _falseLikeResult() {
988
+ return {
989
+ message: errorMessage({
990
+ input: input
991
+ })
992
+ };
993
+ };
994
+ return _assign({
995
+ input: input
996
+ }, switchExec(typeOf(result), {
997
+ string: function string() {
998
+ return {
999
+ message: result
1000
+ };
1001
+ },
1002
+ error: function error() {
1003
+ return {
1004
+ message: result.message,
1005
+ error: result
1006
+ };
1007
+ },
1008
+ object: function object() {
1009
+ var result_ = result;
1010
+ return _assign(_assign({}, result_), {
1011
+ message: errorMessage(result_)
1012
+ });
1013
+ },
1014
+ //
1015
+ // Boolean result here MUST be false, as true was tested earlier
1016
+ //
1017
+ "boolean": _falseLikeResult,
1018
+ "null": _falseLikeResult,
1019
+ undefined: _falseLikeResult,
1020
+ //
1021
+ // Unsupported validation result
1022
+ //
1023
+ "default": function _default() {
1024
+ throw new Error("Invalid validation result: ".concat(result, " (").concat(typeOf(result), ")"));
1025
+ }
1026
+ }));
1027
+ }
1028
+ }
82
1029
 
83
- var applyMatchingReduceAsync = function applyMatchingReduceAsync(fnSpecList, arg) {
84
- var injectArgs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
85
- return fnSpecList.reduce(function (previous, fnSpec) {
86
- return previous.then(function (res) {
87
- return _test(fnSpec.criteria, res) ? Promise.resolve(_fnApply.apply(void 0, [fnSpec, res].concat(_toConsumableArray(injectArgs)))) : Promise.resolve(res);
1030
+ function parseValidatorInput(_a, validatorInput) {
1031
+ var objValidator = _a.objValidator;
1032
+ //
1033
+ // Parses the validator fn.
1034
+ // If provided with a string, will return a type validator
1035
+ // If provided with a function, will return the function itself
1036
+ //
1037
+ function _parseValidatorFn(fnInput) {
1038
+ switch (typeOf(fnInput)) {
1039
+ case 'string':
1040
+ {
1041
+ return typeValidator(fnInput);
1042
+ }
1043
+ case 'object':
1044
+ {
1045
+ return objValidator(fnInput);
1046
+ }
1047
+ case 'function':
1048
+ {
1049
+ return fnInput;
1050
+ }
1051
+ default:
1052
+ {
1053
+ throw new TypeError("Unsupported fnInput ".concat(fnInput));
1054
+ }
1055
+ }
1056
+ }
1057
+ function _parseValidatorErrorMessageFn(fnInput) {
1058
+ switch (_typeof(fnInput)) {
1059
+ case 'string':
1060
+ {
1061
+ return function (details) {
1062
+ return interpolate(fnInput, details);
1063
+ };
1064
+ }
1065
+ case 'function':
1066
+ {
1067
+ return fnInput;
1068
+ }
1069
+ default:
1070
+ {
1071
+ throw new TypeError("Unsupported error message input ".concat(fnInput));
1072
+ }
1073
+ }
1074
+ }
1075
+ //
1076
+ // Parses the validator input. For more details, see:
1077
+ // types/common.ts -> ValidatorSystem
1078
+ //
1079
+ // Ensures the return result is always of the following format:
1080
+ // [ValidatorFn, ValidatorErrorMessageInput]
1081
+ //
1082
+ var _b = Array.isArray(validatorInput) ? validatorInput : [validatorInput, defaultErrorMessage],
1083
+ fnInput = _b[0],
1084
+ errorMessageInput = _b[1];
1085
+ return [_parseValidatorFn(fnInput), _parseValidatorErrorMessageFn(errorMessageInput)];
1086
+ }
1087
+
1088
+ function validateSyncFn(validatorInput, input) {
1089
+ var result;
1090
+ var _a = parseValidatorInput({
1091
+ objValidator: obj$1
1092
+ }, validatorInput),
1093
+ validatorFn = _a[0],
1094
+ errorMessage = _a[1];
1095
+ try {
1096
+ result = validatorFn(input, {
1097
+ validateSync: validateSyncFn
88
1098
  });
89
- }, Promise.resolve(arg));
90
- };
1099
+ } catch (err) {
1100
+ result = err;
1101
+ }
1102
+ if (result instanceof Promise) {
1103
+ throw new Error('Promises not supported in sync validation method');
1104
+ }
1105
+ return resolveValidationResult({
1106
+ errorMessage: errorMessage,
1107
+ input: input,
1108
+ result: result
1109
+ });
1110
+ }
91
1111
 
92
- module.exports = {
93
- applyMatching: applyMatching,
94
- applyMatchingReduce: applyMatchingReduce,
95
- applyMatchingReduceAsync: applyMatchingReduceAsync
96
- };
1112
+ var ValidationError = /** @class */function (_super) {
1113
+ __extends(ValidationError, _super);
1114
+ function ValidationError(_a) {
1115
+ var _this = this;
1116
+ var message = _a.message,
1117
+ details = __rest(_a, ["message"]);
1118
+ _this = _super.call(this, message || "Invalid input ".concat(details.input)) || this;
1119
+ _this.name = 'ValidationError';
1120
+ // Capture correct stack trace in V8 environments (Node.js, Google Chrome)
1121
+ if (Error.captureStackTrace) {
1122
+ Error.captureStackTrace(_this, ValidationError);
1123
+ }
1124
+ Object.assign(_this, details);
1125
+ return _this;
1126
+ }
1127
+ ValidationError.prototype.toJSON = function () {
1128
+ return {
1129
+ message: this.message,
1130
+ input: this.input,
1131
+ code: this.code,
1132
+ error: this.error,
1133
+ path: this.path,
1134
+ nestedErrors: this.nestedErrors
1135
+ };
1136
+ };
1137
+ return ValidationError;
1138
+ }(Error);
97
1139
 
98
- var arrayReduceAsync = function arrayReduceAsync(arr, fn, initial) {
99
- return arr.reduce(function (previousPromise, item, index) {
100
- return previousPromise.then(function (acc) {
101
- return Promise.resolve(fn(acc, item, index, arr));
1140
+ function assertValidSync(validator, input) {
1141
+ var validationResult = validate(validator, input);
1142
+ if (validationResult === true) {
1143
+ return input;
1144
+ } else {
1145
+ throw new ValidationError(validationResult);
1146
+ }
1147
+ }
1148
+ var validate = validateSyncFn;
1149
+ validate.type = typeValidator;
1150
+ validate.obj = obj$1;
1151
+ validate.objOf = objOf$1;
1152
+ validate.tuple = tuple$1;
1153
+ validate.arrayOf = arrayOf$1;
1154
+ validate.and = and$1;
1155
+ validate.or = or$1;
1156
+ validate.not = not$1;
1157
+ validate.assertValid = assertValidSync;
1158
+
1159
+ function _shapeGeneralValidator(_a) {
1160
+ var input = _a.input,
1161
+ baseType = _a.baseType,
1162
+ validateAsync = _a.validateAsync,
1163
+ collectNestedErrors = _a.collectNestedErrors;
1164
+ return __awaiter(this, void 0, void 0, function () {
1165
+ var objectValidation, nestedErrors;
1166
+ return __generator(this, function (_b) {
1167
+ switch (_b.label) {
1168
+ case 0:
1169
+ return [4 /*yield*/, validateAsync(typeValidator({
1170
+ type: baseType,
1171
+ required: true
1172
+ }), input)];
1173
+ case 1:
1174
+ objectValidation = _b.sent();
1175
+ if (objectValidation !== true) {
1176
+ return [2 /*return*/, objectValidation];
1177
+ }
1178
+ return [4 /*yield*/, collectNestedErrors()];
1179
+ case 2:
1180
+ nestedErrors = _b.sent();
1181
+ return [2 /*return*/, nestedErrors.length === 0 ? true : {
1182
+ input: input,
1183
+ nestedErrors: nestedErrors
1184
+ }];
1185
+ }
102
1186
  });
103
- }, Promise.resolve(initial));
104
- };
1187
+ });
1188
+ }
1189
+ function obj(objShape) {
1190
+ return function asyncValidateObj(input, _a) {
1191
+ var validateAsync = _a.validateAsync;
1192
+ return __awaiter(this, void 0, void 0, function () {
1193
+ var _this = this;
1194
+ return __generator(this, function (_b) {
1195
+ return [2 /*return*/, _shapeGeneralValidator({
1196
+ input: input,
1197
+ baseType: 'object',
1198
+ validateAsync: validateAsync,
1199
+ collectNestedErrors: function collectNestedErrors() {
1200
+ return promiseReduce(Object.entries(objShape), function (acc, _a) {
1201
+ var path = _a[0],
1202
+ pathValidator = _a[1];
1203
+ return __awaiter(_this, void 0, void 0, function () {
1204
+ var pathInput, pathResult;
1205
+ return __generator(this, function (_b) {
1206
+ switch (_b.label) {
1207
+ case 0:
1208
+ pathInput = getProperty(input, path);
1209
+ return [4 /*yield*/, validateAsync(pathValidator, pathInput)];
1210
+ case 1:
1211
+ pathResult = _b.sent();
1212
+ return [2 /*return*/, pathResult === true ? acc : __spreadArray(__spreadArray([], acc, true), [_assign(_assign({}, pathResult), {
1213
+ path: path
1214
+ })], false)];
1215
+ }
1216
+ });
1217
+ });
1218
+ }, []);
1219
+ }
1220
+ })];
1221
+ });
1222
+ });
1223
+ };
1224
+ }
1225
+ function objOf(ofType) {
1226
+ return function asyncValidateObjOf(input, _a) {
1227
+ var validateAsync = _a.validateAsync;
1228
+ return __awaiter(this, void 0, void 0, function () {
1229
+ var _this = this;
1230
+ return __generator(this, function (_b) {
1231
+ return [2 /*return*/, _shapeGeneralValidator({
1232
+ input: input,
1233
+ baseType: 'object',
1234
+ validateAsync: validateAsync,
1235
+ collectNestedErrors: function collectNestedErrors() {
1236
+ return promiseReduce(Object.entries(input), function (acc, _a) {
1237
+ var key = _a[0],
1238
+ keyInput = _a[1];
1239
+ return __awaiter(_this, void 0, void 0, function () {
1240
+ var indexResult;
1241
+ return __generator(this, function (_b) {
1242
+ switch (_b.label) {
1243
+ case 0:
1244
+ return [4 /*yield*/, validateAsync(ofType, keyInput)];
1245
+ case 1:
1246
+ indexResult = _b.sent();
1247
+ return [2 /*return*/, indexResult === true ? acc : __spreadArray(__spreadArray([], acc, true), [_assign(_assign({}, indexResult), {
1248
+ path: key
1249
+ })], false)];
1250
+ }
1251
+ });
1252
+ });
1253
+ }, []);
1254
+ }
1255
+ })];
1256
+ });
1257
+ });
1258
+ };
1259
+ }
1260
+ function tuple(tupleShape) {
1261
+ return function asyncValidateTuple(input, _a) {
1262
+ var validateAsync = _a.validateAsync;
1263
+ return __awaiter(this, void 0, void 0, function () {
1264
+ var _this = this;
1265
+ return __generator(this, function (_b) {
1266
+ return [2 /*return*/, _shapeGeneralValidator({
1267
+ input: input,
1268
+ baseType: 'array',
1269
+ validateAsync: validateAsync,
1270
+ collectNestedErrors: function collectNestedErrors() {
1271
+ return promiseReduce(tupleShape, function (acc, indexValidator, index) {
1272
+ return __awaiter(_this, void 0, void 0, function () {
1273
+ var indexInput, indexResult;
1274
+ return __generator(this, function (_a) {
1275
+ switch (_a.label) {
1276
+ case 0:
1277
+ indexInput = input[index];
1278
+ return [4 /*yield*/, validateAsync(indexValidator, indexInput)];
1279
+ case 1:
1280
+ indexResult = _a.sent();
1281
+ return [2 /*return*/, indexResult === true ? acc : __spreadArray(__spreadArray([], acc, true), [_assign(_assign({}, indexResult), {
1282
+ path: index + ''
1283
+ })], false)];
1284
+ }
1285
+ });
1286
+ });
1287
+ }, []);
1288
+ }
1289
+ })];
1290
+ });
1291
+ });
1292
+ };
1293
+ }
1294
+ function arrayOf(ofType) {
1295
+ return function asyncValidateArrayOf(input, _a) {
1296
+ var validateAsync = _a.validateAsync;
1297
+ return __awaiter(this, void 0, void 0, function () {
1298
+ var _this = this;
1299
+ return __generator(this, function (_b) {
1300
+ return [2 /*return*/, _shapeGeneralValidator({
1301
+ input: input,
1302
+ baseType: 'array',
1303
+ validateAsync: validateAsync,
1304
+ collectNestedErrors: function collectNestedErrors() {
1305
+ return promiseReduce(input, function (acc, indexInput, index) {
1306
+ return __awaiter(_this, void 0, void 0, function () {
1307
+ var indexResult;
1308
+ return __generator(this, function (_a) {
1309
+ switch (_a.label) {
1310
+ case 0:
1311
+ return [4 /*yield*/, validateAsync(ofType, indexInput)];
1312
+ case 1:
1313
+ indexResult = _a.sent();
1314
+ return [2 /*return*/, indexResult === true ? acc : __spreadArray(__spreadArray([], acc, true), [_assign(_assign({}, indexResult), {
1315
+ path: index + ''
1316
+ })], false)];
1317
+ }
1318
+ });
1319
+ });
1320
+ }, []);
1321
+ }
1322
+ })];
1323
+ });
1324
+ });
1325
+ };
1326
+ }
105
1327
 
106
- var array = {
107
- arrayReduceAsync: arrayReduceAsync
108
- };
109
- var array_1 = array.arrayReduceAsync;
1328
+ //
1329
+ // And operator (serial)
1330
+ //
1331
+ function and(validators) {
1332
+ return function validateAnd(input, _a) {
1333
+ var validateAsync = _a.validateAsync;
1334
+ return __awaiter(this, void 0, void 0, function () {
1335
+ var result;
1336
+ return __generator(this, function (_b) {
1337
+ switch (_b.label) {
1338
+ case 0:
1339
+ return [4 /*yield*/, promiseReduce(validators, function (acc, validator) {
1340
+ return acc !== true ? acc : validateAsync(validator, input);
1341
+ }, true)];
1342
+ case 1:
1343
+ result = _b.sent();
1344
+ if (result === true) {
1345
+ return [2 /*return*/, true];
1346
+ } else {
1347
+ return [2 /*return*/, result];
1348
+ }
1349
+ }
1350
+ });
1351
+ });
1352
+ };
1353
+ }
1354
+ //
1355
+ // Or operator (serial)
1356
+ //
1357
+ function or(validators) {
1358
+ return function validateOr(input, _a) {
1359
+ var validateAsync = _a.validateAsync;
1360
+ return __awaiter(this, void 0, void 0, function () {
1361
+ var someIsValid;
1362
+ var _this = this;
1363
+ return __generator(this, function (_b) {
1364
+ switch (_b.label) {
1365
+ case 0:
1366
+ return [4 /*yield*/, promiseReduce(validators, function (acc, validator) {
1367
+ return __awaiter(_this, void 0, void 0, function () {
1368
+ var _a;
1369
+ return __generator(this, function (_b) {
1370
+ switch (_b.label) {
1371
+ case 0:
1372
+ _a = acc === true;
1373
+ if (_a) return [3 /*break*/, 2];
1374
+ return [4 /*yield*/, validateAsync(validator, input)];
1375
+ case 1:
1376
+ _a = _b.sent() === true;
1377
+ _b.label = 2;
1378
+ case 2:
1379
+ return [2 /*return*/, _a];
1380
+ }
1381
+ });
1382
+ });
1383
+ }, false)];
1384
+ case 1:
1385
+ someIsValid = _b.sent();
1386
+ return [2 /*return*/, someIsValid];
1387
+ }
1388
+ });
1389
+ });
1390
+ };
1391
+ }
1392
+ //
1393
+ // Not
1394
+ //
1395
+ function not(validator) {
1396
+ return function validateNot(input, _a) {
1397
+ var validateAsync = _a.validateAsync;
1398
+ return __awaiter(this, void 0, void 0, function () {
1399
+ var result;
1400
+ return __generator(this, function (_b) {
1401
+ switch (_b.label) {
1402
+ case 0:
1403
+ return [4 /*yield*/, validateAsync(validator, input)];
1404
+ case 1:
1405
+ result = _b.sent();
1406
+ return [2 /*return*/, result !== true];
1407
+ }
1408
+ });
1409
+ });
1410
+ };
1411
+ }
1412
+
1413
+ function validateAsyncFn(validatorInput, input) {
1414
+ return __awaiter(this, void 0, void 0, function () {
1415
+ var result, _a, validatorFn, errorMessage, err_1;
1416
+ return __generator(this, function (_b) {
1417
+ switch (_b.label) {
1418
+ case 0:
1419
+ _a = parseValidatorInput({
1420
+ objValidator: obj
1421
+ }, validatorInput), validatorFn = _a[0], errorMessage = _a[1];
1422
+ _b.label = 1;
1423
+ case 1:
1424
+ _b.trys.push([1, 3,, 4]);
1425
+ return [4 /*yield*/, validatorFn(input, {
1426
+ validateAsync: validateAsyncFn
1427
+ })];
1428
+ case 2:
1429
+ result = _b.sent();
1430
+ return [3 /*break*/, 4];
1431
+ case 3:
1432
+ err_1 = _b.sent();
1433
+ result = err_1;
1434
+ return [3 /*break*/, 4];
1435
+ case 4:
1436
+ return [2 /*return*/, resolveValidationResult({
1437
+ errorMessage: errorMessage,
1438
+ input: input,
1439
+ result: result
1440
+ })];
1441
+ }
1442
+ });
1443
+ });
1444
+ }
1445
+
1446
+ function assertValidAsync(validator, input) {
1447
+ return __awaiter(this, void 0, void 0, function () {
1448
+ var validationResult;
1449
+ return __generator(this, function (_a) {
1450
+ switch (_a.label) {
1451
+ case 0:
1452
+ return [4 /*yield*/, validateAsync(validator, input)];
1453
+ case 1:
1454
+ validationResult = _a.sent();
1455
+ if (validationResult === true) {
1456
+ return [2 /*return*/, input];
1457
+ } else {
1458
+ throw new ValidationError(validationResult);
1459
+ }
1460
+ }
1461
+ });
1462
+ });
1463
+ }
1464
+ var validateAsync = validateAsyncFn;
1465
+ validateAsync.type = typeValidator;
1466
+ validateAsync.obj = obj;
1467
+ validateAsync.objOf = objOf;
1468
+ validateAsync.tuple = tuple;
1469
+ validateAsync.arrayOf = arrayOf;
1470
+ validateAsync.and = and;
1471
+ validateAsync.or = or;
1472
+ validateAsync.not = not;
1473
+ validateAsync.assertValid = assertValidAsync;
110
1474
 
111
- export { array as __moduleExports, array_1 as arrayReduceAsync };
1475
+ export { PromiseLikeEventEmitter, SKIPPED, ValidationError, batchFn, debugFn, deepFreeze, interpolate, makeTypeOf, maybeFn, parseBatchedResults, promiseReduce, resolveNestedPromises, resolvePaths, switchExec, switchValue, typeMap, typeOf, validate, validateAsync, wait };