vest 4.4.2 → 4.5.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 (51) hide show
  1. package/dist/cjs/classnames.development.js +16 -90
  2. package/dist/cjs/classnames.production.js +1 -1
  3. package/dist/cjs/enforce/compose.development.js +5 -58
  4. package/dist/cjs/enforce/compose.production.js +1 -1
  5. package/dist/cjs/enforce/compounds.development.js +8 -51
  6. package/dist/cjs/enforce/compounds.production.js +1 -1
  7. package/dist/cjs/enforce/schema.development.js +5 -57
  8. package/dist/cjs/enforce/schema.production.js +1 -1
  9. package/dist/cjs/parser.development.js +15 -89
  10. package/dist/cjs/parser.production.js +1 -1
  11. package/dist/cjs/promisify.development.js +2 -26
  12. package/dist/cjs/promisify.production.js +1 -1
  13. package/dist/cjs/vest.development.js +197 -452
  14. package/dist/cjs/vest.production.js +1 -1
  15. package/dist/es/classnames.development.js +14 -88
  16. package/dist/es/classnames.production.js +1 -1
  17. package/dist/es/enforce/compose.development.js +1 -54
  18. package/dist/es/enforce/compose.production.js +1 -1
  19. package/dist/es/enforce/compounds.development.js +2 -45
  20. package/dist/es/enforce/compounds.production.js +1 -1
  21. package/dist/es/enforce/schema.development.js +1 -53
  22. package/dist/es/enforce/schema.production.js +1 -1
  23. package/dist/es/parser.development.js +14 -88
  24. package/dist/es/parser.production.js +1 -1
  25. package/dist/es/promisify.development.js +1 -25
  26. package/dist/es/promisify.production.js +1 -1
  27. package/dist/es/vest.development.js +131 -387
  28. package/dist/es/vest.production.js +1 -1
  29. package/dist/umd/classnames.development.js +27 -56
  30. package/dist/umd/classnames.production.js +1 -1
  31. package/dist/umd/enforce/compose.development.js +111 -94
  32. package/dist/umd/enforce/compose.production.js +1 -1
  33. package/dist/umd/enforce/compounds.development.js +185 -168
  34. package/dist/umd/enforce/compounds.production.js +1 -1
  35. package/dist/umd/enforce/schema.development.js +134 -117
  36. package/dist/umd/enforce/schema.production.js +1 -1
  37. package/dist/umd/parser.development.js +27 -56
  38. package/dist/umd/parser.production.js +1 -1
  39. package/dist/umd/vest.development.js +398 -386
  40. package/dist/umd/vest.production.js +1 -1
  41. package/package.json +4 -3
  42. package/testUtils/__tests__/partition.test.ts +21 -0
  43. package/testUtils/mockThrowError.ts +2 -1
  44. package/testUtils/partition.ts +13 -0
  45. package/testUtils/suiteDummy.ts +1 -1
  46. package/types/enforce/compose.d.ts +17 -32
  47. package/types/enforce/compounds.d.ts +17 -32
  48. package/types/enforce/schema.d.ts +17 -32
  49. package/types/parser.d.ts +8 -7
  50. package/types/promisify.d.ts +20 -35
  51. package/types/vest.d.ts +45 -60
