@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
@@ -0,0 +1,205 @@
1
+ import { deepFreeze, wait } from '../debug'
2
+ import { resolveNestedPromises } from './resolveNestedPromises'
3
+
4
+ async function asyncDo(delay, fn) {
5
+ await wait(delay)
6
+
7
+ const result = fn()
8
+ // console.log('asyncDo', result)
9
+
10
+ return result
11
+ }
12
+
13
+ describe('resolveNestedPromises', () => {
14
+ test('basic', async () => {
15
+ const obj = {
16
+ a: asyncDo(100, () => 'A'),
17
+ b: asyncDo(300, () => 'B'),
18
+ c: asyncDo(200, () => ({
19
+ c: 'C',
20
+ d: asyncDo(200, () => 'D'),
21
+ e: {
22
+ f: {
23
+ g: asyncDo(200, () => 'G'),
24
+ },
25
+ },
26
+ })),
27
+ }
28
+
29
+ const startTime = performance.now()
30
+ const resolved = await resolveNestedPromises(obj)
31
+ const endTime = performance.now()
32
+
33
+ expect(resolved).toEqual({
34
+ a: 'A',
35
+ b: 'B',
36
+ c: {
37
+ c: 'C',
38
+ d: 'D',
39
+ e: {
40
+ f: {
41
+ g: 'G',
42
+ },
43
+ },
44
+ },
45
+ })
46
+ const executionTime = endTime - startTime
47
+
48
+ //
49
+ // Parallel promise resolution
50
+ //
51
+ expect(executionTime).toBeGreaterThan(400)
52
+ expect(executionTime).toBeLessThan(450)
53
+ })
54
+
55
+ test('deep nesting', async () => {
56
+ const obj = {
57
+ a: asyncDo(100, () => ({
58
+ a1: asyncDo(100, () => ({
59
+ a2: asyncDo(100, () => ({
60
+ a3: asyncDo(100, () => ({
61
+ a4: asyncDo(100, () => ({
62
+ a5: asyncDo(100, () => ({
63
+ a6: asyncDo(100, () => ({
64
+ a7: asyncDo(100, () => ({
65
+ a8: 'a8',
66
+ })),
67
+ })),
68
+ })),
69
+ })),
70
+ })),
71
+ })),
72
+ })),
73
+ })),
74
+ b: asyncDo(200, () => ({
75
+ c: 'C',
76
+ d: asyncDo(200, () => 'D'),
77
+ e: {
78
+ f: {
79
+ g: asyncDo(200, () => 'G'),
80
+ },
81
+ },
82
+ })),
83
+ }
84
+
85
+ const startTime = performance.now()
86
+ const resolved = await resolveNestedPromises(obj)
87
+ const endTime = performance.now()
88
+
89
+ expect(resolved).toEqual({
90
+ a: { a1: { a2: { a3: { a4: { a5: { a6: { a7: { a8: 'a8' } } } } } } } },
91
+ b: { c: 'C', d: 'D', e: { f: { g: 'G' } } },
92
+ })
93
+ const executionTime = endTime - startTime
94
+
95
+ //
96
+ // Parallel promise resolution
97
+ //
98
+ expect(executionTime).toBeGreaterThan(800)
99
+ expect(executionTime).toBeLessThan(850)
100
+ })
101
+
102
+ test('immutability', async () => {
103
+ const obj = deepFreeze({
104
+ a: asyncDo(100, () => 'A'),
105
+ b: asyncDo(100, () => 'B'),
106
+ c: asyncDo(200, () => ({
107
+ c: 'C',
108
+ d: asyncDo(200, () => 'D'),
109
+ e: {
110
+ f: {
111
+ g: asyncDo(200, () => 'G'),
112
+ },
113
+ },
114
+ })),
115
+ })
116
+
117
+ expect(await resolveNestedPromises(obj)).toEqual({
118
+ a: 'A',
119
+ b: 'B',
120
+ c: {
121
+ c: 'C',
122
+ d: 'D',
123
+ e: {
124
+ f: {
125
+ g: 'G',
126
+ },
127
+ },
128
+ },
129
+ })
130
+ })
131
+
132
+ test('maxDepth', async () => {
133
+ const obj = {
134
+ a: asyncDo(100, () => 'A'),
135
+ b: asyncDo(300, () => 'B'),
136
+ c: asyncDo(200, () => ({
137
+ c: 'C',
138
+ d: asyncDo(200, () => 'D'),
139
+ e: {
140
+ f: {
141
+ g: asyncDo(200, () => 'G'),
142
+ },
143
+ },
144
+ })),
145
+ }
146
+
147
+ expect(
148
+ await resolveNestedPromises(obj, {
149
+ maxDepth: 4,
150
+ }),
151
+ ).toEqual({
152
+ a: 'A',
153
+ b: 'B',
154
+ c: {
155
+ c: 'C',
156
+ d: 'D',
157
+ e: {
158
+ f: {
159
+ g: 'G',
160
+ },
161
+ },
162
+ },
163
+ })
164
+
165
+ await expect(() =>
166
+ resolveNestedPromises(obj, {
167
+ maxDepth: 3,
168
+ }),
169
+ ).rejects.toThrow('Max depth exceeded: { depth: 4, maxDepth: 3 }')
170
+ })
171
+
172
+ test('lazy mode', async () => {
173
+ const obj = {
174
+ a: asyncDo(100, () => 'A'),
175
+ b: asyncDo(300, () => 'B'),
176
+ c: asyncDo(200, () => ({
177
+ c: 'C',
178
+ d: asyncDo(200, () => 'D'),
179
+ e: {
180
+ f: {
181
+ g: asyncDo(200, () => 'G'),
182
+ },
183
+ },
184
+ })),
185
+ }
186
+
187
+ expect(
188
+ await resolveNestedPromises(obj, {
189
+ mode: 'lazy',
190
+ }),
191
+ ).toEqual({
192
+ a: 'A',
193
+ b: 'B',
194
+ c: {
195
+ c: 'C',
196
+ d: expect.any(Promise),
197
+ e: {
198
+ f: {
199
+ g: expect.any(Promise),
200
+ },
201
+ },
202
+ },
203
+ })
204
+ })
205
+ })
@@ -0,0 +1,83 @@
1
+ import traverse from 'traverse'
2
+ import copy from 'fast-copy'
3
+ import { AnyArray, AnyObject } from './types'
4
+
5
+ type ResolveCtx = {
6
+ root: AnyArray | AnyObject
7
+ depth: number
8
+ }
9
+
10
+ type ResolveOptions = {
11
+ mode: 'eager' | 'lazy'
12
+ maxDepth: number
13
+ clone: false | (<T = { [key: string]: any } | any[]>(obj: T) => T)
14
+ }
15
+
16
+ const DEFAULT_OPTIONS: ResolveOptions = {
17
+ mode: 'eager',
18
+ maxDepth: 30,
19
+ clone: copy,
20
+ }
21
+
22
+ function _echo<T = any>(input: T): T {
23
+ return input
24
+ }
25
+
26
+ export async function resolveNestedPromises(
27
+ node: AnyArray | AnyObject,
28
+ options: Partial<ResolveOptions> = {},
29
+ ctx: ResolveCtx = { root: node, depth: 0 },
30
+ ): Promise<AnyArray | AnyObject> {
31
+ const opts: ResolveOptions = {
32
+ ...DEFAULT_OPTIONS,
33
+ ...options,
34
+ }
35
+ let promises: Promise<void>[] = []
36
+
37
+ //
38
+ // Allow consumers to provide a clone function.
39
+ // There are multiple nuances regarding cloning. Here are some
40
+ // we've faced:
41
+ // - clone-deep (https://www.npmjs.com/package/clone-deep) does
42
+ // creates new versions of symbols
43
+ // - fast-copy (default export) does not clone array custom properties
44
+ // (but allows for creating a copier function that does)
45
+ //
46
+ // It would be incorrect to use copyStrict (from fast-copy) by default, thus
47
+ // we allow consumers to provide their copy functions
48
+ //
49
+ // If opts.clone is set to false, use the _echo function
50
+ //
51
+ const _cloneFn = opts.clone || _echo
52
+ const clonedNode = _cloneFn(node)
53
+
54
+ const resolved = traverse(clonedNode).forEach(function (value) {
55
+ const depth = ctx.depth + this.path.length
56
+
57
+ if (depth > opts.maxDepth) {
58
+ throw new Error(
59
+ `Max depth exceeded: { depth: ${depth}, maxDepth: ${opts.maxDepth} }`,
60
+ )
61
+ }
62
+
63
+ if (value instanceof Promise) {
64
+ const updatePromise = value.then(async (resolvedValue) => {
65
+ // Arrow function;
66
+ // this refers to outside this ctx
67
+ this.update(
68
+ opts.mode === 'eager'
69
+ ? await resolveNestedPromises(resolvedValue, opts, {
70
+ ...ctx,
71
+ // pass the base depth of the current value
72
+ depth,
73
+ })
74
+ : resolvedValue,
75
+ )
76
+ })
77
+
78
+ promises = [...promises, updatePromise]
79
+ }
80
+ })
81
+
82
+ return Promise.all(promises).then(() => resolved)
83
+ }
@@ -0,0 +1,2 @@
1
+ export type AnyArray = unknown[]
2
+ export type AnyObject = Record<string, unknown>
@@ -0,0 +1,42 @@
1
+ import { resolvePaths } from './'
2
+
3
+ describe('resolvePaths', () => {
4
+ test('basic', () => {
5
+ const SOURCE = {
6
+ username: 'joaosilva',
7
+ email: 'joao@example.com',
8
+ profile: {
9
+ givenName: 'João Silva',
10
+ picture: {
11
+ url: 'https://example.com/image.png',
12
+ size: {
13
+ width: 100,
14
+ height: 200,
15
+ },
16
+ },
17
+ },
18
+ }
19
+
20
+ expect(
21
+ resolvePaths(SOURCE, [
22
+ 'username',
23
+ ['primaryEmail', 'email'],
24
+ ['profilePicture.url', 'profile.picture.url'],
25
+ ['profilePicture.width', 'profile.picture.size.width'],
26
+ ['profilePicture.height', 'profile.picture.size.height'],
27
+ [
28
+ 'profilePicture.fileType',
29
+ (obj) => obj.picture?.mimeType || 'application/octet-stream',
30
+ ],
31
+ ]),
32
+ ).toEqual({
33
+ username: SOURCE.username,
34
+ primaryEmail: SOURCE.email,
35
+ profilePicture: {
36
+ url: SOURCE.profile.picture.url,
37
+ ...SOURCE.profile.picture.size,
38
+ fileType: 'application/octet-stream',
39
+ },
40
+ })
41
+ })
42
+ })
@@ -0,0 +1,21 @@
1
+ import { getProperty, setProperty } from 'dot-prop'
2
+
3
+ type Resolver = string | ((sourceObj: Record<string, any>) => any)
4
+
5
+ type PathSpec = string | [string, Resolver]
6
+
7
+ export function resolvePaths(
8
+ sourceObj: Record<string, any>,
9
+ paths: PathSpec[],
10
+ ): Record<string, any> {
11
+ return paths.reduce((acc, path) => {
12
+ const [targetPath, resolver] = Array.isArray(path) ? path : [path, path]
13
+
14
+ const value =
15
+ typeof resolver === 'string'
16
+ ? getProperty(sourceObj, resolver)
17
+ : resolver(sourceObj)
18
+
19
+ return setProperty(acc, targetPath, value)
20
+ }, {})
21
+ }
@@ -0,0 +1,30 @@
1
+ import { switchValue } from './switchValue'
2
+
3
+ describe('switchValue', () => {
4
+ test('basic', () => {
5
+ const cases = {
6
+ string: 'String value',
7
+ number: 'Number value',
8
+ regexp: 'RegExp value',
9
+ }
10
+
11
+ expect(switchValue(typeof 'string input', cases)).toEqual('String value')
12
+ expect(switchValue(typeof 8, cases)).toEqual('Number value')
13
+ })
14
+
15
+ test('function criteria', () => {
16
+ const cases = [
17
+ [
18
+ (input) => typeof input === 'string' || typeof input === 'number',
19
+ 'Number or String',
20
+ ],
21
+ [(input) => Array.isArray(input), 'Array value'],
22
+ ['Default value']
23
+ ]
24
+
25
+ expect(switchValue('str', cases)).toEqual('Number or String')
26
+ expect(switchValue(8, cases)).toEqual('Number or String')
27
+ expect(switchValue([], cases)).toEqual('Array value')
28
+ expect(switchValue({}, cases)).toEqual('Default value')
29
+ })
30
+ })
@@ -0,0 +1,59 @@
1
+ type Criteria = string | symbol | RegExp | ((input: any) => boolean)
2
+
3
+ type ObjectSwitchCases<ValueType = any> = {
4
+ [key: string | symbol]: ValueType
5
+ }
6
+
7
+ type TupleSwitchCases<ValueType = any> = ([Criteria, ValueType] | [ValueType])[]
8
+
9
+ export type SwitchCases<ValueType = any> =
10
+ | ObjectSwitchCases<ValueType>
11
+ | TupleSwitchCases<ValueType>
12
+
13
+ function testCriteria(criteria: Criteria, input: any) {
14
+ switch (typeof criteria) {
15
+ case 'function': {
16
+ return criteria(input)
17
+ }
18
+ case 'object': {
19
+ if (criteria instanceof RegExp) {
20
+ return criteria.test(input)
21
+ } else {
22
+ throw new Error('Unsupported criteria')
23
+ }
24
+ }
25
+ case 'string':
26
+ case 'symbol': {
27
+ return criteria === input
28
+ }
29
+ }
30
+ }
31
+
32
+ export function switchValue<ValueType = any>(
33
+ input: any,
34
+ cases: SwitchCases,
35
+ ): ValueType | undefined {
36
+ if (Array.isArray(cases)) {
37
+ const matching = cases.find((case_) => {
38
+ return case_.length === 1 || testCriteria(case_[0], input)
39
+ })
40
+
41
+ return matching
42
+ ? matching.length === 1
43
+ ? matching[0]
44
+ : matching[1]
45
+ : undefined
46
+ } else {
47
+ return cases[input] || cases.default
48
+ }
49
+ }
50
+
51
+ export function switchExec<ArgsType extends any[] = any[]>(
52
+ input: any,
53
+ cases: SwitchCases<(...args: any[]) => any>,
54
+ ...args: ArgsType
55
+ ) {
56
+ const fn = switchValue(input, cases)
57
+
58
+ return fn(...args)
59
+ }
@@ -0,0 +1,47 @@
1
+ import { typeOf } from './typeOf'
2
+
3
+ describe('test', () => {
4
+ test('basic', () => {
5
+ expect(typeOf({})).toEqual('object')
6
+ expect(typeOf(null)).toEqual('null')
7
+ expect(typeOf(undefined)).toEqual('undefined')
8
+ expect(typeOf(true)).toEqual('boolean')
9
+ expect(typeOf(1n)).toEqual('bigint')
10
+ expect(typeOf(Symbol())).toEqual('symbol')
11
+ expect(typeOf(() => {})).toEqual('function')
12
+ expect(typeOf('string')).toEqual('string')
13
+ expect(typeOf(6)).toEqual('number')
14
+ expect(typeOf(parseInt('('))).toEqual('nan')
15
+ expect(typeOf([6, 'str'])).toEqual('array')
16
+ expect(typeOf(new Error('Error message'))).toEqual('error')
17
+ expect(typeOf(new RegExp('^Hello'))).toEqual('regexp')
18
+ expect(typeOf(new Date())).toEqual('date')
19
+ expect(typeOf(new Map())).toEqual('map')
20
+ expect(typeOf(new WeakMap())).toEqual('weakmap')
21
+ expect(typeOf(new Set())).toEqual('set')
22
+ expect(typeOf(new WeakSet())).toEqual('weakset')
23
+ expect(typeOf(new Promise(() => {}))).toEqual('promise')
24
+ })
25
+
26
+ test('custom error', () => {
27
+ class ValidationError extends Error {
28
+ constructor(message) {
29
+ super(message) // (1)
30
+ this.name = 'ValidationError' // (2)
31
+ }
32
+ }
33
+
34
+ expect(typeOf(new ValidationError('Some error'))).toEqual('error')
35
+ })
36
+
37
+ test('unknown constructor', () => {
38
+ class Custom {
39
+ a: string
40
+ constructor(a) {
41
+ this.a = a
42
+ }
43
+ }
44
+
45
+ expect(typeOf(new Custom('a'))).toEqual(null)
46
+ })
47
+ })
package/src/typeOf.ts ADDED
@@ -0,0 +1,81 @@
1
+ import { isPlainObject } from 'is-plain-object'
2
+
3
+ export type TypeTest = (value: any) => boolean
4
+
5
+ type TypeMap = Record<string, TypeTest>
6
+
7
+ //
8
+ // The only role of this function is to let typescript
9
+ // be aware of the available types
10
+ //
11
+ export function typeMap<T extends TypeMap>(typeMap: T) {
12
+ return typeMap
13
+ }
14
+
15
+ export function makeTypeOf<T extends TypeMap>(typeMap: T) {
16
+ return function typeOf(value: any): keyof T | null {
17
+ for (const typeName in typeMap) {
18
+ if (typeMap[typeName](value)) {
19
+ return typeName
20
+ }
21
+ }
22
+ return null
23
+ }
24
+ }
25
+
26
+ function basicType(typeName: string) {
27
+ return (value: any) => typeof value === typeName
28
+ }
29
+
30
+ const DEFAULT_TYPES = typeMap({
31
+ //
32
+ // Primitives
33
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#primitive_values
34
+ //
35
+ null: (value) => value === null,
36
+ undefined: basicType('undefined'),
37
+ boolean: basicType('boolean'),
38
+
39
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
40
+ number: (value) => typeof value === 'number' && !Number.isNaN(value),
41
+ bigint: basicType('bigint'),
42
+ string: basicType('string'),
43
+ symbol: basicType('symbol'),
44
+ function: basicType('function'),
45
+
46
+ object: isPlainObject,
47
+ array: (value) => Array.isArray(value),
48
+
49
+ regexp: (value) => value instanceof RegExp,
50
+ nan: (value) => Number.isNaN(value),
51
+ date: (value) => value instanceof Date,
52
+ map: (value) => value instanceof Map,
53
+ weakmap: (value) => value instanceof WeakMap,
54
+ set: (value) => value instanceof Set,
55
+ weakset: (value) => value instanceof WeakSet,
56
+ promise: (value) => value instanceof Promise,
57
+ error: (value) => value instanceof Error,
58
+ })
59
+
60
+ export type DEFAULT_TYPE_NAMES =
61
+ | 'null'
62
+ | 'undefined'
63
+ | 'boolean'
64
+ | 'number'
65
+ | 'bigint'
66
+ | 'string'
67
+ | 'symbol'
68
+ | 'function'
69
+ | 'object'
70
+ | 'array'
71
+ | 'regexp'
72
+ | 'nan'
73
+ | 'date'
74
+ | 'map'
75
+ | 'weakmap'
76
+ | 'set'
77
+ | 'weakset'
78
+ | 'promise'
79
+ | 'error'
80
+
81
+ export const typeOf = makeTypeOf(DEFAULT_TYPES)
@@ -0,0 +1,9 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`validateAPI 1`] = `
4
+ [
5
+ "validate",
6
+ "validateAsync",
7
+ "ValidationError",
8
+ ]
9
+ `;