@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.
- package/dist/PromiseLikeEventEmitter/index.d.ts +10 -0
- package/dist/array/arrayChunk.d.ts +1 -0
- package/dist/array/index.d.ts +1 -0
- package/dist/debug/debugFn/index.d.ts +15 -0
- package/dist/debug/debugFn/util.d.ts +1 -0
- package/dist/debug/debugFn.d.ts +15 -0
- package/dist/debug/deepFreeze.d.ts +5 -0
- package/dist/debug/index.d.ts +3 -0
- package/dist/debug/wait.d.ts +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.mjs +21 -14
- package/dist/interpolate/index.d.ts +14 -0
- package/dist/maybeFn.d.ts +1 -0
- package/dist/pickPaths/index.d.ts +4 -0
- package/dist/promise/batchFn.d.ts +49 -0
- package/dist/promise/index.d.ts +3 -0
- package/dist/promise/promiseReduce.d.ts +1 -0
- package/dist/promise/resolveNestedPromises.d.ts +14 -0
- package/dist/promise/types.d.ts +2 -0
- package/dist/resolvePaths/index.d.ts +4 -0
- package/dist/switchValue.d.ts +9 -0
- package/dist/typeOf.d.ts +7 -0
- package/dist/validate/async/index.d.ts +18 -0
- package/dist/validate/async/validateAsyncFn.d.ts +2 -0
- package/dist/validate/async/validators/index.d.ts +2 -0
- package/dist/validate/async/validators/logical.d.ts +4 -0
- package/dist/validate/async/validators/shape.d.ts +7 -0
- package/dist/validate/async/validators/tmpand.d.ts +2 -0
- package/dist/validate/async/validators/tmpor.d.ts +2 -0
- package/dist/validate/common/ValidationError.d.ts +11 -0
- package/dist/validate/common/util/defaultErrorMessage.d.ts +2 -0
- package/dist/validate/common/util/index.d.ts +3 -0
- package/dist/validate/common/util/parseValidatorInput.d.ts +5 -0
- package/dist/validate/common/util/resolveValidationResult.d.ts +6 -0
- package/dist/validate/common/validators/index.d.ts +1 -0
- package/dist/validate/common/validators/type.d.ts +9 -0
- package/dist/validate/index.d.ts +3 -0
- package/dist/validate/sync/index.d.ts +18 -0
- package/dist/validate/sync/validateSyncFn.d.ts +2 -0
- package/dist/validate/sync/validators/index.d.ts +2 -0
- package/dist/validate/sync/validators/logical.d.ts +4 -0
- package/dist/validate/sync/validators/shape.d.ts +7 -0
- package/dist/validate/types/async.d.ts +5 -0
- package/dist/validate/types/common.d.ts +27 -0
- package/dist/validate/types/index.d.ts +3 -0
- package/dist/validate/types/sync.d.ts +5 -0
- package/package.json +7 -2
- package/babel.config.cjs +0 -13
- package/coverage/clover.xml +0 -491
- package/coverage/coverage-final.json +0 -29
- package/coverage/lcov-report/ValidationError.ts.html +0 -184
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -87
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -296
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -196
- package/coverage/lcov-report/src/debug/deepFreeze.ts.html +0 -157
- package/coverage/lcov-report/src/debug/index.html +0 -146
- package/coverage/lcov-report/src/debug/index.ts.html +0 -91
- package/coverage/lcov-report/src/debug/wait.ts.html +0 -127
- package/coverage/lcov-report/src/index.html +0 -131
- package/coverage/lcov-report/src/interpolate/index.html +0 -116
- package/coverage/lcov-report/src/interpolate/index.ts.html +0 -277
- package/coverage/lcov-report/src/maybeFn.ts.html +0 -94
- package/coverage/lcov-report/src/promise/index.html +0 -146
- package/coverage/lcov-report/src/promise/index.ts.html +0 -91
- package/coverage/lcov-report/src/promise/promiseReduce.ts.html +0 -130
- package/coverage/lcov-report/src/promise/resolveNestedPromises.ts.html +0 -271
- package/coverage/lcov-report/src/switchValue.ts.html +0 -253
- package/coverage/lcov-report/src/typeOf.ts.html +0 -328
- package/coverage/lcov-report/src/validate/ValidationError.ts.html +0 -184
- package/coverage/lcov-report/src/validate/async/index.html +0 -131
- package/coverage/lcov-report/src/validate/async/index.ts.html +0 -241
- package/coverage/lcov-report/src/validate/async/parseValidator.ts.html +0 -136
- package/coverage/lcov-report/src/validate/async/validateAsyncFn.ts.html +0 -208
- package/coverage/lcov-report/src/validate/async/validators/and.ts.html +0 -154
- package/coverage/lcov-report/src/validate/async/validators/index.html +0 -146
- package/coverage/lcov-report/src/validate/async/validators/index.ts.html +0 -91
- package/coverage/lcov-report/src/validate/async/validators/logical.ts.html +0 -253
- package/coverage/lcov-report/src/validate/async/validators/or.ts.html +0 -151
- package/coverage/lcov-report/src/validate/async/validators/shape.ts.html +0 -565
- package/coverage/lcov-report/src/validate/common/ValidationError.ts.html +0 -184
- package/coverage/lcov-report/src/validate/common/index.html +0 -116
- package/coverage/lcov-report/src/validate/common/util/defaultErrorMessage.ts.html +0 -163
- package/coverage/lcov-report/src/validate/common/util/index.html +0 -161
- package/coverage/lcov-report/src/validate/common/util/index.ts.html +0 -94
- package/coverage/lcov-report/src/validate/common/util/parseValidator.ts.html +0 -316
- package/coverage/lcov-report/src/validate/common/util/parseValidatorInput.ts.html +0 -316
- package/coverage/lcov-report/src/validate/common/util/resolveValidationResult.ts.html +0 -277
- package/coverage/lcov-report/src/validate/common/util/validatorParser.ts.html +0 -316
- package/coverage/lcov-report/src/validate/common/validators/index.html +0 -131
- package/coverage/lcov-report/src/validate/common/validators/index.ts.html +0 -88
- package/coverage/lcov-report/src/validate/common/validators/type.ts.html +0 -388
- package/coverage/lcov-report/src/validate/fmtValidationResult.ts.html +0 -268
- package/coverage/lcov-report/src/validate/index.html +0 -116
- package/coverage/lcov-report/src/validate/index.ts.html +0 -94
- package/coverage/lcov-report/src/validate/makeValidate.ts.html +0 -634
- package/coverage/lcov-report/src/validate/specUtil/commonTests.js.html +0 -1324
- package/coverage/lcov-report/src/validate/specUtil/index.html +0 -116
- package/coverage/lcov-report/src/validate/sync/index.html +0 -131
- package/coverage/lcov-report/src/validate/sync/index.ts.html +0 -244
- package/coverage/lcov-report/src/validate/sync/parseValidator.ts.html +0 -136
- package/coverage/lcov-report/src/validate/sync/validateSyncFn.ts.html +0 -223
- package/coverage/lcov-report/src/validate/sync/validators/and.ts.html +0 -148
- package/coverage/lcov-report/src/validate/sync/validators/index.html +0 -146
- package/coverage/lcov-report/src/validate/sync/validators/index.ts.html +0 -91
- package/coverage/lcov-report/src/validate/sync/validators/logical.ts.html +0 -226
- package/coverage/lcov-report/src/validate/sync/validators/or.ts.html +0 -130
- package/coverage/lcov-report/src/validate/sync/validators/shape.ts.html +0 -523
- package/coverage/lcov-report/src/validate/sync/validators/type.ts.html +0 -154
- package/coverage/lcov-report/src/validate/syncValidators/and.ts.html +0 -157
- package/coverage/lcov-report/src/validate/syncValidators/index.html +0 -176
- package/coverage/lcov-report/src/validate/syncValidators/index.ts.html +0 -97
- package/coverage/lcov-report/src/validate/syncValidators/or.ts.html +0 -127
- package/coverage/lcov-report/src/validate/syncValidators/shape.ts.html +0 -559
- package/coverage/lcov-report/src/validate/syncValidators/string.ts.html +0 -163
- package/coverage/lcov-report/src/validate/syncValidators/type.ts.html +0 -154
- package/coverage/lcov-report/src/validate/util/defaultErrorMessage.ts.html +0 -169
- package/coverage/lcov-report/src/validate/util/index.html +0 -146
- package/coverage/lcov-report/src/validate/util/index.ts.html +0 -91
- package/coverage/lcov-report/src/validate/util/resolveValidationResult.ts.html +0 -253
- package/coverage/lcov-report/src/validate/validate.ts.html +0 -220
- package/coverage/lcov-report/src/validate/validateAsync.ts.html +0 -220
- package/coverage/lcov-report/src/validate/validators/and.ts.html +0 -157
- package/coverage/lcov-report/src/validate/validators/index.html +0 -176
- package/coverage/lcov-report/src/validate/validators/index.ts.html +0 -97
- package/coverage/lcov-report/src/validate/validators/or.ts.html +0 -127
- package/coverage/lcov-report/src/validate/validators/shape.ts.html +0 -541
- package/coverage/lcov-report/src/validate/validators/type.ts.html +0 -154
- package/coverage/lcov-report/src/validate_/ValidationError.ts.html +0 -184
- package/coverage/lcov-report/src/validate_/fmtValidationResult.ts.html +0 -268
- package/coverage/lcov-report/src/validate_/index.html +0 -161
- package/coverage/lcov-report/src/validate_/makeValidate.ts.html +0 -634
- package/coverage/lcov-report/src/validate_/validate.ts.html +0 -220
- package/coverage/lcov-report/switchValue.ts.html +0 -253
- package/coverage/lcov-report/typeOf.ts.html +0 -331
- package/coverage/lcov-report/validate.ts.html +0 -757
- package/coverage/lcov.info +0 -1056
- package/jest.config.js +0 -6
- package/rollup.config.mjs +0 -6
- package/src/PromiseLikeEventEmitter/index.ts +0 -35
- package/src/array/arrayChunk.ts +0 -7
- package/src/array/index.ts +0 -1
- package/src/debug/debugFn/index.ts +0 -48
- package/src/debug/debugFn/util.ts +0 -27
- package/src/debug/deepFreeze.ts +0 -26
- package/src/debug/index.ts +0 -3
- package/src/debug/wait.ts +0 -14
- package/src/index.ts +0 -9
- package/src/interpolate/index.spec.ts +0 -20
- package/src/interpolate/index.ts +0 -64
- package/src/maybeFn.ts +0 -3
- package/src/promise/batchFn.spec.ts +0 -92
- package/src/promise/batchFn.ts +0 -176
- package/src/promise/index.ts +0 -3
- package/src/promise/promiseReduce.ts +0 -15
- package/src/promise/resolveNestedPromises.spec.ts +0 -205
- package/src/promise/resolveNestedPromises.ts +0 -83
- package/src/promise/types.ts +0 -2
- package/src/resolvePaths/index.spec.ts +0 -42
- package/src/resolvePaths/index.ts +0 -21
- package/src/switchValue.spec.ts +0 -30
- package/src/switchValue.ts +0 -59
- package/src/typeOf.spec.ts +0 -47
- package/src/typeOf.ts +0 -81
- package/src/validate/__snapshots__/index.spec.ts.snap +0 -9
- package/src/validate/async/index.spec.ts +0 -236
- package/src/validate/async/index.ts +0 -52
- package/src/validate/async/validateAsyncFn.ts +0 -41
- package/src/validate/async/validators/index.ts +0 -2
- package/src/validate/async/validators/logical.ts +0 -56
- package/src/validate/async/validators/shape.ts +0 -160
- package/src/validate/async/validators/tmpand.ts +0 -24
- package/src/validate/async/validators/tmpor.ts +0 -21
- package/src/validate/common/ValidationError.ts +0 -33
- package/src/validate/common/util/defaultErrorMessage.ts +0 -26
- package/src/validate/common/util/index.ts +0 -3
- package/src/validate/common/util/parseValidatorInput.ts +0 -77
- package/src/validate/common/util/resolveValidationResult.ts +0 -64
- package/src/validate/common/validators/index.ts +0 -1
- package/src/validate/common/validators/type.ts +0 -101
- package/src/validate/index.spec.ts +0 -5
- package/src/validate/index.ts +0 -3
- package/src/validate/specUtil/commonTests.js +0 -413
- package/src/validate/sync/index.spec.ts +0 -81
- package/src/validate/sync/index.ts +0 -53
- package/src/validate/sync/validateSyncFn.ts +0 -46
- package/src/validate/sync/validators/index.ts +0 -2
- package/src/validate/sync/validators/logical.ts +0 -47
- package/src/validate/sync/validators/shape.ts +0 -146
- package/src/validate/types/async.ts +0 -20
- package/src/validate/types/common.ts +0 -70
- package/src/validate/types/index.ts +0 -3
- package/src/validate/types/sync.ts +0 -20
- 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
|
-
}
|
package/src/promise/types.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,21 +0,0 @@
|
|
|
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
|
-
}
|
package/src/switchValue.spec.ts
DELETED
|
@@ -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
|
-
})
|
package/src/switchValue.ts
DELETED
|
@@ -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
|
-
}
|
package/src/typeOf.spec.ts
DELETED
|
@@ -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)
|