@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/jest.config.js ADDED
@@ -0,0 +1,6 @@
1
+ export default {
2
+ transformIgnorePatterns: [
3
+ // Ignore all node_modules except those that need transpiling
4
+ '/node_modules/(?!dot-prop).+\\.js$'
5
+ ]
6
+ };
package/package.json CHANGED
@@ -1,37 +1,37 @@
1
1
  {
2
2
  "name": "@orioro/util",
3
- "version": "0.0.0",
4
- "description": "",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "scripts": {
8
- "test": "jest --coverage",
9
- "serve": "browser-sync start --server demo --watch --ignore **/*/index.js --no-ui --no-ghost-mode",
10
- "dev-serve": "npm run serve & rollup --config ./rollup/dev.config.js --watch",
11
- "dev-test": "jest --watch",
12
- "dev-bundle": "rollup --config ./rollup/dev.config.js",
13
- "build": "rm -rf dist && rollup --config ./rollup/build.config.js",
14
- "prepublish": "npm run build"
3
+ "version": "0.2.0",
4
+ "packageManager": "yarn@4.0.2",
5
+ "type": "module",
6
+ "main": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": "./dist/index.mjs",
10
+ "./*": "./dist/*"
15
11
  },
16
- "author": "",
17
- "license": "ISC",
18
12
  "publishConfig": {
19
13
  "access": "public"
20
14
  },
15
+ "license": "ISC",
16
+ "scripts": {
17
+ "build:watch": "rollup --config ./rollup.config.mjs --watch",
18
+ "build": "rollup --config ./rollup.config.mjs",
19
+ "test": "jest",
20
+ "dev": "jest --watch"
21
+ },
21
22
  "devDependencies": {
22
- "@babel/core": "^7.1.2",
23
- "@babel/plugin-proposal-object-rest-spread": "^7.0.0",
24
- "@babel/preset-env": "^7.1.0",
25
- "babel-core": "^7.0.0-bridge.0",
26
- "babel-jest": "^23.6.0",
27
- "browser-sync": "^2.24.7",
28
- "jest": "^23.6.0",
29
- "rollup": "^0.66.4",
30
- "rollup-plugin-babel": "^4.0.3",
31
- "rollup-plugin-commonjs": "^9.1.8",
32
- "rollup-plugin-node-resolve": "^3.4.0"
23
+ "@orioro/dev": "0.0.0",
24
+ "@types/clone-deep": "^4.0.4",
25
+ "@types/jest": "^29.5.12",
26
+ "babel-jest": "^29.7.0",
27
+ "jest": "^29.7.0",
28
+ "rollup": "^4.13.0"
33
29
  },
34
30
  "dependencies": {
35
- "mingo": "^2.3.5"
31
+ "dot-prop": "^8.0.2",
32
+ "eventemitter3": "^5.0.1",
33
+ "fast-copy": "^3.0.2",
34
+ "traverse": "^0.6.9",
35
+ "type-fest": "^4.18.1"
36
36
  }
37
- }
37
+ }
@@ -0,0 +1,6 @@
1
+ import { rollupConfig } from '@orioro/dev/ts/rollup.mjs'
2
+
3
+ export default rollupConfig((base) => ({
4
+ ...base,
5
+ plugins: [...base.plugins],
6
+ }))
@@ -0,0 +1,35 @@
1
+ import EventEmitter from 'eventemitter3'
2
+
3
+ export class PromiseLikeEventEmitter<
4
+ T,
5
+ EventTypes extends EventEmitter.ValidEventTypes = {},
6
+ > extends EventEmitter<EventTypes> {
7
+ private promise: Promise<T>
8
+ public resolve!: (value: T | PromiseLike<T>) => void
9
+ public reject!: (reason?: any) => void
10
+
11
+ constructor() {
12
+ super()
13
+ this.promise = new Promise<T>((resolve, reject) => {
14
+ this.resolve = resolve
15
+ this.reject = reject
16
+ })
17
+ }
18
+
19
+ public then<TResult = T, TError = any>(
20
+ onFulfilled?: ((value: T) => TResult | PromiseLike<TResult>) | null,
21
+ onRejected?: ((reason: any) => TError | PromiseLike<TError>) | null,
22
+ ): Promise<TResult | TError> {
23
+ return this.promise.then(onFulfilled, onRejected)
24
+ }
25
+
26
+ public catch<TError = any>(
27
+ onRejected?: ((reason: any) => TError | PromiseLike<TError>) | null,
28
+ ): Promise<T | TError> {
29
+ return this.promise.catch(onRejected)
30
+ }
31
+
32
+ public finally(onFinally?: (() => void) | null | undefined): Promise<T> {
33
+ return this.promise.finally(onFinally)
34
+ }
35
+ }
@@ -0,0 +1,7 @@
1
+ export function arrayChunk<T>(array: T[], chunkSize: number): T[][] {
2
+ const chunks: T[][] = []
3
+ for (let i = 0; i < array.length; i += chunkSize) {
4
+ chunks.push(array.slice(i, i + chunkSize))
5
+ }
6
+ return chunks
7
+ }
@@ -0,0 +1 @@
1
+ export * from './arrayChunk'
@@ -0,0 +1,48 @@
1
+ import { Merge } from 'type-fest'
2
+ import { generateDeterministicId } from './util'
3
+ import copy from 'fast-copy'
4
+
5
+ type AnyFn = (...args: any[]) => any
6
+
7
+ export type FnCallLog<FnType extends AnyFn = AnyFn> = {
8
+ type: 'call'
9
+ callId: string
10
+ fnName: string
11
+ args: Parameters<FnType>
12
+ }
13
+
14
+ export type FnResultLog<FnType extends AnyFn = AnyFn> = Merge<
15
+ FnCallLog<FnType>,
16
+ {
17
+ type: 'result'
18
+ result: ReturnType<FnType>
19
+ }
20
+ >
21
+
22
+ export type FnDebugLog<FnType extends AnyFn = AnyFn> =
23
+ | FnCallLog<FnType>
24
+ | FnResultLog<FnType>
25
+
26
+ export function debugFn<FnType extends AnyFn = AnyFn>(
27
+ fnName: string,
28
+ fn: FnType,
29
+ logCall?: (log: FnCallLog<FnType>) => void,
30
+ logResult?: (log: FnResultLog<FnType>) => void,
31
+ ) {
32
+ return function (...args: Parameters<FnType>): ReturnType<FnType> {
33
+ const callId = generateDeterministicId([fnName, fn, ...args])
34
+
35
+ if (typeof logCall === 'function') {
36
+ // Deep clone args, so that we may detect mutations later on
37
+ logCall({ type: 'call', callId, args: copy(args), fnName })
38
+ }
39
+
40
+ const result = fn(...args)
41
+
42
+ if (typeof logResult === 'function') {
43
+ logResult({ type: 'result', callId, args, result, fnName })
44
+ }
45
+
46
+ return result
47
+ }
48
+ }
@@ -0,0 +1,27 @@
1
+ function stringifyReplacer(key: string, value: any): any {
2
+ if (typeof value === 'function') {
3
+ // Convert functions to a consistent string representation
4
+ return `function:${value.toString()}`
5
+ }
6
+ return value
7
+ }
8
+
9
+ function simpleHash(str: string): string {
10
+ let hash = 0
11
+ for (let i = 0; i < str.length; i++) {
12
+ const char = str.charCodeAt(i)
13
+ hash = (hash << 5) - hash + char
14
+ hash |= 0 // Convert to 32bit integer
15
+ }
16
+ // Ensure the hash is positive to avoid '-' signals
17
+ hash = Math.abs(hash)
18
+ return hash.toString(36) // Convert to base36 to ensure the result is alphanumeric
19
+ }
20
+
21
+ export function generateDeterministicId(args: any[]): string {
22
+ // Convert arguments to a JSON string using the custom replacer
23
+ const argsString = JSON.stringify(args, stringifyReplacer)
24
+
25
+ // Generate a simple hash of the string
26
+ return simpleHash(argsString)
27
+ }
@@ -0,0 +1,26 @@
1
+ type AnyObjOrArray = any[] | { [key: string]: any }
2
+
3
+ export function deepFreeze<T extends AnyObjOrArray = AnyObjOrArray>(
4
+ object: T,
5
+ ): T {
6
+ Object.freeze(object)
7
+
8
+ if (Array.isArray(object)) {
9
+ // Handle the array case
10
+ object.forEach((item) => {
11
+ if (item && typeof item === 'object' && !Object.isFrozen(item)) {
12
+ deepFreeze(item)
13
+ }
14
+ })
15
+ } else {
16
+ // Handle the object case
17
+ Object.keys(object).forEach((key) => {
18
+ const value = object[key]
19
+ if (value && typeof value === 'object' && !Object.isFrozen(value)) {
20
+ deepFreeze(value)
21
+ }
22
+ })
23
+ }
24
+
25
+ return object
26
+ }
@@ -0,0 +1,3 @@
1
+ export * from './wait'
2
+ export * from './deepFreeze'
3
+ export * from './debugFn'
@@ -0,0 +1,14 @@
1
+ export async function wait<ResultType = any>(
2
+ ms: number = 1000,
3
+ result?: ResultType | Error,
4
+ ): Promise<ResultType> {
5
+ return new Promise((resolve, reject) => {
6
+ setTimeout(() => {
7
+ if (result instanceof Error) {
8
+ reject(result)
9
+ } else {
10
+ resolve(result as ResultType)
11
+ }
12
+ }, ms)
13
+ })
14
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export * from './PromiseLikeEventEmitter'
2
+ export * from './debug'
3
+ export * from './interpolate'
4
+ export * from './maybeFn'
5
+ export * from './promise'
6
+ export * from './resolvePaths'
7
+ export * from './switchValue'
8
+ export * from './typeOf'
9
+ export * from './validate'
@@ -0,0 +1,20 @@
1
+ import { interpolate } from './'
2
+
3
+ describe('interpolate', () => {
4
+ test('basic', () => {
5
+ // const date = new Date('2024-05-02T08:42:51.303Z')
6
+ expect(
7
+ interpolate(
8
+ 'Hello ${ name }! Today is ${now.day}, ${now.hour}:${now.min}',
9
+ {
10
+ name: 'World',
11
+ now: {
12
+ day: '02',
13
+ hour: '08',
14
+ min: '51',
15
+ },
16
+ },
17
+ ),
18
+ ).toEqual('Hello World! Today is 02, 08:51')
19
+ })
20
+ })
@@ -0,0 +1,64 @@
1
+ import { getProperty } from 'dot-prop'
2
+
3
+ /**
4
+ * /\$\{\s*([\w$.]+)\s*\}/g
5
+ * ![](docs/resources/interpolation_regexp.png)
6
+ *
7
+ * RegExp used for matching interpolation expressions.
8
+ * Allows a non-interrupted sequence of alphanumeric chars ([A-Za-z0-9_]),
9
+ * dollar signs ($) and dots (.) wrapped in curly braces ({})
10
+ * with or without any number of whitespace chars (' ') between braces and the
11
+ * value identifier.
12
+ *
13
+ * Some resources on RegExp safety concerning RegExp Denial of Service (ReDOS)
14
+ * through Catastrophic backtracking, for future study and reference:
15
+ *
16
+ * - [Catastrophic backtracking](https://www.regular-expressions.info/catastrophic.html)
17
+ * - [Regular expression visualizer](https://github.com/CJex/regulex)
18
+ * - [Validator.js](https://github.com/validatorjs/validator.js)
19
+ * - [Stack Overflow interesting question](https://stackoverflow.com/questions/63127145/safe-regex-patterns-from-redos-attack)
20
+ * - [Catastrophic backtracking - JavaScript Info](https://javascript.info/regexp-catastrophic-backtracking#preventing-backtracking)
21
+ * - [Google re2 library](https://github.com/google/re2)
22
+ * - [Google re2 for Node.js - re2](https://github.com/uhop/node-re2/)
23
+ *
24
+ * @const {RegExp} INTERPOLATION_REGEXP
25
+ */
26
+ const INTERPOLATION_REGEXP = /\$\{\s*([\w$.]+)\s*\}/g
27
+
28
+ /**
29
+ * @function $stringInterpolate
30
+ * @param {String} template Basic JS template string like `${value.path}` value
31
+ * interpolation. It is possible to access nested properties
32
+ * through dot `.` notation. Keywords between braces are
33
+ * only interpreted as paths to the value. No logic
34
+ * supported: loops, conditionals, etc.
35
+ * @param {Object | Array} data Data context to be used for interpolation
36
+ */
37
+ export function interpolate(
38
+ template: string,
39
+ data: { [key: string]: any } | any[],
40
+ { maxLength = 10000 }: { maxLength?: number } = {}
41
+ ): string {
42
+ if (template.length > maxLength) {
43
+ throw new Error(`Template exceeds maxLength ${maxLength}`)
44
+ }
45
+
46
+ return template.replace(INTERPOLATION_REGEXP, (_, path) => {
47
+ const value = getProperty(data, path)
48
+
49
+ switch (typeof value) {
50
+ case 'number': {
51
+ return value + ''
52
+ }
53
+ case 'string': {
54
+ return value
55
+ }
56
+ default: {
57
+ console.warn(
58
+ `Attempting to use non interpolatable value in interpolate ${value}`,
59
+ )
60
+ return ''
61
+ }
62
+ }
63
+ })
64
+ }
package/src/maybeFn.ts ADDED
@@ -0,0 +1,3 @@
1
+ export function maybeFn(input: any, ...args: any[]) {
2
+ return typeof input === 'function' ? input(...args) : input
3
+ }
@@ -0,0 +1,92 @@
1
+ import { SKIPPED, batchFn } from './batchFn'
2
+ import { wait } from '../debug'
3
+
4
+ describe('basic', () => {
5
+ test('basic', async () => {
6
+ const DELAY = 100
7
+ const BATCH_SIZE = 3
8
+ const entries = 'abcdefghijklmnopqrstuvwxyz1234567890'.split('')
9
+
10
+ const onItemStart = jest.fn()
11
+ const onItemSkip = jest.fn()
12
+ const onProgress = jest.fn()
13
+ const onBatchStart = jest.fn()
14
+ const onBatchProgress = jest.fn()
15
+
16
+ const batchToUpperCase = batchFn(
17
+ async (input: string) => {
18
+ await wait(DELAY)
19
+
20
+ if (input === 'd') {
21
+ throw new Error('ARBITRARY_INVALID_INPUT')
22
+ }
23
+
24
+ return input.toUpperCase()
25
+ },
26
+ {
27
+ batchSize: BATCH_SIZE,
28
+ skip: async (input: string) => {
29
+ switch (input) {
30
+ case 'k':
31
+ case 'w': {
32
+ return true
33
+ }
34
+ default: {
35
+ return false
36
+ }
37
+ }
38
+ },
39
+ },
40
+ )
41
+
42
+ const startTime = performance.now()
43
+ const promise = batchToUpperCase(entries)
44
+
45
+ promise.on('itemStart', onItemStart)
46
+ promise.on('itemSkip', onItemSkip)
47
+ promise.on('progress', onProgress)
48
+ promise.on('batchStart', onBatchStart)
49
+ promise.on('batchProgress', onBatchProgress)
50
+
51
+ const result = await promise
52
+ const endTime = performance.now()
53
+
54
+ expect(onItemStart).toHaveBeenCalledTimes(entries.length)
55
+ expect(onItemSkip).toHaveBeenCalledTimes(2)
56
+ expect(onBatchStart).toHaveBeenCalledTimes(
57
+ Math.ceil(entries.length / BATCH_SIZE),
58
+ )
59
+ expect(onBatchProgress).toHaveBeenCalledTimes(
60
+ Math.ceil(entries.length / BATCH_SIZE),
61
+ )
62
+
63
+ expect(result).toEqual(
64
+ entries.map((entry, index) => {
65
+ switch (entry) {
66
+ case 'd': {
67
+ return new Error('ARBITRARY_INVALID_INPUT')
68
+ }
69
+ case 'k':
70
+ case 'w': {
71
+ return SKIPPED
72
+ }
73
+ default: {
74
+ return entry.toUpperCase()
75
+ }
76
+ }
77
+ }),
78
+ )
79
+
80
+ //
81
+ // Expect the execution time to be approximately
82
+ // DELAY * batch count
83
+ //
84
+ const executionTime = endTime - startTime
85
+ expect(executionTime).toBeGreaterThan(
86
+ Math.ceil(entries.length / BATCH_SIZE) * DELAY,
87
+ )
88
+ expect(executionTime).toBeLessThan(
89
+ Math.ceil(entries.length / BATCH_SIZE) * DELAY + 100,
90
+ )
91
+ })
92
+ })
@@ -0,0 +1,176 @@
1
+ import { arrayChunk } from '../array'
2
+ import { promiseReduce } from './promiseReduce'
3
+ import { PromiseLikeEventEmitter } from '../PromiseLikeEventEmitter'
4
+
5
+ type BatchFnOptions = {
6
+ skip?: (input: any) => Promise<boolean> | boolean
7
+ batchSize?: number
8
+ }
9
+
10
+ type Batch = {
11
+ index: number
12
+ items: any[]
13
+ }
14
+
15
+ export const SKIPPED = Symbol()
16
+
17
+ type EventTypes = {
18
+ batchStart: (data: { batch: Batch }) => void
19
+ batchProgress: (data: {
20
+ batch: Batch
21
+ progress: number
22
+ results: any[]
23
+ }) => void
24
+ itemStart: (data: { batch: Batch; item: any }) => void
25
+ itemSkip: (data: { batch: Batch; item: any }) => void
26
+ progress: (data: {
27
+ type: 'data' | 'skip' | 'error'
28
+ batch: Batch
29
+ item: any
30
+ result: any
31
+ progress: number
32
+ }) => void
33
+ results: (data: { results: any[] }) => void
34
+ error: (err: Error) => void
35
+ }
36
+
37
+ export function batchFn(
38
+ fn: (input: any) => Promise<any> | any,
39
+ { batchSize = 10, skip }: BatchFnOptions = {},
40
+ ) {
41
+ return function batchExec(
42
+ items: any[],
43
+ ): PromiseLikeEventEmitter<any[], EventTypes> {
44
+ const batches = arrayChunk(items, batchSize)
45
+
46
+ const promise = new PromiseLikeEventEmitter<any[], EventTypes>()
47
+
48
+ // const events = new EventEmitter<EventTypes>()
49
+
50
+ let progressCount = 0
51
+
52
+ const resultsPromise = promiseReduce(
53
+ batches,
54
+ async (acc, batchItems, index) => {
55
+ const batch = {
56
+ index,
57
+ items: batchItems,
58
+ }
59
+
60
+ promise.emit('batchStart', {
61
+ batch,
62
+ })
63
+
64
+ //
65
+ // A batch items are executed in parallell
66
+ //
67
+ const batchResults: any[] = await Promise.all(
68
+ batchItems.map(async (item) => {
69
+ try {
70
+ promise.emit('itemStart', {
71
+ batch,
72
+ item,
73
+ })
74
+
75
+ const itemSkip = await (typeof skip === 'function' && skip(item))
76
+
77
+ if (itemSkip) {
78
+ promise.emit('itemSkip', {
79
+ batch,
80
+ item,
81
+ })
82
+ }
83
+
84
+ const itemResult = itemSkip ? SKIPPED : await fn(item)
85
+
86
+ progressCount += 1
87
+
88
+ promise.emit('progress', {
89
+ type: itemSkip ? 'skip' : 'data',
90
+ batch,
91
+ progress: progressCount / items.length,
92
+ item,
93
+ result: itemResult,
94
+ })
95
+
96
+ return itemResult
97
+ } catch (err) {
98
+ progressCount += 1
99
+
100
+ promise.emit('progress', {
101
+ type: 'error',
102
+ batch,
103
+ progress: progressCount / items.length,
104
+ item,
105
+ result: err,
106
+ })
107
+
108
+ return err
109
+ }
110
+ }),
111
+ )
112
+
113
+ promise.emit('batchProgress', {
114
+ batch,
115
+ progress: batch.index + 1 / batches.length,
116
+ results: batchResults,
117
+ })
118
+
119
+ return [...acc, ...batchResults]
120
+ },
121
+ [] as any[],
122
+ )
123
+
124
+ resultsPromise.then(
125
+ (results) => {
126
+ promise.resolve(results)
127
+ promise.emit('results', {
128
+ results,
129
+ })
130
+ },
131
+ (err) => {
132
+ promise.reject(err)
133
+ promise.emit('error', err)
134
+ },
135
+ )
136
+
137
+ return promise
138
+ }
139
+ }
140
+
141
+ type ParsedBatchResults = {
142
+ results: any[]
143
+ errors: Error[]
144
+ skipped: any[]
145
+ }
146
+
147
+ export function parseBatchedResults(
148
+ inputs: any,
149
+ results: any[],
150
+ ): ParsedBatchResults {
151
+ return results.reduce(
152
+ (acc, result, index) => {
153
+ if (result === SKIPPED) {
154
+ return {
155
+ ...acc,
156
+ skipped: [...acc.skipped, inputs[index]],
157
+ }
158
+ } else if (result instanceof Error) {
159
+ return {
160
+ ...acc,
161
+ errors: [...acc.errors, result],
162
+ }
163
+ } else {
164
+ return {
165
+ ...acc,
166
+ results: [...acc.results, result],
167
+ }
168
+ }
169
+ },
170
+ {
171
+ results: [],
172
+ errors: [],
173
+ skipped: [],
174
+ } as ParsedBatchResults,
175
+ )
176
+ }
@@ -0,0 +1,3 @@
1
+ export * from './promiseReduce'
2
+ export * from './resolveNestedPromises'
3
+ export * from './batchFn'
@@ -0,0 +1,15 @@
1
+ export function promiseReduce<ItemType = any, AccType = any>(
2
+ inputArr: ItemType[],
3
+ reducerFn: (
4
+ acc: AccType,
5
+ input: ItemType,
6
+ index: number,
7
+ ) => AccType | Promise<AccType>,
8
+ initial: AccType,
9
+ ) {
10
+ return inputArr.reduce(
11
+ (prevPromise, input, index) =>
12
+ prevPromise.then((acc) => reducerFn(acc, input, index)),
13
+ Promise.resolve(initial) as Promise<AccType>,
14
+ )
15
+ }