@@ -4,12 +4,6 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.vest = {}));
5
5
  }(this, (function (exports) { 'use strict';
6
6
 
7
- var assign = Object.assign;
8
-
9
- function isFunction(value) {
10
- return typeof value === 'function';
11
- }
12
-
13
7
  function bindNot(fn) {
14
8
  return function () {
15
9
  var args = [];
@@ -20,6 +14,69 @@
20
14
  };
21
15
  }
22
16
 
17
+ function isNumeric(value) {
18
+ var str = String(value);
19
+ var num = Number(value);
20
+ var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
21
+ return Boolean(result);
22
+ }
23
+ var isNotNumeric = bindNot(isNumeric);
24
+
25
+ function numberEquals(value, eq) {
26
+ return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
27
+ }
28
+ var numberNotEquals = bindNot(numberEquals);
29
+
30
+ function lengthEquals(value, arg1) {
31
+ return numberEquals(value.length, arg1);
32
+ }
33
+ var lengthNotEquals = bindNot(lengthEquals);
34
+
35
+ function greaterThan(value, gt) {
36
+ return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
37
+ }
38
+
39
+ function longerThan(value, arg1) {
40
+ return greaterThan(value.length, arg1);
41
+ }
42
+
43
+ /**
44
+ * Creates a cache function
45
+ */
46
+ function createCache(maxSize) {
47
+ if (maxSize === void 0) { maxSize = 1; }
48
+ var cacheStorage = [];
49
+ var cache = function (deps, cacheAction) {
50
+ var cacheHit = cache.get(deps);
51
+ // cache hit is not null
52
+ if (cacheHit)
53
+ return cacheHit[1];
54
+ var result = cacheAction();
55
+ cacheStorage.unshift([deps.concat(), result]);
56
+ if (longerThan(cacheStorage, maxSize))
57
+ cacheStorage.length = maxSize;
58
+ return result;
59
+ };
60
+ // invalidate an item in the cache by its dependencies
61
+ cache.invalidate = function (deps) {
62
+ var index = findIndex(deps);
63
+ if (index > -1)
64
+ cacheStorage.splice(index, 1);
65
+ };
66
+ // Retrieves an item from the cache.
67
+ cache.get = function (deps) {
68
+ return cacheStorage[findIndex(deps)] || null;
69
+ };
70
+ return cache;
71
+ function findIndex(deps) {
72
+ return cacheStorage.findIndex(function (_a) {
73
+ var cachedDeps = _a[0];
74
+ return lengthEquals(deps, cachedDeps.length) &&
75
+ deps.every(function (dep, i) { return dep === cachedDeps[i]; });
76
+ });
77
+ }
78
+ }
79
+
23
80
  function isNull(value) {
24
81
  return value === null;
25
82
  }
@@ -35,39 +92,25 @@
35
92
  }
36
93
  var isNotNullish = bindNot(isNullish);
37
94
 
38
- function isStringValue(v) {
39
- return String(v) === v;
40
- }
41
-
42
- function endsWith(value, arg1) {
43
- return isStringValue(value) && isStringValue(arg1) && value.endsWith(arg1);
44
- }
45
- var doesNotEndWith = bindNot(endsWith);
46
-
47
- function equals(value, arg1) {
48
- return value === arg1;
49
- }
50
- var notEquals = bindNot(equals);
51
-
52
- function isNumeric(value) {
53
- var str = String(value);
54
- var num = Number(value);
55
- var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
56
- return Boolean(result);
95
+ function asArray(possibleArg) {
96
+ return [].concat(possibleArg);
57
97
  }
58
- var isNotNumeric = bindNot(isNumeric);
59
98
 
60
- function greaterThan(value, gt) {
61
- return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
99
+ function isFunction(value) {
100
+ return typeof value === 'function';
62
101
  }
63
102
 
64
- function numberEquals(value, eq) {
65
- return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
103
+ function optionalFunctionValue(value) {
104
+ var args = [];
105
+ for (var _i = 1; _i < arguments.length; _i++) {
106
+ args[_i - 1] = arguments[_i];
107
+ }
108
+ return isFunction(value) ? value.apply(void 0, args) : value;
66
109
  }
67
- var numberNotEquals = bindNot(numberEquals);
68
110
 
69
- function greaterThanOrEquals(value, gte) {
70
- return numberEquals(value, gte) || greaterThan(value, gte);
111
+ function defaultTo(value, defaultValue) {
112
+ var _a;
113
+ return (_a = optionalFunctionValue(value)) !== null && _a !== void 0 ? _a : optionalFunctionValue(defaultValue);
71
114
  }
72
115
 
73
116
  // The module is named "isArrayValue" since it
@@ -78,53 +121,154 @@
78
121
  }
79
122
  var isNotArray = bindNot(isArray);
80
123
 
81
- function inside(value, arg1) {
82
- if (isArray(arg1)) {
83
- return arg1.indexOf(value) !== -1;
124
+ function last(values) {
125
+ var valuesArray = asArray(values);
126
+ return valuesArray[valuesArray.length - 1];
127
+ }
128
+
129
+ // This is kind of a map/filter in one function.
130
+ // Normally, behaves like a nested-array map,
131
+ // but returning `null` will drop the element from the array
132
+ function transform(array, cb) {
133
+ var res = [];
134
+ for (var _i = 0, array_1 = array; _i < array_1.length; _i++) {
135
+ var v = array_1[_i];
136
+ if (isArray(v)) {
137
+ res.push(transform(v, cb));
138
+ }
139
+ else {
140
+ var output = cb(v);
141
+ if (isNotNull(output)) {
142
+ res.push(output);
143
+ }
144
+ }
84
145
  }
85
- // both value and arg1 are strings
86
- if (isStringValue(arg1) && isStringValue(value)) {
87
- return arg1.indexOf(value) !== -1;
146
+ return res;
147
+ }
148
+ function valueAtPath(array, path) {
149
+ return getCurrent(array, path)[last(path)];
150
+ }
151
+ function setValueAtPath(array, path, value) {
152
+ var current = getCurrent(array, path);
153
+ current[last(path)] = value;
154
+ return array;
155
+ }
156
+ function flatten(values) {
157
+ return asArray(values).reduce(function (acc, value) {
158
+ if (isArray(value)) {
159
+ return acc.concat(flatten(value));
160
+ }
161
+ return asArray(acc).concat(value);
162
+ }, []);
163
+ }
164
+ function getCurrent(array, path) {
165
+ var current = array;
166
+ for (var _i = 0, _a = path.slice(0, -1); _i < _a.length; _i++) {
167
+ var p = _a[_i];
168
+ current[p] = defaultTo(current[p], []);
169
+ current = current[p];
88
170
  }
89
- return false;
171
+ return current;
90
172
  }
91
- var notInside = bindNot(inside);
92
173
 
93
- function lessThan(value, lt) {
94
- return isNumeric(value) && isNumeric(lt) && Number(value) < Number(lt);
174
+ function callEach(arr) {
175
+ return arr.forEach(function (fn) { return fn(); });
95
176
  }
96
177
 
97
- function lessThanOrEquals(value, lte) {
98
- return numberEquals(value, lte) || lessThan(value, lte);
178
+ /**
179
+ * A safe hasOwnProperty access
180
+ */
181
+ function hasOwnProperty(obj, key) {
182
+ return Object.prototype.hasOwnProperty.call(obj, key);
99
183
  }
100
184
 
101
- function isBetween(value, min, max) {
102
- return greaterThanOrEquals(value, min) && lessThanOrEquals(value, max);
185
+ function isPromise(value) {
186
+ return value && isFunction(value.then);
103
187
  }
104
- var isNotBetween = bindNot(isBetween);
105
188
 
106
- function isBlank(value) {
107
- return isNullish(value) || (isStringValue(value) && !value.trim());
189
+ var assign = Object.assign;
190
+
191
+ function invariant(condition,
192
+ // eslint-disable-next-line @typescript-eslint/ban-types
193
+ message) {
194
+ if (condition) {
195
+ return;
196
+ }
197
+ // If message is a string object (rather than string literal)
198
+ // Throw the value directly as a string
199
+ // Alternatively, throw an error with the message
200
+ throw message instanceof String
201
+ ? message.valueOf()
202
+ : new Error(message ? optionalFunctionValue(message) : message);
203
+ }
204
+ // eslint-disable-next-line @typescript-eslint/ban-types
205
+ function StringObject(value) {
206
+ return new String(optionalFunctionValue(value));
207
+ }
208
+
209
+ function isStringValue(v) {
210
+ return String(v) === v;
211
+ }
212
+
213
+ function either(a, b) {
214
+ return !!a !== !!b;
108
215
  }
109
- var isNotBlank = bindNot(isBlank);
110
216
 
111
217
  function isBoolean(value) {
112
218
  return !!value === value;
113
219
  }
114
220
 
115
- var isNotBoolean = bindNot(isBoolean);
221
+ function deferThrow(message) {
222
+ setTimeout(function () {
223
+ throw new Error(message);
224
+ }, 0);
225
+ }
226
+
227
+ function createBus() {
228
+ var listeners = {};
229
+ return {
230
+ emit: function (event, data) {
231
+ listener(event).forEach(function (handler) {
232
+ handler(data);
233
+ });
234
+ },
235
+ on: function (event, handler) {
236
+ listeners[event] = listener(event).concat(handler);
237
+ return {
238
+ off: function () {
239
+ listeners[event] = listener(event).filter(function (h) { return h !== handler; });
240
+ }
241
+ };
242
+ }
243
+ };
244
+ function listener(event) {
245
+ return listeners[event] || [];
246
+ }
247
+ }
116
248
 
117
249
  /**
118
- * A safe hasOwnProperty access
250
+ * @returns a unique numeric id.
119
251
  */
120
- function hasOwnProperty(obj, key) {
121
- return Object.prototype.hasOwnProperty.call(obj, key);
122
- }
252
+ var seq = (function (n) { return function () {
253
+ return "".concat(n++);
254
+ }; })(0);
123
255
 
124
- function lengthEquals(value, arg1) {
125
- return numberEquals(value.length, arg1);
256
+ function mapFirst(array, callback) {
257
+ var broke = false;
258
+ var breakoutValue = null;
259
+ for (var i = 0; i < array.length; i++) {
260
+ callback(array[i], breakout, i);
261
+ if (broke) {
262
+ return breakoutValue;
263
+ }
264
+ }
265
+ function breakout(conditional, value) {
266
+ if (conditional) {
267
+ broke = true;
268
+ breakoutValue = value;
269
+ }
270
+ }
126
271
  }
127
- var lengthNotEquals = bindNot(lengthEquals);
128
272
 
129
273
  function isEmpty(value) {
130
274
  if (!value) {
@@ -140,6 +284,56 @@
140
284
  }
141
285
  var isNotEmpty = bindNot(isEmpty);
142
286
 
287
+ function isPositive(value) {
288
+ return greaterThan(value, 0);
289
+ }
290
+
291
+ function endsWith(value, arg1) {
292
+ return isStringValue(value) && isStringValue(arg1) && value.endsWith(arg1);
293
+ }
294
+ var doesNotEndWith = bindNot(endsWith);
295
+
296
+ function equals(value, arg1) {
297
+ return value === arg1;
298
+ }
299
+ var notEquals = bindNot(equals);
300
+
301
+ function greaterThanOrEquals(value, gte) {
302
+ return numberEquals(value, gte) || greaterThan(value, gte);
303
+ }
304
+
305
+ function inside(value, arg1) {
306
+ if (isArray(arg1)) {
307
+ return arg1.indexOf(value) !== -1;
308
+ }
309
+ // both value and arg1 are strings
310
+ if (isStringValue(arg1) && isStringValue(value)) {
311
+ return arg1.indexOf(value) !== -1;
312
+ }
313
+ return false;
314
+ }
315
+ var notInside = bindNot(inside);
316
+
317
+ function lessThan(value, lt) {
318
+ return isNumeric(value) && isNumeric(lt) && Number(value) < Number(lt);
319
+ }
320
+
321
+ function lessThanOrEquals(value, lte) {
322
+ return numberEquals(value, lte) || lessThan(value, lte);
323
+ }
324
+
325
+ function isBetween(value, min, max) {
326
+ return greaterThanOrEquals(value, min) && lessThanOrEquals(value, max);
327
+ }
328
+ var isNotBetween = bindNot(isBetween);
329
+
330
+ function isBlank(value) {
331
+ return isNullish(value) || (isStringValue(value) && !value.trim());
332
+ }
333
+ var isNotBlank = bindNot(isBlank);
334
+
335
+ var isNotBoolean = bindNot(isBoolean);
336
+
143
337
  /**
144
338
  * Validates that a given value is an even number
145
339
  */
@@ -179,10 +373,6 @@
179
373
  return false;
180
374
  };
181
375
 
182
- function isPositive(value) {
183
- return greaterThan(value, 0);
184
- }
185
-
186
376
  var isNotString = bindNot(isStringValue);
187
377
 
188
378
  function isTruthy(value) {
@@ -203,10 +393,6 @@
203
393
  }
204
394
  var isNotValueOf = bindNot(isValueOf);
205
395
 
206
- function longerThan(value, arg1) {
207
- return greaterThan(value.length, arg1);
208
- }
209
-
210
396
  function longerThanOrEquals(value, arg1) {
211
397
  return greaterThanOrEquals(value.length, arg1);
212
398
  }
@@ -318,46 +504,15 @@
318
504
  return baseRules[ruleName];
319
505
  }
320
506
 
321
- function eachEnforceRule(action) {
322
- for (var ruleName in baseRules) {
323
- var ruleFn = getRule(ruleName);
324
- if (isFunction(ruleFn)) {
325
- action(ruleName, ruleFn);
326
- }
327
- }
328
- }
329
-
330
- function optionalFunctionValue(value) {
331
- var args = [];
332
- for (var _i = 1; _i < arguments.length; _i++) {
333
- args[_i - 1] = arguments[_i];
334
- }
335
- return isFunction(value) ? value.apply(void 0, args) : value;
336
- }
337
-
338
- function defaultTo(value, defaultValue) {
339
- var _a;
340
- return (_a = optionalFunctionValue(value)) !== null && _a !== void 0 ? _a : optionalFunctionValue(defaultValue);
341
- }
342
-
343
- function invariant(condition,
344
- // eslint-disable-next-line @typescript-eslint/ban-types
345
- message) {
346
- if (condition) {
347
- return;
348
- }
349
- // If message is a string object (rather than string literal)
350
- // Throw the value directly as a string
351
- // Alternatively, throw an error with the message
352
- throw message instanceof String
353
- ? message.valueOf()
354
- : new Error(message ? optionalFunctionValue(message) : message);
355
- }
356
- // eslint-disable-next-line @typescript-eslint/ban-types
357
- function StringObject(value) {
358
- return new String(optionalFunctionValue(value));
359
- }
360
-
507
+ function eachEnforceRule(action) {
508
+ for (var ruleName in baseRules) {
509
+ var ruleFn = getRule(ruleName);
510
+ if (isFunction(ruleFn)) {
511
+ action(ruleName, ruleFn);
512
+ }
513
+ }
514
+ }
515
+
361
516
  // eslint-disable-next-line max-lines-per-function
362
517
  function createContext(init) {
363
518
  var storage = { ancestry: [] };
@@ -512,30 +667,47 @@
512
667
 
513
668
  function enforceEager(value) {
514
669
  var target = {};
670
+ // This condition is for when we don't have proxy support (ES5).
671
+ // In this case, we need to manually assign the rules to the target object on runtime.
672
+ // The follow up proxy block is used in case we do have proxy support, and we can assign each rule upon invocation.
515
673
  if (!isProxySupported()) {
674
+ // We iterate over each of the rules, and add them to the target object being return by enforce
516
675
  eachEnforceRule(function (ruleName, ruleFn) {
676
+ // We then wrap the rule with `genRuleCall` that adds the base enforce behavior
517
677
  target[ruleName] = genRuleCall(target, ruleFn, ruleName);
518
678
  });
519
679
  return target;
520
680
  }
681
+ // We create a proxy intercepting access to the target object (which is empty).
521
682
  var proxy = new Proxy(target, {
522
683
  get: function (_, ruleName) {
684
+ // On property access, we identify if it is a rule or not.
523
685
  var rule = getRule(ruleName);
686
+ // If it is a rule, we wrap it with `genRuleCall` that adds the base enforce behavior
524
687
  if (rule) {
525
688
  return genRuleCall(proxy, rule, ruleName);
526
689
  }
527
690
  }
528
691
  });
529
692
  return proxy;
693
+ // This function is used to wrap a rule with the base enforce behavior
694
+ // It takes the target object, the rule function, and the rule name
695
+ // It then returns the rule, in a manner that can be used by enforce
530
696
  function genRuleCall(target, rule, ruleName) {
531
697
  return function ruleCall() {
532
698
  var args = [];
533
699
  for (var _i = 0; _i < arguments.length; _i++) {
534
700
  args[_i] = arguments[_i];
535
701
  }
702
+ // Order of operation:
703
+ // 1. Create a context with the value being enforced
704
+ // 2. Call the rule within the context, and pass over the arguments passed to it
705
+ // 3. Transform the result to the correct output format
536
706
  var transformedResult = ctx.run({ value: value }, function () {
537
707
  return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args, false)), ruleName, value], args, false));
538
708
  });
709
+ // On rule failure (the result is false), we either throw an error
710
+ // or throw a string value if the rule has a message defined in it.
539
711
  invariant(transformedResult.pass, isNullish(transformedResult.message)
540
712
  ? "enforce/".concat(ruleName, " failed with ").concat(JSON.stringify(value))
541
713
  : StringObject(transformedResult.message));
@@ -544,23 +716,6 @@
544
716
  }
545
717
  }
546
718
 
547
- function mapFirst(array, callback) {
548
- var broke = false;
549
- var breakoutValue = null;
550
- for (var i = 0; i < array.length; i++) {
551
- callback(array[i], breakout, i);
552
- if (broke) {
553
- return breakoutValue;
554
- }
555
- }
556
- function breakout(conditional, value) {
557
- if (conditional) {
558
- broke = true;
559
- breakoutValue = value;
560
- }
561
- }
562
- }
563
-
564
719
  // eslint-disable-next-line max-lines-per-function
565
720
  function genEnforceLazy(key) {
566
721
  var registeredRules = [];
@@ -674,109 +829,11 @@
674
829
  }
675
830
  var enforce = genEnforce();
676
831
 
677
- /**
678
- * @returns a unique numeric id.
679
- */
680
- var genId = (function (n) { return function () {
681
- return "".concat(n++);
682
- }; })(0);
683
-
684
832
  function shouldUseErrorAsMessage(message, error) {
685
833
  // kind of cheating with this safe guard, but it does the job
686
834
  return isUndefined(message) && isStringValue(error);
687
835
  }
688
836
 
689
- function asArray(possibleArg) {
690
- return [].concat(possibleArg);
691
- }
692
-
693
- /**
694
- * Creates a cache function
695
- */
696
- function createCache(maxSize) {
697
- if (maxSize === void 0) { maxSize = 1; }
698
- var cacheStorage = [];
699
- var cache = function (deps, cacheAction) {
700
- var cacheHit = cache.get(deps);
701
- // cache hit is not null
702
- if (cacheHit)
703
- return cacheHit[1];
704
- var result = cacheAction();
705
- cacheStorage.unshift([deps.concat(), result]);
706
- if (longerThan(cacheStorage, maxSize))
707
- cacheStorage.length = maxSize;
708
- return result;
709
- };
710
- // invalidate an item in the cache by its dependencies
711
- cache.invalidate = function (deps) {
712
- var index = findIndex(deps);
713
- if (index > -1)
714
- cacheStorage.splice(index, 1);
715
- };
716
- // Retrieves an item from the cache.
717
- cache.get = function (deps) {
718
- return cacheStorage[findIndex(deps)] || null;
719
- };
720
- return cache;
721
- function findIndex(deps) {
722
- return cacheStorage.findIndex(function (_a) {
723
- var cachedDeps = _a[0];
724
- return lengthEquals(deps, cachedDeps.length) &&
725
- deps.every(function (dep, i) { return dep === cachedDeps[i]; });
726
- });
727
- }
728
- }
729
-
730
- function last(values) {
731
- var valuesArray = asArray(values);
732
- return valuesArray[valuesArray.length - 1];
733
- }
734
-
735
- // This is kind of a map/filter in one function.
736
- // Normally, behaves like a nested-array map,
737
- // but returning `null` will drop the element from the array
738
- function transform(array, cb) {
739
- var res = [];
740
- for (var _i = 0, array_1 = array; _i < array_1.length; _i++) {
741
- var v = array_1[_i];
742
- if (isArray(v)) {
743
- res.push(transform(v, cb));
744
- }
745
- else {
746
- var output = cb(v);
747
- if (isNotNull(output)) {
748
- res.push(output);
749
- }
750
- }
751
- }
752
- return res;
753
- }
754
- function valueAtPath(array, path) {
755
- return getCurrent(array, path)[last(path)];
756
- }
757
- function setValueAtPath(array, path, value) {
758
- var current = getCurrent(array, path);
759
- current[last(path)] = value;
760
- return array;
761
- }
762
- function flatten(values) {
763
- return asArray(values).reduce(function (acc, value) {
764
- if (isArray(value)) {
765
- return acc.concat(flatten(value));
766
- }
767
- return asArray(acc).concat(value);
768
- }, []);
769
- }
770
- function getCurrent(array, path) {
771
- var current = array;
772
- for (var _i = 0, _a = path.slice(0, -1); _i < _a.length; _i++) {
773
- var p = _a[_i];
774
- current[p] = defaultTo(current[p], []);
775
- current = current[p];
776
- }
777
- return current;
778
- }
779
-
780
837
  var IsolateTypes;
781
838
  (function (IsolateTypes) {
782
839
  IsolateTypes[IsolateTypes["DEFAULT"] = 0] = "DEFAULT";
@@ -831,7 +888,7 @@
831
888
  var context = createContext(function (ctxRef, parentContext) {
832
889
  return parentContext
833
890
  ? null
834
- : assign({}, {
891
+ : assign({
835
892
  exclusion: {
836
893
  tests: {},
837
894
  groups: {}
@@ -922,7 +979,7 @@
922
979
  function VestTest(fieldName, testFn, _a) {
923
980
  var _b = _a === void 0 ? {} : _a, message = _b.message, groupName = _b.groupName, key = _b.key;
924
981
  this.key = null;
925
- this.id = genId();
982
+ this.id = seq();
926
983
  this.severity = TestSeverity.Error;
927
984
  this.status = STATUS_UNTESTED;
928
985
  this.fieldName = fieldName;
@@ -1140,10 +1197,24 @@
1140
1197
  };
1141
1198
  }
1142
1199
 
1143
- function deferThrow(message) {
1144
- setTimeout(function () {
1145
- throw new Error(message);
1146
- }, 0);
1200
+ /**
1201
+ * @returns {Isolate} The current isolate layer
1202
+ */
1203
+ function useIsolate() {
1204
+ return context.useX().isolate;
1205
+ }
1206
+ /**
1207
+ * @returns {number[]} The current cursor path of the isolate tree
1208
+ */
1209
+ function useCurrentPath() {
1210
+ var isolate = useIsolate();
1211
+ return isolate.path.concat(isolate.cursor.current());
1212
+ }
1213
+ /**
1214
+ * @returns {IsolateCursor} The cursor object for the current isolate
1215
+ */
1216
+ function useCursor() {
1217
+ return useIsolate().cursor;
1147
1218
  }
1148
1219
 
1149
1220
  function usePrevKeys() {
@@ -1188,31 +1259,12 @@
1188
1259
  useCursor().next();
1189
1260
  return output;
1190
1261
  }
1191
- /**
1192
- * @returns {Isolate} The current isolate layer
1193
- */
1194
- function useIsolate() {
1195
- return context.useX().isolate;
1196
- }
1197
1262
  /**
1198
1263
  * @returns {boolean} Whether or not the current isolate allows tests to be reordered
1199
1264
  */
1200
1265
  function shouldAllowReorder() {
1201
1266
  return useIsolate().type === IsolateTypes.EACH;
1202
1267
  }
1203
- /**
1204
- * @returns {number[]} The current cursor path of the isolate tree
1205
- */
1206
- function useCurrentPath() {
1207
- var isolate = useIsolate();
1208
- return isolate.path.concat(isolate.cursor.current());
1209
- }
1210
- /**
1211
- * @returns {IsolateCursor} The cursor object for the current isolate
1212
- */
1213
- function useCursor() {
1214
- return useIsolate().cursor;
1215
- }
1216
1268
 
1217
1269
  var Severity;
1218
1270
  (function (Severity) {
@@ -1242,10 +1294,6 @@
1242
1294
  return testObject.groupName === groupName;
1243
1295
  }
1244
1296
 
1245
- function either(a, b) {
1246
- return !!a !== !!b;
1247
- }
1248
-
1249
1297
  /**
1250
1298
  * Checks that a given test object matches the currently specified severity level
1251
1299
  */
@@ -1376,11 +1424,6 @@
1376
1424
  testObject.isOmitted());
1377
1425
  }
1378
1426
 
1379
- function useSummary() {
1380
- var summary = context.useX().summary;
1381
- invariant(summary);
1382
- return summary;
1383
- }
1384
1427
  /**
1385
1428
  * Reads the testObjects list and gets full validation result from it.
1386
1429
  */
@@ -1492,57 +1535,85 @@
1492
1535
  return output;
1493
1536
  }
1494
1537
 
1495
- function getErrors(fieldName) {
1496
- return getFailures(Severity.ERRORS, fieldName);
1497
- }
1498
- function getWarnings(fieldName) {
1499
- return getFailures(Severity.WARNINGS, fieldName);
1538
+ // eslint-disable-next-line max-lines-per-function, max-statements
1539
+ function suiteSelectors(summary) {
1540
+ var selectors = {
1541
+ getErrors: getErrors,
1542
+ getErrorsByGroup: getErrorsByGroup,
1543
+ getWarnings: getWarnings,
1544
+ getWarningsByGroup: getWarningsByGroup,
1545
+ hasErrors: hasErrors,
1546
+ hasErrorsByGroup: hasErrorsByGroup,
1547
+ hasWarnings: hasWarnings,
1548
+ hasWarningsByGroup: hasWarningsByGroup,
1549
+ isValid: isValid,
1550
+ isValidByGroup: isValidByGroup
1551
+ };
1552
+ return selectors;
1553
+ // Booleans
1554
+ function isValid(fieldName) {
1555
+ var _a;
1556
+ return fieldName ? Boolean((_a = summary.tests[fieldName]) === null || _a === void 0 ? void 0 : _a.valid) : summary.valid;
1557
+ }
1558
+ function isValidByGroup(groupName, fieldName) {
1559
+ var group = summary.groups[groupName];
1560
+ if (!group) {
1561
+ return false;
1562
+ }
1563
+ if (fieldName) {
1564
+ return isFieldValid(group, fieldName);
1565
+ }
1566
+ for (var fieldName_1 in group) {
1567
+ if (!isFieldValid(group, fieldName_1)) {
1568
+ return false;
1569
+ }
1570
+ }
1571
+ return true;
1572
+ }
1573
+ function hasWarnings(fieldName) {
1574
+ return hasFailures(summary, SeverityCount.WARN_COUNT, fieldName);
1575
+ }
1576
+ function hasErrors(fieldName) {
1577
+ return hasFailures(summary, SeverityCount.ERROR_COUNT, fieldName);
1578
+ }
1579
+ function hasWarningsByGroup(groupName, fieldName) {
1580
+ return hasFailuresByGroup(summary, SeverityCount.WARN_COUNT, groupName, fieldName);
1581
+ }
1582
+ function hasErrorsByGroup(groupName, fieldName) {
1583
+ return hasFailuresByGroup(summary, SeverityCount.ERROR_COUNT, groupName, fieldName);
1584
+ }
1585
+ function getWarnings(fieldName) {
1586
+ return getFailures(summary, Severity.WARNINGS, fieldName);
1587
+ }
1588
+ function getErrors(fieldName) {
1589
+ return getFailures(summary, Severity.ERRORS, fieldName);
1590
+ }
1591
+ function getErrorsByGroup(groupName, fieldName) {
1592
+ return getFailuresByGroup(summary, Severity.ERRORS, groupName, fieldName);
1593
+ }
1594
+ function getWarningsByGroup(groupName, fieldName) {
1595
+ return getFailuresByGroup(summary, Severity.WARNINGS, groupName, fieldName);
1596
+ }
1500
1597
  }
1501
- /**
1502
- * @returns suite or field's errors or warnings.
1503
- */
1504
- function getFailures(severityKey, fieldName) {
1505
- var summary = useSummary();
1598
+ // Gathers all failures of a given severity
1599
+ // With a fieldName, it will only gather failures for that field
1600
+ function getFailures(summary, severityKey, fieldName) {
1506
1601
  return gatherFailures(summary.tests, severityKey, fieldName);
1507
1602
  }
1508
-
1509
- function getErrorsByGroup(groupName, fieldName) {
1510
- return getFailuresByGroup(groupName, Severity.ERRORS, fieldName);
1511
- }
1512
- function getWarningsByGroup(groupName, fieldName) {
1513
- return getFailuresByGroup(groupName, Severity.WARNINGS, fieldName);
1514
- }
1515
- function getFailuresByGroup(groupName, severityKey, fieldName) {
1516
- var summary = useSummary();
1603
+ // Gathers all failures of a given severity within a group
1604
+ // With a fieldName, it will only gather failures for that field
1605
+ function getFailuresByGroup(summary, severityKey, groupName, fieldName) {
1517
1606
  return gatherFailures(summary.groups[groupName], severityKey, fieldName);
1518
1607
  }
1519
-
1520
- function hasErrors(fieldName) {
1521
- return hasFailures(SeverityCount.ERROR_COUNT, fieldName);
1522
- }
1523
- function hasWarnings(fieldName) {
1524
- return hasFailures(SeverityCount.WARN_COUNT, fieldName);
1525
- }
1526
- function hasFailures(severityCount, fieldName) {
1608
+ // Checks if a field is valid within a container object - can be within a group or top level
1609
+ function isFieldValid(testContainer, fieldName) {
1527
1610
  var _a;
1528
- var summary = useSummary();
1529
- if (fieldName) {
1530
- return isPositive((_a = summary.tests[fieldName]) === null || _a === void 0 ? void 0 : _a[severityCount]);
1531
- }
1532
- return isPositive(summary[severityCount]);
1533
- }
1534
-
1535
- function hasErrorsByGroup(groupName, fieldName) {
1536
- return hasFailuresByGroup(Severity.ERRORS, groupName, fieldName);
1537
- }
1538
- function hasWarningsByGroup(groupName, fieldName) {
1539
- return hasFailuresByGroup(Severity.WARNINGS, groupName, fieldName);
1611
+ return !!((_a = testContainer[fieldName]) === null || _a === void 0 ? void 0 : _a.valid);
1540
1612
  }
1541
- // eslint-disable-next-line max-statements
1542
- function hasFailuresByGroup(severityKey, groupName, fieldName) {
1613
+ // Checks if a there are any failures of a given severity within a group
1614
+ // If a fieldName is provided, it will only check for failures within that field
1615
+ function hasFailuresByGroup(summary, severityCount, groupName, fieldName) {
1543
1616
  var _a, _b;
1544
- var summary = useSummary();
1545
- var severityCount = countKeyBySeverity(severityKey);
1546
1617
  var group = summary.groups[groupName];
1547
1618
  if (!group) {
1548
1619
  return false;
@@ -1557,32 +1628,14 @@
1557
1628
  }
1558
1629
  return false;
1559
1630
  }
1560
-
1561
- function isValid(fieldName) {
1562
- var summary = useSummary();
1563
- return fieldName
1564
- ? Boolean(isFieldValid(summary.tests, fieldName))
1565
- : summary.valid;
1566
- }
1567
- function isValidByGroup(groupName, fieldName) {
1568
- var summary = useSummary();
1569
- var group = summary.groups[groupName];
1570
- if (!group) {
1571
- return false;
1572
- }
1573
- if (fieldName) {
1574
- return isFieldValid(group, fieldName);
1575
- }
1576
- for (var fieldName_1 in group) {
1577
- if (!isFieldValid(group, fieldName_1)) {
1578
- return false;
1579
- }
1580
- }
1581
- return true;
1582
- }
1583
- function isFieldValid(testContainer, fieldName) {
1631
+ // Checks if there are any failures of a given severity
1632
+ // If a fieldName is provided, it will only check for failures within that field
1633
+ function hasFailures(summary, countKey, fieldName) {
1584
1634
  var _a;
1585
- return !!((_a = testContainer[fieldName]) === null || _a === void 0 ? void 0 : _a.valid);
1635
+ var failureCount = fieldName
1636
+ ? (_a = summary.tests[fieldName]) === null || _a === void 0 ? void 0 : _a[countKey]
1637
+ : summary[countKey] || 0;
1638
+ return isPositive(failureCount);
1586
1639
  }
1587
1640
 
1588
1641
  var cache$1 = createCache(1);
@@ -1592,18 +1645,7 @@
1592
1645
  return cache$1([testObjects], context.bind(ctxRef, function () {
1593
1646
  var summary = genTestsSummary();
1594
1647
  var suiteName = useSuiteName();
1595
- var ref = { summary: summary };
1596
- return assign(summary, {
1597
- getErrors: context.bind(ref, getErrors),
1598
- getErrorsByGroup: context.bind(ref, getErrorsByGroup),
1599
- getWarnings: context.bind(ref, getWarnings),
1600
- getWarningsByGroup: context.bind(ref, getWarningsByGroup),
1601
- hasErrors: context.bind(ref, hasErrors),
1602
- hasErrorsByGroup: context.bind(ref, hasErrorsByGroup),
1603
- hasWarnings: context.bind(ref, hasWarnings),
1604
- hasWarningsByGroup: context.bind(ref, hasWarningsByGroup),
1605
- isValid: context.bind(ref, isValid),
1606
- isValidByGroup: context.bind(ref, isValidByGroup),
1648
+ return assign(summary, suiteSelectors(summary), {
1607
1649
  suiteName: suiteName
1608
1650
  });
1609
1651
  }));
@@ -1622,7 +1664,7 @@
1622
1664
  return matchingFieldName(testObject, fieldName);
1623
1665
  });
1624
1666
  }
1625
- return isNotEmpty(allIncomplete);
1667
+ return true;
1626
1668
  }
1627
1669
 
1628
1670
  var cache = createCache(20);
@@ -1639,10 +1681,10 @@
1639
1681
  * DONE is here and not in its own module to prevent circular dependency issues.
1640
1682
  */
1641
1683
  function shouldSkipDoneRegistration(callback, fieldName, output) {
1684
+ var _a;
1642
1685
  // If we do not have any test runs for the current field
1643
1686
  return !!(!isFunction(callback) ||
1644
- (fieldName &&
1645
- (!output.tests[fieldName] || isEmpty(output.tests[fieldName].testCount))));
1687
+ (fieldName && numberEquals((_a = output.tests[fieldName]) === null || _a === void 0 ? void 0 : _a.testCount, 0)));
1646
1688
  }
1647
1689
  function shouldRunDoneCallback(fieldName) {
1648
1690
  // is suite finished || field name exists, and test is finished;
@@ -1684,28 +1726,6 @@
1684
1726
  });
1685
1727
  }
1686
1728
 
1687
- function createBus() {
1688
- var listeners = {};
1689
- return {
1690
- emit: function (event, data) {
1691
- listener(event).forEach(function (handler) {
1692
- handler(data);
1693
- });
1694
- },
1695
- on: function (event, handler) {
1696
- listeners[event] = listener(event).concat(handler);
1697
- return {
1698
- off: function () {
1699
- listeners[event] = listener(event).filter(function (h) { return h !== handler; });
1700
- }
1701
- };
1702
- }
1703
- };
1704
- function listener(event) {
1705
- return listeners[event] || [];
1706
- }
1707
- }
1708
-
1709
1729
  function omitOptionalFields() {
1710
1730
  var optionalFields = useOptionalFields()[0];
1711
1731
  if (isEmpty(optionalFields)) {
@@ -1748,10 +1768,6 @@
1748
1768
  });
1749
1769
  }
1750
1770
 
1751
- function callEach(arr) {
1752
- return arr.forEach(function (fn) { return fn(); });
1753
- }
1754
-
1755
1771
  /**
1756
1772
  * Runs done callback per field when async tests are finished running.
1757
1773
  */
@@ -1773,10 +1789,10 @@
1773
1789
 
1774
1790
  // eslint-disable-next-line max-lines-per-function
1775
1791
  function initBus() {
1776
- var bus = createBus();
1792
+ var vestBus = createBus();
1777
1793
  // Report a the completion of a test. There may be other tests with the same
1778
1794
  // name that are still running, or not yet started.
1779
- bus.on(Events.TEST_COMPLETED, function (testObject) {
1795
+ vestBus.on(Events.TEST_COMPLETED, function (testObject) {
1780
1796
  if (testObject.isCanceled()) {
1781
1797
  return;
1782
1798
  }
@@ -1784,21 +1800,21 @@
1784
1800
  runFieldCallbacks(testObject.fieldName);
1785
1801
  if (!hasRemainingTests()) {
1786
1802
  // When no more tests are running, emit the done event
1787
- bus.emit(Events.ALL_RUNNING_TESTS_FINISHED);
1803
+ vestBus.emit(Events.ALL_RUNNING_TESTS_FINISHED);
1788
1804
  }
1789
1805
  });
1790
1806
  // Report that the suite completed its synchronous test run.
1791
1807
  // Async operations may still be running.
1792
- bus.on(Events.SUITE_CALLBACK_DONE_RUNNING, function () {
1808
+ vestBus.on(Events.SUITE_CALLBACK_DONE_RUNNING, function () {
1793
1809
  // Remove tests that are optional and need to be omitted
1794
1810
  omitOptionalFields();
1795
1811
  });
1796
1812
  // Called when all the tests, including async, are done running
1797
- bus.on(Events.ALL_RUNNING_TESTS_FINISHED, function () {
1813
+ vestBus.on(Events.ALL_RUNNING_TESTS_FINISHED, function () {
1798
1814
  runDoneCallbacks();
1799
1815
  });
1800
1816
  // Removes a certain field from the state.
1801
- bus.on(Events.REMOVE_FIELD, function (fieldName) {
1817
+ vestBus.on(Events.REMOVE_FIELD, function (fieldName) {
1802
1818
  useEachTestObject(function (testObject) {
1803
1819
  if (matchingFieldName(testObject, fieldName)) {
1804
1820
  testObject.cancel();
@@ -1807,14 +1823,14 @@
1807
1823
  });
1808
1824
  });
1809
1825
  // Resets a certain field in the state.
1810
- bus.on(Events.RESET_FIELD, function (fieldName) {
1826
+ vestBus.on(Events.RESET_FIELD, function (fieldName) {
1811
1827
  useEachTestObject(function (testObject) {
1812
1828
  if (matchingFieldName(testObject, fieldName)) {
1813
1829
  testObject.reset();
1814
1830
  }
1815
1831
  });
1816
1832
  });
1817
- return bus;
1833
+ return vestBus;
1818
1834
  }
1819
1835
  function useBus() {
1820
1836
  var context$1 = context.useX();
@@ -1843,7 +1859,7 @@
1843
1859
  // State initialization
1844
1860
  var state = createState();
1845
1861
  // State reference - this holds the actual state values
1846
- var stateRef = createStateRef(state, { suiteId: genId(), suiteName: suiteName });
1862
+ var stateRef = createStateRef(state, { suiteId: seq(), suiteName: suiteName });
1847
1863
  // Create base context reference. All hooks will derive their data from this
1848
1864
  var ctxRef = { stateRef: stateRef, bus: bus };
1849
1865
  var suite = assign(
@@ -1938,10 +1954,10 @@
1938
1954
  * only('username');
1939
1955
  */
1940
1956
  function only(item) {
1941
- return addTo(0 /* ONLY */, 'tests', item);
1957
+ return addTo(0 /* ExclusionGroup.ONLY */, 'tests', item);
1942
1958
  }
1943
1959
  only.group = function (item) {
1944
- return addTo(0 /* ONLY */, 'groups', item);
1960
+ return addTo(0 /* ExclusionGroup.ONLY */, 'groups', item);
1945
1961
  };
1946
1962
  /**
1947
1963
  * Adds a field or a list of fields into the exclusion list
@@ -1951,13 +1967,13 @@
1951
1967
  * skip('username');
1952
1968
  */
1953
1969
  function skip(item) {
1954
- return addTo(1 /* SKIP */, 'tests', item);
1970
+ return addTo(1 /* ExclusionGroup.SKIP */, 'tests', item);
1955
1971
  }
1956
1972
  skip.group = function (item) {
1957
- return addTo(1 /* SKIP */, 'groups', item);
1973
+ return addTo(1 /* ExclusionGroup.SKIP */, 'groups', item);
1958
1974
  };
1959
1975
  //Checks whether a certain test profile excluded by any of the exclusion groups.
1960
- // eslint-disable-next-line complexity, max-statements, max-lines-per-function
1976
+ // eslint-disable-next-line complexity, max-statements
1961
1977
  function isExcluded(testObject) {
1962
1978
  var fieldName = testObject.fieldName, groupName = testObject.groupName;
1963
1979
  if (isExcludedIndividually())
@@ -2033,7 +2049,7 @@
2033
2049
  return;
2034
2050
  }
2035
2051
  context$1.exclusion[itemType][itemName] =
2036
- exclusionGroup === 0 /* ONLY */;
2052
+ exclusionGroup === 0 /* ExclusionGroup.ONLY */;
2037
2053
  });
2038
2054
  }
2039
2055
  /**
@@ -2199,10 +2215,6 @@
2199
2215
  }
2200
2216
  }
2201
2217
 
2202
- function isPromise(value) {
2203
- return value && isFunction(value.then);
2204
- }
2205
-
2206
2218
  function isSameProfileTest(testObject1, testObject2) {
2207
2219
  return (testObject1.fieldName === testObject2.fieldName &&
2208
2220
  testObject1.groupName === testObject2.groupName);
@@ -2396,7 +2408,6 @@
2396
2408
  }
2397
2409
 
2398
2410
  /* eslint-disable jest/valid-title */
2399
- // eslint-disable-next-line max-lines-per-function
2400
2411
  function bindTestMemo(test) {
2401
2412
  var cache = createCache(10); // arbitrary cache size
2402
2413
  // eslint-disable-next-line max-statements
@@ -2467,7 +2478,7 @@
2467
2478
  ctx.currentTest.warn();
2468
2479
  }
2469
2480
 
2470
- var VERSION = "4.4.2";
2481
+ var VERSION = "4.5.0";
2471
2482
 
2472
2483
  exports.VERSION = VERSION;
2473
2484
  exports.context = context;
@@ -2482,6 +2493,7 @@
2482
2493
  exports.optional = optional;
2483
2494
  exports.skip = skip;
2484
2495
  exports.skipWhen = skipWhen;
2496
+ exports.suiteSelectors = suiteSelectors;
2485
2497
  exports.test = test;
2486
2498
  exports.warn = warn;
2487
2499