@orioro/util 0.1.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.
- package/dist/index.mjs +773 -763
- package/package.json +1 -1
- package/src/index.ts +7 -6
- package/src/resolvePaths/index.spec.ts +42 -0
- package/src/resolvePaths/index.ts +21 -0
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './validate'
|
|
4
|
-
export * from './promise'
|
|
1
|
+
export * from './PromiseLikeEventEmitter'
|
|
2
|
+
export * from './debug'
|
|
5
3
|
export * from './interpolate'
|
|
6
4
|
export * from './maybeFn'
|
|
7
|
-
export * from './
|
|
8
|
-
export * from './
|
|
5
|
+
export * from './promise'
|
|
6
|
+
export * from './resolvePaths'
|
|
7
|
+
export * from './switchValue'
|
|
8
|
+
export * from './typeOf'
|
|
9
|
+
export * from './validate'
|
|
@@ -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
|
+
}
|