-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
34 lines (30 loc) · 1.22 KB
/
index.js
File metadata and controls
34 lines (30 loc) · 1.22 KB
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
const flatPlainObj = function(originObj, argOptions) {
const options = argOptions || {}
const keypathDelimiter = options.delimiter || '.'
const flatArrayFlag = options.flatArrayFlag || false
if(typeof originObj !== 'object')
throw new Error("Bad parameter, the originObj must be a plain object")
if(typeof keypathDelimiter !== 'string')
throw new Error("Bad parameter, the delimiter must be a string")
if(typeof flatArrayFlag !== 'boolean')
throw new Error("Bad parameter, the flatArrayFlag must be a boolean value")
var r = {}
const path = ''
if(JSON.stringify(originObj) !== '{}')
flatting(originObj, path, r, keypathDelimiter, flatArrayFlag)
return r
}
const flatting = function(obj, path, r, delimiter, flatArrayFlag) {
if(typeof obj !== 'object' || (typeof obj === 'object' && JSON.stringify(obj) === '{}')
|| (!flatArrayFlag && (obj instanceof Array))){
path = path.slice(1)
r[path] = obj
}
else{
const keys = Object.keys(obj)
for(var i = 0, len = keys.length; i < len; i++){
flatting(obj[keys[i]], path+delimiter+keys[i], r, delimiter, flatArrayFlag)
}
}
}
module.exports = flatPlainObj