-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (60 loc) · 1.62 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function ignoreObject (obj){
return Buffer.isBuffer(obj)
|| (Object.hasOwnProperty.call(obj,'_bsontype') && obj._bsontype == 'Binary')
}
function isString (obj) {
return 'string' === typeof obj
}
function isNumber (obj) {
return 'number' === typeof obj && !isNaN(obj)
}
function isObject (obj) {
return obj && 'object' === typeof obj
}
function isArray (obj) {
return Array.isArray(obj)
}
function isPrimitive(obj) {
return isString(obj) || isNumber(obj)
}
function isText (obj) {
return isString() && /\s/.test(obj)
}
module.exports = pairs
function composeIndex (p,k){
return p != null ? p+"."+k : k
}
function pairs (obj, indexer) {
var all = {}
indexer = indexer || function (a, b) { return [[a, b]] }
function add (k, v, array) {
var items = indexer(k, v)
if(items != undefined) {
items.forEach(function (item) {
all[JSON.stringify(item)] = true
})
}
}
;(function _pairs (obj, p) {
for(var k in obj) {
if(p != null && !isArray(obj))
add(p, k)
if(ignoreObject(obj)) return;
if(isObject(obj[k])) {
_pairs(obj[k], isArray(obj) ? p : composeIndex(p,k), isArray(obj))
} else if(p != null)
add(isArray(obj) ? p : composeIndex(p,k), obj[k], isArray(obj))
else if(isPrimitive(obj[k]))
add(k, obj[k])
}
})(obj)
return Object.keys(all).sort().map(JSON.parse)
}
module.exports.index = pairsIndex;
function pairsIndex(key, value, emit) {
if (value && typeof value === 'object' && Object.keys(value).length > 0) {
pairs(value).forEach(function (pair) {
emit(pair);
});
}
}