@orioro/util 0.2.0 → 0.4.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 (198) hide show
  1. package/dist/PromiseLikeEventEmitter/index.d.ts +10 -0
  2. package/dist/array/arrayChunk.d.ts +1 -0
  3. package/dist/array/index.d.ts +1 -0
  4. package/dist/debug/debugFn/index.d.ts +15 -0
  5. package/dist/debug/debugFn/util.d.ts +1 -0
  6. package/dist/debug/debugFn.d.ts +15 -0
  7. package/dist/debug/deepFreeze.d.ts +5 -0
  8. package/dist/debug/index.d.ts +3 -0
  9. package/dist/debug/wait.d.ts +1 -0
  10. package/dist/index.d.ts +9 -0
  11. package/dist/index.mjs +21 -14
  12. package/dist/interpolate/index.d.ts +14 -0
  13. package/dist/maybeFn.d.ts +1 -0
  14. package/dist/pickPaths/index.d.ts +4 -0
  15. package/dist/promise/batchFn.d.ts +49 -0
  16. package/dist/promise/index.d.ts +3 -0
  17. package/dist/promise/promiseReduce.d.ts +1 -0
  18. package/dist/promise/resolveNestedPromises.d.ts +14 -0
  19. package/dist/promise/types.d.ts +2 -0
  20. package/dist/resolvePaths/index.d.ts +4 -0
  21. package/dist/switchValue.d.ts +9 -0
  22. package/dist/typeOf.d.ts +7 -0
  23. package/dist/validate/async/index.d.ts +18 -0
  24. package/dist/validate/async/validateAsyncFn.d.ts +2 -0
  25. package/dist/validate/async/validators/index.d.ts +2 -0
  26. package/dist/validate/async/validators/logical.d.ts +4 -0
  27. package/dist/validate/async/validators/shape.d.ts +7 -0
  28. package/dist/validate/async/validators/tmpand.d.ts +2 -0
  29. package/dist/validate/async/validators/tmpor.d.ts +2 -0
  30. package/dist/validate/common/ValidationError.d.ts +11 -0
  31. package/dist/validate/common/util/defaultErrorMessage.d.ts +2 -0
  32. package/dist/validate/common/util/index.d.ts +3 -0
  33. package/dist/validate/common/util/parseValidatorInput.d.ts +5 -0
  34. package/dist/validate/common/util/resolveValidationResult.d.ts +6 -0
  35. package/dist/validate/common/validators/index.d.ts +1 -0
  36. package/dist/validate/common/validators/type.d.ts +9 -0
  37. package/dist/validate/index.d.ts +3 -0
  38. package/dist/validate/sync/index.d.ts +18 -0
  39. package/dist/validate/sync/validateSyncFn.d.ts +2 -0
  40. package/dist/validate/sync/validators/index.d.ts +2 -0
  41. package/dist/validate/sync/validators/logical.d.ts +4 -0
  42. package/dist/validate/sync/validators/shape.d.ts +7 -0
  43. package/dist/validate/types/async.d.ts +5 -0
  44. package/dist/validate/types/common.d.ts +27 -0
  45. package/dist/validate/types/index.d.ts +3 -0
  46. package/dist/validate/types/sync.d.ts +5 -0
  47. package/package.json +7 -2
  48. package/babel.config.cjs +0 -13
  49. package/coverage/clover.xml +0 -491
  50. package/coverage/coverage-final.json +0 -29
  51. package/coverage/lcov-report/ValidationError.ts.html +0 -184
  52. package/coverage/lcov-report/base.css +0 -224
  53. package/coverage/lcov-report/block-navigation.js +0 -87
  54. package/coverage/lcov-report/favicon.png +0 -0
  55. package/coverage/lcov-report/index.html +0 -296
  56. package/coverage/lcov-report/prettify.css +0 -1
  57. package/coverage/lcov-report/prettify.js +0 -2
  58. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  59. package/coverage/lcov-report/sorter.js +0 -196
  60. package/coverage/lcov-report/src/debug/deepFreeze.ts.html +0 -157
  61. package/coverage/lcov-report/src/debug/index.html +0 -146
  62. package/coverage/lcov-report/src/debug/index.ts.html +0 -91
  63. package/coverage/lcov-report/src/debug/wait.ts.html +0 -127
  64. package/coverage/lcov-report/src/index.html +0 -131
  65. package/coverage/lcov-report/src/interpolate/index.html +0 -116
  66. package/coverage/lcov-report/src/interpolate/index.ts.html +0 -277
  67. package/coverage/lcov-report/src/maybeFn.ts.html +0 -94
  68. package/coverage/lcov-report/src/promise/index.html +0 -146
  69. package/coverage/lcov-report/src/promise/index.ts.html +0 -91
  70. package/coverage/lcov-report/src/promise/promiseReduce.ts.html +0 -130
  71. package/coverage/lcov-report/src/promise/resolveNestedPromises.ts.html +0 -271
  72. package/coverage/lcov-report/src/switchValue.ts.html +0 -253
  73. package/coverage/lcov-report/src/typeOf.ts.html +0 -328
  74. package/coverage/lcov-report/src/validate/ValidationError.ts.html +0 -184
  75. package/coverage/lcov-report/src/validate/async/index.html +0 -131
  76. package/coverage/lcov-report/src/validate/async/index.ts.html +0 -241
  77. package/coverage/lcov-report/src/validate/async/parseValidator.ts.html +0 -136
  78. package/coverage/lcov-report/src/validate/async/validateAsyncFn.ts.html +0 -208
  79. package/coverage/lcov-report/src/validate/async/validators/and.ts.html +0 -154
  80. package/coverage/lcov-report/src/validate/async/validators/index.html +0 -146
  81. package/coverage/lcov-report/src/validate/async/validators/index.ts.html +0 -91
  82. package/coverage/lcov-report/src/validate/async/validators/logical.ts.html +0 -253
  83. package/coverage/lcov-report/src/validate/async/validators/or.ts.html +0 -151
  84. package/coverage/lcov-report/src/validate/async/validators/shape.ts.html +0 -565
  85. package/coverage/lcov-report/src/validate/common/ValidationError.ts.html +0 -184
  86. package/coverage/lcov-report/src/validate/common/index.html +0 -116
  87. package/coverage/lcov-report/src/validate/common/util/defaultErrorMessage.ts.html +0 -163
  88. package/coverage/lcov-report/src/validate/common/util/index.html +0 -161
  89. package/coverage/lcov-report/src/validate/common/util/index.ts.html +0 -94
  90. package/coverage/lcov-report/src/validate/common/util/parseValidator.ts.html +0 -316
  91. package/coverage/lcov-report/src/validate/common/util/parseValidatorInput.ts.html +0 -316
  92. package/coverage/lcov-report/src/validate/common/util/resolveValidationResult.ts.html +0 -277
  93. package/coverage/lcov-report/src/validate/common/util/validatorParser.ts.html +0 -316
  94. package/coverage/lcov-report/src/validate/common/validators/index.html +0 -131
  95. package/coverage/lcov-report/src/validate/common/validators/index.ts.html +0 -88
  96. package/coverage/lcov-report/src/validate/common/validators/type.ts.html +0 -388
  97. package/coverage/lcov-report/src/validate/fmtValidationResult.ts.html +0 -268
  98. package/coverage/lcov-report/src/validate/index.html +0 -116
  99. package/coverage/lcov-report/src/validate/index.ts.html +0 -94
  100. package/coverage/lcov-report/src/validate/makeValidate.ts.html +0 -634
  101. package/coverage/lcov-report/src/validate/specUtil/commonTests.js.html +0 -1324
  102. package/coverage/lcov-report/src/validate/specUtil/index.html +0 -116
  103. package/coverage/lcov-report/src/validate/sync/index.html +0 -131
  104. package/coverage/lcov-report/src/validate/sync/index.ts.html +0 -244
  105. package/coverage/lcov-report/src/validate/sync/parseValidator.ts.html +0 -136
  106. package/coverage/lcov-report/src/validate/sync/validateSyncFn.ts.html +0 -223
  107. package/coverage/lcov-report/src/validate/sync/validators/and.ts.html +0 -148
  108. package/coverage/lcov-report/src/validate/sync/validators/index.html +0 -146
  109. package/coverage/lcov-report/src/validate/sync/validators/index.ts.html +0 -91
  110. package/coverage/lcov-report/src/validate/sync/validators/logical.ts.html +0 -226
  111. package/coverage/lcov-report/src/validate/sync/validators/or.ts.html +0 -130
  112. package/coverage/lcov-report/src/validate/sync/validators/shape.ts.html +0 -523
  113. package/coverage/lcov-report/src/validate/sync/validators/type.ts.html +0 -154
  114. package/coverage/lcov-report/src/validate/syncValidators/and.ts.html +0 -157
  115. package/coverage/lcov-report/src/validate/syncValidators/index.html +0 -176
  116. package/coverage/lcov-report/src/validate/syncValidators/index.ts.html +0 -97
  117. package/coverage/lcov-report/src/validate/syncValidators/or.ts.html +0 -127
  118. package/coverage/lcov-report/src/validate/syncValidators/shape.ts.html +0 -559
  119. package/coverage/lcov-report/src/validate/syncValidators/string.ts.html +0 -163
  120. package/coverage/lcov-report/src/validate/syncValidators/type.ts.html +0 -154
  121. package/coverage/lcov-report/src/validate/util/defaultErrorMessage.ts.html +0 -169
  122. package/coverage/lcov-report/src/validate/util/index.html +0 -146
  123. package/coverage/lcov-report/src/validate/util/index.ts.html +0 -91
  124. package/coverage/lcov-report/src/validate/util/resolveValidationResult.ts.html +0 -253
  125. package/coverage/lcov-report/src/validate/validate.ts.html +0 -220
  126. package/coverage/lcov-report/src/validate/validateAsync.ts.html +0 -220
  127. package/coverage/lcov-report/src/validate/validators/and.ts.html +0 -157
  128. package/coverage/lcov-report/src/validate/validators/index.html +0 -176
  129. package/coverage/lcov-report/src/validate/validators/index.ts.html +0 -97
  130. package/coverage/lcov-report/src/validate/validators/or.ts.html +0 -127
  131. package/coverage/lcov-report/src/validate/validators/shape.ts.html +0 -541
  132. package/coverage/lcov-report/src/validate/validators/type.ts.html +0 -154
  133. package/coverage/lcov-report/src/validate_/ValidationError.ts.html +0 -184
  134. package/coverage/lcov-report/src/validate_/fmtValidationResult.ts.html +0 -268
  135. package/coverage/lcov-report/src/validate_/index.html +0 -161
  136. package/coverage/lcov-report/src/validate_/makeValidate.ts.html +0 -634
  137. package/coverage/lcov-report/src/validate_/validate.ts.html +0 -220
  138. package/coverage/lcov-report/switchValue.ts.html +0 -253
  139. package/coverage/lcov-report/typeOf.ts.html +0 -331
  140. package/coverage/lcov-report/validate.ts.html +0 -757
  141. package/coverage/lcov.info +0 -1056
  142. package/jest.config.js +0 -6
  143. package/rollup.config.mjs +0 -6
  144. package/src/PromiseLikeEventEmitter/index.ts +0 -35
  145. package/src/array/arrayChunk.ts +0 -7
  146. package/src/array/index.ts +0 -1
  147. package/src/debug/debugFn/index.ts +0 -48
  148. package/src/debug/debugFn/util.ts +0 -27
  149. package/src/debug/deepFreeze.ts +0 -26
  150. package/src/debug/index.ts +0 -3
  151. package/src/debug/wait.ts +0 -14
  152. package/src/index.ts +0 -9
  153. package/src/interpolate/index.spec.ts +0 -20
  154. package/src/interpolate/index.ts +0 -64
  155. package/src/maybeFn.ts +0 -3
  156. package/src/promise/batchFn.spec.ts +0 -92
  157. package/src/promise/batchFn.ts +0 -176
  158. package/src/promise/index.ts +0 -3
  159. package/src/promise/promiseReduce.ts +0 -15
  160. package/src/promise/resolveNestedPromises.spec.ts +0 -205
  161. package/src/promise/resolveNestedPromises.ts +0 -83
  162. package/src/promise/types.ts +0 -2
  163. package/src/resolvePaths/index.spec.ts +0 -42
  164. package/src/resolvePaths/index.ts +0 -21
  165. package/src/switchValue.spec.ts +0 -30
  166. package/src/switchValue.ts +0 -59
  167. package/src/typeOf.spec.ts +0 -47
  168. package/src/typeOf.ts +0 -81
  169. package/src/validate/__snapshots__/index.spec.ts.snap +0 -9
  170. package/src/validate/async/index.spec.ts +0 -236
  171. package/src/validate/async/index.ts +0 -52
  172. package/src/validate/async/validateAsyncFn.ts +0 -41
  173. package/src/validate/async/validators/index.ts +0 -2
  174. package/src/validate/async/validators/logical.ts +0 -56
  175. package/src/validate/async/validators/shape.ts +0 -160
  176. package/src/validate/async/validators/tmpand.ts +0 -24
  177. package/src/validate/async/validators/tmpor.ts +0 -21
  178. package/src/validate/common/ValidationError.ts +0 -33
  179. package/src/validate/common/util/defaultErrorMessage.ts +0 -26
  180. package/src/validate/common/util/index.ts +0 -3
  181. package/src/validate/common/util/parseValidatorInput.ts +0 -77
  182. package/src/validate/common/util/resolveValidationResult.ts +0 -64
  183. package/src/validate/common/validators/index.ts +0 -1
  184. package/src/validate/common/validators/type.ts +0 -101
  185. package/src/validate/index.spec.ts +0 -5
  186. package/src/validate/index.ts +0 -3
  187. package/src/validate/specUtil/commonTests.js +0 -413
  188. package/src/validate/sync/index.spec.ts +0 -81
  189. package/src/validate/sync/index.ts +0 -53
  190. package/src/validate/sync/validateSyncFn.ts +0 -46
  191. package/src/validate/sync/validators/index.ts +0 -2
  192. package/src/validate/sync/validators/logical.ts +0 -47
  193. package/src/validate/sync/validators/shape.ts +0 -146
  194. package/src/validate/types/async.ts +0 -20
  195. package/src/validate/types/common.ts +0 -70
  196. package/src/validate/types/index.ts +0 -3
  197. package/src/validate/types/sync.ts +0 -20
  198. package/tsconfig.json +0 -11
