-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
51 lines (43 loc) · 1.26 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
/*!
* stringify-keys <https://github.com/doowb/stringify-keys>
*
* Copyright (c) 2014-present, Brian Woodward.
* Released under the MIT License.
*/
'use strict';
/**
* Stringify the nested keys of `object` into dot-notation object paths.
*
* @param {Object} `object` The object to stringify
* @param {Object|String} `options` Options with `separator` to use. Default is `.`.
* @return {Array} Returns an array of object paths.
*/
module.exports = function(target, options) {
if (typeof options === 'string') {
options = { separator: options };
}
let opts = Object.assign({ separator: '.' }, options);
let sep = opts.separator;
let values = {};
let keys = [];
function stringify(obj, prev) {
for (let key of Object.keys(obj)) {
let val = obj[key];
key = (prev ? prev + sep : '') + esc(key, opts);
if (Array.isArray(val) || val !== null && typeof val === 'object') {
stringify(val, key);
} else {
keys.push(key);
values[key] = val;
}
}
}
stringify(target);
return opts.values ? values : keys;
};
function esc(key, options) {
if (typeof options.escape === 'function') {
return options.escape(key, options);
}
return key.split(options.separator).join('\\' + options.separator);
}