aneka 0.6.0 → 0.7.1
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/CHANGES.md +2 -0
- package/index.js +3 -0
- package/package.json +1 -1
- package/src/parse-object.js +70 -0
package/CHANGES.md
CHANGED
package/index.js
CHANGED
|
@@ -24,6 +24,7 @@ import currentLoc from './src/current-loc.js'
|
|
|
24
24
|
import parseArgsArgv from './src/parse-args-argv.js'
|
|
25
25
|
import parseEnv from './src/parse-env.js'
|
|
26
26
|
import parseDuration from './src/parse-duration.js'
|
|
27
|
+
import parseObject from './src/parse-object.js'
|
|
27
28
|
|
|
28
29
|
export {
|
|
29
30
|
arrangeArray,
|
|
@@ -46,6 +47,7 @@ export {
|
|
|
46
47
|
parseArgsArgv,
|
|
47
48
|
parseDuration,
|
|
48
49
|
parseEnv,
|
|
50
|
+
parseObject,
|
|
49
51
|
pascalCase,
|
|
50
52
|
resolvePath,
|
|
51
53
|
round,
|
|
@@ -76,6 +78,7 @@ export default {
|
|
|
76
78
|
parseArgsArgv,
|
|
77
79
|
parseDuration,
|
|
78
80
|
parseEnv,
|
|
81
|
+
parseObject,
|
|
79
82
|
pascalCase,
|
|
80
83
|
resolvePath,
|
|
81
84
|
round,
|
package/package.json
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import isSet from './is-set.js'
|
|
2
|
+
import parseDuration from './parse-duration.js'
|
|
3
|
+
import dotenvParseVariables from 'dotenv-parse-variables'
|
|
4
|
+
import { set, isString, cloneDeep, isPlainObject, isArray, omit } from 'lodash-es'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Parse an object and optionally normalize its values recursively. Use {@link https://github.com/ladjs/dotenv-parse-variables}
|
|
8
|
+
* to parse values, so please have a visit to know how it works
|
|
9
|
+
*
|
|
10
|
+
* If ```options.parseValue``` is ```true```, any key ends with ```Dur``` and ```Dt``` will
|
|
11
|
+
* also be parsed as millisecond and Javascript date time accordingly.
|
|
12
|
+
*
|
|
13
|
+
* @param {(Object|string)} input - If string is given, parse it first using JSON.parse
|
|
14
|
+
* @param {Object} [options={}] - Options
|
|
15
|
+
* @param {boolean} [options.silent=true] - If ```true``` (default), exception are not thrown and silently ignored
|
|
16
|
+
* @param {boolean} [options.parseValue=false] - If ```true```, values will be parsed & normalized
|
|
17
|
+
* @param {function(string): string} [options.translator] - If provided, translate string value with this function
|
|
18
|
+
* @returns {Object}
|
|
19
|
+
* @see {@link https://github.com/ladjs/dotenv-parse-variables}
|
|
20
|
+
*/
|
|
21
|
+
function parseObject (input, options = {}) {
|
|
22
|
+
const { silent = true, parseValue = false, translate } = options
|
|
23
|
+
const statics = ['*']
|
|
24
|
+
if (isString(input)) {
|
|
25
|
+
try {
|
|
26
|
+
input = JSON.parse(input)
|
|
27
|
+
} catch (err) {
|
|
28
|
+
if (silent) input = {}
|
|
29
|
+
else throw err
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
let obj = cloneDeep(input)
|
|
33
|
+
const keys = Object.keys(obj)
|
|
34
|
+
const mutated = []
|
|
35
|
+
keys.forEach(k => {
|
|
36
|
+
let v = obj[k]
|
|
37
|
+
if (isPlainObject(v)) obj[k] = parseObject(v, options)
|
|
38
|
+
else if (isArray(v)) {
|
|
39
|
+
v.forEach((i, idx) => {
|
|
40
|
+
if (isPlainObject(i)) obj[k][idx] = parseObject(i, options)
|
|
41
|
+
else if (statics.includes(i)) obj[k][idx] = i
|
|
42
|
+
else if (parseValue) obj[k][idx] = dotenvParseVariables(set({}, 'item', obj[k][idx]), { assignToProcessEnv: false }).item
|
|
43
|
+
if (isArray(obj[k][idx])) obj[k][idx] = obj[k][idx].map(item => typeof item === 'string' ? item.trim() : item)
|
|
44
|
+
})
|
|
45
|
+
} else if (isSet(v)) {
|
|
46
|
+
if (isString(v) && translate) v = translate(v.slice(2))
|
|
47
|
+
try {
|
|
48
|
+
if (statics.includes(v)) obj[k] = v
|
|
49
|
+
else if (k.startsWith('t:') && isString(v)) {
|
|
50
|
+
const newK = k.slice(2)
|
|
51
|
+
if (translate) obj[newK] = translate(v)
|
|
52
|
+
else obj[newK] = v
|
|
53
|
+
mutated.push(k)
|
|
54
|
+
} else if (parseValue) {
|
|
55
|
+
obj[k] = dotenvParseVariables(set({}, 'item', v), { assignToProcessEnv: false }).item
|
|
56
|
+
if (isArray(obj[k])) obj[k] = obj[k].map(item => typeof item === 'string' ? item.trim() : item)
|
|
57
|
+
}
|
|
58
|
+
if (k.slice(-3) === 'Dur') obj[k] = parseDuration(v)
|
|
59
|
+
if (k.slice(-2) === 'Dt') obj[k] = Date.parse(v)
|
|
60
|
+
} catch (err) {
|
|
61
|
+
obj[k] = undefined
|
|
62
|
+
if (!silent) throw err
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
if (mutated.length > 0) obj = omit(obj, mutated)
|
|
67
|
+
return obj
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export default parseObject
|