-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
40 lines (29 loc) · 1019 Bytes
/
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
var push = Array.prototype.push
, toString = Object.prototype.toString
function isObject (o) {
return toString.call(o) === '[object Object]'
}
function prefix (pref) {
return function (str) {
return pref + str
}
}
function toSnakeCase (str, sep) {
return str.replace(/([a-z])([A-Z])/g, '$1'+sep+'$2')
}
function flatKeys (obj, options) {
var options = options || {}
, sep = options.sep || '_'
, snake = options.snake != null ? options.snake : true
, filter = options.filter != null ? options.filter : String.prototype.toLowerCase
return Object.keys(obj).reduce(function(keys, key){
var _key = snake ? toSnakeCase(key, sep) : key
, value = obj[key]
if (typeof filter === 'function') _key = filter.call(_key)
isObject(value)
? push.apply(keys, flatKeys(value, options).map(prefix(_key + sep)))
: push.call(keys, _key)
return keys
}, [])
}
module.exports = flatKeys