forked from watson/flatten-obj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (33 loc) · 869 Bytes
/
index.js
File metadata and controls
42 lines (33 loc) · 869 Bytes
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
'use strict'
var isObj = require('isobj')
module.exports = function (options) {
options = options || {}
var blacklist = options.blacklist || []
var separator = options.separator == null ? '.' : options.separator
return flatten
function flatten (obj) {
var result = {}
iterator(obj, '', result)
return result
}
function iterator (obj, prefix, flattened) {
var n = 0
var keys = Object.keys(obj)
var len = keys.length
var key, val
for (; n < len; n++) {
key = keys[n]
val = obj[key]
if (isObj(val) && !isBlacklisted(val)) {
iterator(val, prefix + key + separator, flattened)
continue
}
flattened[prefix + key] = val
}
}
function isBlacklisted (obj) {
for (var i = 0; i < blacklist.length; i++) {
if (obj instanceof blacklist[i]) return true
}
}
}