@@ -0,0 +1,10 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ export declare class PromiseLikeEventEmitter<T, EventTypes extends EventEmitter.ValidEventTypes = {}> extends EventEmitter<EventTypes> {
3
+ private promise;
4
+ resolve: (value: T | PromiseLike<T>) => void;
5
+ reject: (reason?: any) => void;
6
+ constructor();
7
+ then<TResult = T, TError = any>(onFulfilled?: ((value: T) => TResult | PromiseLike<TResult>) | null, onRejected?: ((reason: any) => TError | PromiseLike<TError>) | null): Promise<TResult | TError>;
8
+ catch<TError = any>(onRejected?: ((reason: any) => TError | PromiseLike<TError>) | null): Promise<T | TError>;
9
+ finally(onFinally?: (() => void) | null | undefined): Promise<T>;
10
+ }
@@ -0,0 +1 @@
1
+ export declare function arrayChunk<T>(array: T[], chunkSize: number): T[][];
@@ -0,0 +1 @@
1
+ export * from './arrayChunk';
@@ -0,0 +1,15 @@
1
+ import { Merge } from 'type-fest';
2
+ type AnyFn = (...args: any[]) => any;
3
+ export type FnCallLog<FnType extends AnyFn = AnyFn> = {
4
+ type: 'call';
5
+ callId: string;
6
+ fnName: string;
7
+ args: Parameters<FnType>;
8
+ };
9
+ export type FnResultLog<FnType extends AnyFn = AnyFn> = Merge<FnCallLog<FnType>, {
10
+ type: 'result';
11
+ result: ReturnType<FnType>;
12
+ }>;
13
+ export type FnDebugLog<FnType extends AnyFn = AnyFn> = FnCallLog<FnType> | FnResultLog<FnType>;
14
+ export declare function debugFn<FnType extends AnyFn = AnyFn>(fnName: string, fn: FnType, logCall?: (log: FnCallLog<FnType>) => void, logResult?: (log: FnResultLog<FnType>) => void): (...args: Parameters<FnType>) => ReturnType<FnType>;
15
+ export {};
@@ -0,0 +1 @@
1
+ export declare function generateDeterministicId(args: any[]): string;
@@ -0,0 +1,15 @@
1
+ import { Merge } from 'type-fest';
2
+ type AnyFn = (...args: any[]) => any;
3
+ export type FnCallLog<FnType extends AnyFn = AnyFn> = {
4
+ type: 'call';
5
+ callId: string;
6
+ fnName: string;
7
+ args: Parameters<FnType>;
8
+ };
9
+ export type FnResultLog<FnType extends AnyFn = AnyFn> = Merge<FnCallLog<FnType>, {
10
+ type: 'result';
11
+ result: ReturnType<FnType>;
12
+ }>;
13
+ export type FnDebugLog<FnType extends AnyFn = AnyFn> = FnCallLog<FnType> | FnResultLog<FnType>;
14
+ export declare function debugFn<FnType extends AnyFn = AnyFn>(fnName: string, fn: FnType, logCall?: (log: FnCallLog<FnType>) => void, logResult?: (log: FnResultLog<FnType>) => void): (...args: Parameters<FnType>) => ReturnType<FnType>;
15
+ export {};
@@ -0,0 +1,5 @@
1
+ type AnyObjOrArray = any[] | {
2
+ [key: string]: any;
3
+ };
4
+ export declare function deepFreeze<T extends AnyObjOrArray = AnyObjOrArray>(object: T): T;
5
+ export {};
@@ -0,0 +1,3 @@
1
+ export * from './wait';
2
+ export * from './deepFreeze';
3
+ export * from './debugFn';
@@ -0,0 +1 @@
1
+ export declare function wait<ResultType = any>(ms?: number, result?: ResultType | Error): Promise<ResultType>;
@@ -0,0 +1,9 @@
1
+ export * from './PromiseLikeEventEmitter';
2
+ export * from './debug';
3
+ export * from './interpolate';
4
+ export * from './maybeFn';
5
+ export * from './pickPaths';
6
+ export * from './promise';
7
+ export * from './switchValue';
8
+ export * from './typeOf';
9
+ export * from './validate';
package/dist/index.mjs CHANGED
@@ -2,6 +2,7 @@ import EventEmitter from 'eventemitter3';
2
2
  import copy from 'fast-copy';
