@orioro/util 0.1.0 → 0.3.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 (196) 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 +773 -763
  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 +47 -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 +6 -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 -8
  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/switchValue.spec.ts +0 -30
  164. package/src/switchValue.ts +0 -59
  165. package/src/typeOf.spec.ts +0 -47
  166. package/src/typeOf.ts +0 -81
  167. package/src/validate/__snapshots__/index.spec.ts.snap +0 -9
  168. package/src/validate/async/index.spec.ts +0 -236
  169. package/src/validate/async/index.ts +0 -52
  170. package/src/validate/async/validateAsyncFn.ts +0 -41
  171. package/src/validate/async/validators/index.ts +0 -2
  172. package/src/validate/async/validators/logical.ts +0 -56
  173. package/src/validate/async/validators/shape.ts +0 -160
  174. package/src/validate/async/validators/tmpand.ts +0 -24
  175. package/src/validate/async/validators/tmpor.ts +0 -21
  176. package/src/validate/common/ValidationError.ts +0 -33
  177. package/src/validate/common/util/defaultErrorMessage.ts +0 -26
  178. package/src/validate/common/util/index.ts +0 -3
  179. package/src/validate/common/util/parseValidatorInput.ts +0 -77
  180. package/src/validate/common/util/resolveValidationResult.ts +0 -64
  181. package/src/validate/common/validators/index.ts +0 -1
  182. package/src/validate/common/validators/type.ts +0 -101
  183. package/src/validate/index.spec.ts +0 -5
  184. package/src/validate/index.ts +0 -3
  185. package/src/validate/specUtil/commonTests.js +0 -413
  186. package/src/validate/sync/index.spec.ts +0 -81
  187. package/src/validate/sync/index.ts +0 -53
  188. package/src/validate/sync/validateSyncFn.ts +0 -46
  189. package/src/validate/sync/validators/index.ts +0 -2
  190. package/src/validate/sync/validators/logical.ts +0 -47
  191. package/src/validate/sync/validators/shape.ts +0 -146
  192. package/src/validate/types/async.ts +0 -20
  193. package/src/validate/types/common.ts +0 -70
  194. package/src/validate/types/index.ts +0 -3
  195. package/src/validate/types/sync.ts +0 -20
  196. package/tsconfig.json +0 -11
@@ -1,205 +0,0 @@
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
- })
@@ -1,83 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- export type AnyArray = unknown[]
2
- export type AnyObject = Record<string, unknown>
@@ -1,30 +0,0 @@
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
- })
@@ -1,59 +0,0 @@
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
- }
@@ -1,47 +0,0 @@
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 DELETED
@@ -1,81 +0,0 @@
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)
@@ -1,9 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`validateAPI 1`] = `
4
- [
5
- "validate",
6
- "validateAsync",
7
- "ValidationError",
8
- ]
9
- `;