-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (86 loc) · 3.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict'
const { assert, reach } = require('@hapi/hoek')
const { transform } = require('./transform')
function convert (source, transforms, options) {
const transformed = transform(source, transforms, options)
return options.trim ? trim(transformed) : transformed
}
function isDefined (value) {
return ![null, undefined, void 0].includes(value) // eslint-disable-line
}
function hasChildren (value) {
return isObject(value) && Object.keys(value).length > 0
}
function isObject (value) {
return isDefined(value) && typeof value === 'object'
}
function trim (transformed) {
if (Array.isArray(transformed)) { return transformed }
const res = Object
.keys(transformed)
.reduce((result, key) => {
let value = transformed[key]
if (hasChildren(value)) {
value = trim(value)
}
if (isDefined(value)) {
result[key] = value
}
return result
}, {})
return hasChildren(res) ? res : null
}
async function doTransform (id, source, configuration) {
assert(
Object.prototype.hasOwnProperty.call(configuration, 'path'),
'Transform options should at least include `path` property.'
)
if (Object.prototype.hasOwnProperty.call(configuration, 'default')) {
assert(typeof configuration.path === 'string',
'Transformations with default values cannot be functions'
)
}
if (Object.prototype.hasOwnProperty.call(configuration, 'validate')) {
assert(
typeof configuration.validate === 'function',
'validate property should be a function which returns true or throws an error'
)
}
const { path } = configuration
const transformed = typeof path === 'function'
? await path.call(path, source)
: reach(source, path, configuration.default ? { default: configuration.default } : undefined)
const value = configuration.validate ? configuration.validate(transformed, source) : transformed
return { value, destination: `methodized.${id}` }
}
function makeArray (indexed) {
return Array.from(
Object.keys(indexed), i => indexed[i]
)
}
function noop () {
return null
}
exports.transform = async function (source, transforms, options = {}) {
const arrayOutput = Array.isArray(transforms)
const allKeys = Object.keys(transforms)
const newSource = { mappings: source, methodized: {} }
const newTransforms = {}
await Promise.all(allKeys.map(async (key, i) => {
const destinationKey = arrayOutput ? i : key
if (!transforms[key]) {
transforms[key] = noop
}
if (typeof transforms[key] === 'string') {
newTransforms[destinationKey] = `mappings.${transforms[key]}`
return
}
const id = `_key${i}`
const transformConfiguration = typeof transforms[key] === 'object' ? transforms[key] : { path: transforms[key] }
const { value, destination } = await doTransform(id, source, transformConfiguration)
newSource.methodized[id] = value
newTransforms[destinationKey] = destination
}))
const transformed = convert(newSource, newTransforms, options)
return arrayOutput ? makeArray(transformed) : transformed
}