3
3
  import { getProperty, setProperty } from 'dot-prop';
4
4
  import traverse from 'traverse';
5
+ import { backOff } from 'exponential-backoff';
5
6
 
6
7
  function _typeof(o) {
7
8
  "@babel/helpers - typeof";
@@ -379,6 +380,16 @@ function maybeFn(input) {
379
380
  return typeof input === 'function' ? input.apply(void 0, args) : input;
380
381
  }
381
382
 
383
+ function pickPaths(sourceObj, paths) {
384
+ return paths.reduce(function (acc, path) {
385
+ var _a = Array.isArray(path) ? path : [path, path],
386
+ targetPath = _a[0],
387
+ resolver = _a[1];
388
+ var value = typeof resolver === 'string' ? getProperty(sourceObj, resolver) : resolver(sourceObj);
389
+ return setProperty(acc, targetPath, value);
390
+ }, {});
391
+ }
392
+
382
393
  function promiseReduce(inputArr, reducerFn, initial) {
383
394
  return inputArr.reduce(function (prevPromise, input, index) {
384
395
  return prevPromise.then(function (acc) {
@@ -467,11 +478,13 @@ function arrayChunk(array, chunkSize) {
467
478
  }
468
479
 
469
480
  var SKIPPED = Symbol();
470
- function batchFn(fn, _a) {
481
+ function batchFn(itemFn, _a) {
471
482
  var _b = _a === void 0 ? {} : _a,
472
483
  _c = _b.batchSize,
473
484
  batchSize = _c === void 0 ? 10 : _c,
474
- skip = _b.skip;
485
+ skip = _b.skip,
486
+ _d = _b.retry,
487
+ retry = _d === void 0 ? false : _d;
475
488
  return function batchExec(items) {
476
489
  var _this = this;
477
490
  var batches = arrayChunk(items, batchSize);
@@ -516,7 +529,11 @@ function batchFn(fn, _a) {
516
529
  _a = SKIPPED;
517
530
  return [3 /*break*/, 4];
518
531
  case 2:
519
- return [4 /*yield*/, fn(item)];
532
+ return [4 /*yield*/, retry === true ? backOff(function () {
533
+ return itemFn(item);
534
+ }) : _typeof(retry) === 'object' && retry !== null ? backOff(function () {
535
+ return itemFn(item);
536
+ }, retry) : itemFn(item)];
520
537
  case 3:
521
538
  _a = _b.sent();
522
539
  _b.label = 4;
@@ -594,16 +611,6 @@ function parseBatchedResults(inputs, results) {
594
611
  });
595
612
  }
596
613
 
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
- }
606
-
607
614
  function testCriteria(criteria, input) {
608
615
  switch (_typeof(criteria)) {
609
616
  case 'function':
@@ -1472,4 +1479,4 @@ validateAsync.or = or;
1472
1479
  validateAsync.not = not;
1473
1480
  validateAsync.assertValid = assertValidAsync;
1474
1481
 
1475
- export { PromiseLikeEventEmitter, SKIPPED, ValidationError, batchFn, debugFn, deepFreeze, interpolate, makeTypeOf, maybeFn, parseBatchedResults, promiseReduce, resolveNestedPromises, resolvePaths, switchExec, switchValue, typeMap, typeOf, validate, validateAsync, wait };
1482
+ export { PromiseLikeEventEmitter, SKIPPED, ValidationError, batchFn, debugFn, deepFreeze, interpolate, makeTypeOf, maybeFn, parseBatchedResults, pickPaths, promiseReduce, resolveNestedPromises, switchExec, switchValue, typeMap, typeOf, validate, validateAsync, wait };
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @function $stringInterpolate
3
+ * @param {String} template Basic JS template string like `${value.path}` value
4
+ * interpolation. It is possible to access nested properties
5
+ * through dot `.` notation. Keywords between braces are
6
+ * only interpreted as paths to the value. No logic
7
+ * supported: loops, conditionals, etc.
8
+ * @param {Object | Array} data Data context to be used for interpolation
9
+ */
10
+ export declare function interpolate(template: string, data: {
11
+ [key: string]: any;
12
+ } | any[], { maxLength }?: {
13
+ maxLength?: number;
14
+ }): string;
@@ -0,0 +1 @@
1
+ export declare function maybeFn(input: any, ...args: any[]): any;
@@ -0,0 +1,4 @@
1
+ type Resolver = string | ((sourceObj: Record<string, any>) => any);
2
+ type PathSpec = string | [string, Resolver];
3
+ export declare function pickPaths(sourceObj: Record<string, any>, paths: PathSpec[]): Record<string, any>;
4
+ export {};
@@ -0,0 +1,49 @@
1
+ import { PromiseLikeEventEmitter } from '../PromiseLikeEventEmitter';
2
+ import { BackoffOptions } from 'exponential-backoff';
3
+ type BatchFnOptions = {
4
+ skip?: (input: any) => Promise<boolean> | boolean;
5
+ batchSize?: number;
6
+ retry?: BackoffOptions | boolean;
7
+ };
8
+ type Batch = {
9
+ index: number;
10
+ items: any[];
11
+ };
12
+ export declare const SKIPPED: unique symbol;
13
+ type EventTypes = {
14
+ batchStart: (data: {
15
+ batch: Batch;
16
+ }) => void;
17
+ batchProgress: (data: {
18
+ batch: Batch;
19
+ progress: number;
20
+ results: any[];
21
+ }) => void;
22
+ itemStart: (data: {
23
+ batch: Batch;
24
+ item: any;
25
+ }) => void;
26
+ itemSkip: (data: {
27
+ batch: Batch;
28
+ item: any;
29
+ }) => void;
30
+ progress: (data: {
31
+ type: 'data' | 'skip' | 'error';
32
+ batch: Batch;
33
+ item: any;
34
+ result: any;
35
+ progress: number;
36
+ }) => void;
37
+ results: (data: {
38
+ results: any[];
39
+ }) => void;
40
+ error: (err: Error) => void;
41
+ };
42
+ export declare function batchFn(itemFn: (input: any) => Promise<any> | any, { batchSize, skip, retry }?: BatchFnOptions): (items: any[]) => PromiseLikeEventEmitter<any[], EventTypes>;
43
+ type ParsedBatchResults = {
44
+ results: any[];
45
+ errors: Error[];
46
+ skipped: any[];
47
+ };
48
+ export declare function parseBatchedResults(inputs: any, results: any[]): ParsedBatchResults;
49
+ export {};
@@ -0,0 +1,3 @@
1
+ export * from './promiseReduce';
2
+ export * from './resolveNestedPromises';
3
+ export * from './batchFn';
@@ -0,0 +1 @@
1
+ export declare function promiseReduce<ItemType = any, AccType = any>(inputArr: ItemType[], reducerFn: (acc: AccType, input: ItemType, index: number) => AccType | Promise<AccType>, initial: AccType): Promise<AccType>;
@@ -0,0 +1,14 @@
1
+ import { AnyArray, AnyObject } from './types';
2
+ type ResolveCtx = {
3
+ root: AnyArray | AnyObject;
4
+ depth: number;
5
+ };
6
+ type ResolveOptions = {
7
+ mode: 'eager' | 'lazy';
8
+ maxDepth: number;
9
+ clone: false | (<T = {
10
+ [key: string]: any;
11
+ } | any[]>(obj: T) => T);
12
+ };
13
+ export declare function resolveNestedPromises(node: AnyArray | AnyObject, options?: Partial<ResolveOptions>, ctx?: ResolveCtx): Promise<AnyArray | AnyObject>;
14
+ export {};
@@ -0,0 +1,2 @@
1
+ export type AnyArray = unknown[];
2
+ export type AnyObject = Record<string, unknown>;
@@ -0,0 +1,4 @@
1
+ type Resolver = string | ((sourceObj: Record<string, any>) => any);
2
+ type PathSpec = string | [string, Resolver];
3
+ export declare function resolvePaths(sourceObj: Record<string, any>, paths: PathSpec[]): Record<string, any>;
4
+ export {};
@@ -0,0 +1,9 @@
1
+ type Criteria = string | symbol | RegExp | ((input: any) => boolean);
2
+ type ObjectSwitchCases<ValueType = any> = {
3
+ [key: string | symbol]: ValueType;
4
+ };
5
+ type TupleSwitchCases<ValueType = any> = ([Criteria, ValueType] | [ValueType])[];
6
+ export type SwitchCases<ValueType = any> = ObjectSwitchCases<ValueType> | TupleSwitchCases<ValueType>;
7
+ export declare function switchValue<ValueType = any>(input: any, cases: SwitchCases): ValueType | undefined;
8
+ export declare function switchExec<ArgsType extends any[] = any[]>(input: any, cases: SwitchCases<(...args: any[]) => any>, ...args: ArgsType): any;
9
+ export {};
@@ -0,0 +1,7 @@
1
+ export type TypeTest = (value: any) => boolean;
2
+ type TypeMap = Record<string, TypeTest>;
3
+ export declare function typeMap<T extends TypeMap>(typeMap: T): T;
4
+ export declare function makeTypeOf<T extends TypeMap>(typeMap: T): (value: any) => keyof T | null;
5
+ export type DEFAULT_TYPE_NAMES = 'null' | 'undefined' | 'boolean' | 'number' | 'bigint' | 'string' | 'symbol' | 'function' | 'object' | 'array' | 'regexp' | 'nan' | 'date' | 'map' | 'weakmap' | 'set' | 'weakset' | 'promise' | 'error';
6
+ export declare const typeOf: (value: any) => "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "map" | "error" | "null" | "array" | "regexp" | "nan" | "date" | "weakmap" | "set" | "weakset" | "promise" | null;
7
+ export {};
@@ -0,0 +1,18 @@
1
+ import * as common from '../common/validators';
2
+ import * as asyncValidators from './validators';
3
+ import { DetailedInvalid, AsyncValidatorSystem, Valid } from '../types';
4
+ declare function assertValidAsync<InputT = any>(validator: AsyncValidatorSystem['ValidatorInput'], input: InputT): Promise<InputT>;
5
+ interface ValidateAsync {
6
+ (validatorInput: AsyncValidatorSystem['ValidatorInput'], input: any): Promise<Valid | DetailedInvalid>;
7
+ type: typeof common.typeValidator;
8
+ obj: typeof asyncValidators.obj;
9
+ objOf: typeof asyncValidators.objOf;
10
+ tuple: typeof asyncValidators.tuple;
11
+ arrayOf: typeof asyncValidators.arrayOf;
12
+ and: typeof asyncValidators.and;
13
+ or: typeof asyncValidators.or;
14
+ not: typeof asyncValidators.not;
15
+ assertValid: typeof assertValidAsync;
16
+ }
17
+ export declare const validateAsync: ValidateAsync;
18
+ export {};
@@ -0,0 +1,2 @@
1
+ import { DetailedInvalid, Valid, AsyncValidatorSystem } from '../types';
2
+ export declare function validateAsyncFn(validatorInput: AsyncValidatorSystem['ValidatorInput'], input: any): Promise<Valid | DetailedInvalid>;
@@ -0,0 +1,2 @@
1
+ export * from './shape';
2
+ export * from './logical';
@@ -0,0 +1,4 @@
1
+ import { AsyncValidatorSystem } from '../../types';
2
+ export declare function and(validators: AsyncValidatorSystem['ValidatorInput'][]): AsyncValidatorSystem['ValidatorFn'];
3
+ export declare function or(validators: AsyncValidatorSystem['ValidatorInput'][]): AsyncValidatorSystem['ValidatorFn'];
4
+ export declare function not(validator: AsyncValidatorSystem['ValidatorInput']): AsyncValidatorSystem['ValidatorFn'];
@@ -0,0 +1,7 @@
1
+ import { AsyncValidatorSystem } from '../../types';
2
+ export declare function obj(objShape: {
3
+ [key: string]: AsyncValidatorSystem['ValidatorInput'];
4
+ }): AsyncValidatorSystem['ValidatorFn'];
5
+ export declare function objOf(ofType: AsyncValidatorSystem['ValidatorInput']): AsyncValidatorSystem['ValidatorFn'];
6
+ export declare function tuple(tupleShape: AsyncValidatorSystem['ValidatorInput'][]): AsyncValidatorSystem['ValidatorFn'];
7
+ export declare function arrayOf(ofType: AsyncValidatorSystem['ValidatorInput']): AsyncValidatorSystem['ValidatorFn'];
@@ -0,0 +1,2 @@
1
+ import { AsyncValidatorSystem } from '../../types';
2
+ export declare function and(validators: AsyncValidatorSystem['ValidatorInput'][]): AsyncValidatorSystem['ValidatorFn'];
@@ -0,0 +1,2 @@
1
+ import { AsyncValidatorSystem } from '../../types';
2
+ export declare function or(validators: AsyncValidatorSystem['ValidatorInput'][]): AsyncValidatorSystem['ValidatorFn'];
@@ -0,0 +1,11 @@
1
+ import { DetailedInvalid } from '../types';
2
+ export declare class ValidationError extends Error {
3
+ input: any;
4
+ code?: string;
5
+ error?: Error;
6
+ path?: string;
7
+ nestedErrors?: DetailedInvalid[];
8
+ name: string;
9
+ constructor({ message, ...details }: DetailedInvalid);
10
+ toJSON(): DetailedInvalid;
11
+ }
@@ -0,0 +1,2 @@
1
+ import { DetailedInvalid } from '../../types';
2
+ export declare function defaultErrorMessage({ input, message, nestedErrors, expectedTypes, }: DetailedInvalid): string;
@@ -0,0 +1,3 @@
1
+ export * from './defaultErrorMessage';
2
+ export * from './resolveValidationResult';
3
+ export * from './parseValidatorInput';
@@ -0,0 +1,5 @@
1
+ export declare function parseValidatorInput<ValidatorInputT, ValidatorFnT, ValidatorT>({ objValidator, }: {
2
+ objValidator: (objShape: {
3
+ [key: string]: ValidatorInputT;
4
+ }) => ValidatorFnT;
5
+ }, validatorInput: ValidatorInputT): ValidatorT;
@@ -0,0 +1,6 @@
1
+ import { DetailedInvalid, Valid, ValidatorErrorMessageFn, ValidatorResult } from '../../types';
2
+ export declare function resolveValidationResult({ input, result, errorMessage, }: {
3
+ input: any;
4
+ result: ValidatorResult;
5
+ errorMessage: ValidatorErrorMessageFn;
6
+ }): Valid | DetailedInvalid;
@@ -0,0 +1 @@
1
+ export * from './type';
@@ -0,0 +1,9 @@
1
+ import { DEFAULT_TYPE_NAMES } from '../../../typeOf';
2
+ import { CommonValidatorFn } from '../../types';
3
+ type ParsedExpectedType = {
4
+ type: DEFAULT_TYPE_NAMES;
5
+ required: boolean;
6
+ };
7
+ type ExpectedTypesInput = (string | ParsedExpectedType)[] | string | ParsedExpectedType;
8
+ export declare function typeValidator(expectedTypesInput: ExpectedTypesInput): CommonValidatorFn;
9
+ export {};
@@ -0,0 +1,3 @@
1
+ export * from './sync';
2
+ export * from './async';
3
+ export * from './common/ValidationError';
@@ -0,0 +1,18 @@
1
+ import * as common from '../common/validators';
2
+ import * as sync from './validators';
3
+ import { DetailedInvalid, SyncValidatorSystem, Valid } from '../types';
4
+ declare function assertValidSync<InputT = any>(validator: SyncValidatorSystem['ValidatorInput'], input: InputT): InputT;
5
+ interface Validate {
6
+ (validatorInput: SyncValidatorSystem['ValidatorInput'], input: any): Valid | DetailedInvalid;
7
+ type: typeof common.typeValidator;
8
+ obj: typeof sync.obj;
9
+ objOf: typeof sync.objOf;
10
+ tuple: typeof sync.tuple;
11
+ arrayOf: typeof sync.arrayOf;
12
+ and: typeof sync.and;
13
+ or: typeof sync.or;
14
+ not: typeof sync.not;
15
+ assertValid: typeof assertValidSync;
16
+ }
17
+ export declare const validate: Validate;
18
+ export {};
@@ -0,0 +1,2 @@
1
+ import { DetailedInvalid, Valid, SyncValidatorSystem } from '../types';
2
+ export declare function validateSyncFn(validatorInput: SyncValidatorSystem['ValidatorInput'], input: any): Valid | DetailedInvalid;
@@ -0,0 +1,2 @@
1
+ export * from './logical';
2
+ export * from './shape';
@@ -0,0 +1,4 @@
1
+ import { SyncValidatorSystem } from '../../types';
2
+ export declare function and(validators: SyncValidatorSystem['ValidatorInput'][]): SyncValidatorSystem['ValidatorFn'];
3
+ export declare function or(validators: SyncValidatorSystem['ValidatorInput'][]): SyncValidatorSystem['ValidatorFn'];
4
+ export declare function not(validator: SyncValidatorSystem['ValidatorInput']): SyncValidatorSystem['ValidatorFn'];
@@ -0,0 +1,7 @@
1
+ import { SyncValidatorSystem } from '../../types';
2
+ export declare function obj(objShape: {
3
+ [key: string]: SyncValidatorSystem['ValidatorInput'];
4
+ }): SyncValidatorSystem['ValidatorFn'];
5
+ export declare function objOf(ofType: SyncValidatorSystem['ValidatorInput']): SyncValidatorSystem['ValidatorFn'];
6
+ export declare function tuple(tupleShape: SyncValidatorSystem['ValidatorInput'][]): SyncValidatorSystem['ValidatorFn'];
7
+ export declare function arrayOf(ofType: SyncValidatorSystem['ValidatorInput']): SyncValidatorSystem['ValidatorFn'];
@@ -0,0 +1,5 @@
1
+ import { DetailedInvalid, Valid, ValidatorResult, ValidatorSystem } from './common';
2
+ export type AsyncValidatorSystem = ValidatorSystem<(input: any, context: {
3
+ validateAsync: AsyncValidateFn;
4
+ }) => ValidatorResult | Promise<ValidatorResult>>;
5
+ export type AsyncValidateFn = (validatorInput: AsyncValidatorSystem['ValidatorInput'], input: any) => Promise<Valid | DetailedInvalid>;
@@ -0,0 +1,27 @@
1
+ export type Valid = true;
2
+ export type SimpleInvalid = false | undefined | null;
3
+ export type ErrorMessageInvalid = string;
4
+ export type ErrorInvalid = Error;
5
+ export type DetailedInvalid = {
6
+ input: any;
7
+ message?: string;
8
+ code?: string;
9
+ error?: Error;
10
+ path?: string;
11
+ nestedErrors?: DetailedInvalid[];
12
+ expectedTypes?: string;
13
+ };
14
+ export type ValidatorResult = Valid | SimpleInvalid | ErrorMessageInvalid | ErrorInvalid | DetailedInvalid;
15
+ export type ValidatorErrorMessageFn = (props: DetailedInvalid) => string;
16
+ export type ValidatorErrorMessageInput = string | ValidatorErrorMessageFn;
17
+ export type CommonValidatorFn = (input: any) => ValidatorResult;
18
+ type _ValidatorFnInput<ValidatorFn> = string | {
19
+ [key: string]: _ValidatorFnInput<ValidatorFn> | [_ValidatorFnInput<ValidatorFn>, ValidatorErrorMessageInput];
20
+ } | ValidatorFn;
21
+ export type ValidatorSystem<ValidatorFn> = {
22
+ ValidatorFn: ValidatorFn;
23
+ ValidatorFnInput: _ValidatorFnInput<ValidatorFn>;
24
+ Validator: [ValidatorFn, ValidatorErrorMessageFn];
25
+ ValidatorInput: _ValidatorFnInput<ValidatorFn> | [_ValidatorFnInput<ValidatorFn>, ValidatorErrorMessageInput];
26
+ };
27
+ export {};
@@ -0,0 +1,3 @@
1
+ export * from './common';
2
+ export * from './sync';
3
+ export * from './async';
@@ -0,0 +1,5 @@
1
+ import { DetailedInvalid, Valid, ValidatorResult, ValidatorSystem } from './common';
2
+ export type SyncValidatorSystem = ValidatorSystem<(input: any, context: {
3
+ validateSync: SyncValidateFn;
4
+ }) => ValidatorResult>;
5
+ export type SyncValidateFn = (validatorInput: SyncValidatorSystem['ValidatorInput'], input: any) => Valid | DetailedInvalid;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orioro/util",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "packageManager": "yarn@4.0.2",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",
@@ -9,6 +9,9 @@
9
9
  ".": "./dist/index.mjs",
10
10
  "./*": "./dist/*"
11
11
  },
12
+ "files": [
13
+ "dist"
14
+ ],
12
15
  "publishConfig": {
13
16
  "access": "public"
14
17
  },
@@ -17,7 +20,8 @@
17
20
  "build:watch": "rollup --config ./rollup.config.mjs --watch",
18
21
  "build": "rollup --config ./rollup.config.mjs",
19
22
  "test": "jest",
20
- "dev": "jest --watch"
23
+ "dev": "jest --watch",
24
+ "prepublish": "yarn build"
21
25
  },
22
26
  "devDependencies": {
23
27
  "@orioro/dev": "0.0.0",
@@ -30,6 +34,7 @@
30
34
  "dependencies": {
31
35
  "dot-prop": "^8.0.2",
32
36
  "eventemitter3": "^5.0.1",
37
+ "exponential-backoff": "^3.1.1",
33
38
  "fast-copy": "^3.0.2",
34
39
  "traverse": "^0.6.9",
35
40
  "type-fest": "^4.18.1"
package/babel.config.cjs DELETED
@@ -1,13 +0,0 @@
1
- //
2
- // TODO: keep track of babel + jest support for esm and update
3
- // accordingly
4
- // https://stackoverflow.com/questions/61146112/error-while-loading-config-you-appear-to-be-using-a-native-ecmascript-module-c
5
- //
6
- // import { BABEL_PRESET } from '@orioro/dev/ts'
7
-
8
- // export default BABEL_PRESET
9
-
10
- module.exports = {
11
- presets: ['@babel/preset-env', '@babel/preset-typescript'],
12
- exclude: 'node_modules/**',
13
- }