-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmake.js
192 lines (164 loc) · 4.4 KB
/
make.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var u = require('./util')
function isBasicMap (rule) {
if(u.isString(rule)) return function (value) {
return value != null ? value[rule] : undefined
}
//negative numbers access from the end of the list.
if(u.isInteger(rule)) return function (value) {
return rule >= 0 ? value[rule] : value[+value.length + rule]
}
if(true === rule)
return function (value) { return value }
}
function isBasicLiteral (rule) {
if(u.isBasic(rule))
return function (v) { return rule === v }
}
function isFun (rule) {
if(u.isFunction(rule)) return rule
}
function isArrayCompose (rule) {
if(u.isArray(rule)) {
var rules = rule.map(make)
return function (value) {
return rules.reduce(function (value, fn) {
return fn(value)
}, value)
}
}
}
function isUnknown(rule) {
throw new Error('could not process:'+JSON.stringify(rule))
}
function map$ (obj, map) {
var $obj = {}
for(var k in obj)
$obj['$'+k] = map(obj[k])
return $obj
}
var maps = map$(require('./simple/maps'), function (fn) {
return function (argument) {
return function (value) {
return fn(value, argument)
}
}
})
function and (a, b) {
if(!a) return b
return function (val) {
return a(val) && b(val)
}
}
function isMap(obj) {
var fn
for(var k in obj)
if(maps[k]) fn = and(fn, maps[k].call(make, obj[k]))
return fn
}
function applyFirst(fns) {
return function (query) {
for(var k in query)
if(fns[k]) return fns[k](query[k], query)
}
}
var isReduce = applyFirst(map$(require('./simple/reduces'), function (reduce) {
return function (argument) {
var get = make(argument)
return function (a, b) {
return reduce(a, get(b))
}
}
}))
var compare = require('typewiselite')
var search = require('binary-search')
function is$ (obj) {
for(var k in obj)
if(k[0] === '$') return true
return false
}
//rawpaths, reducedpaths, reduce
function arrayGroup (set, get, reduce) {
//we can use a different lookup path on the right hand object
//is always the "needle"
function _compare (hay, needle) {
for(var i in set) {
var x = u.get(hay, set[i]), y = needle[i]
if(x !== y) return compare(x, y)
}
return 0
}
return function (a, b) {
if(a && !Array.isArray(a)) a = reduce([], a)
var A = a = a || []
var i = search(A, get.map(function (fn) { return fn(b) }), _compare)
if(i >= 0) A[i] = reduce(A[i], b)
else A.splice(~i, 0, reduce(undefined, b))
return a
}
}
var special = {
filter: function makeFilter (rule) {
if(u.isContainer(rule) && !is$(rule)) { //array or object
rule = u.map(rule, makeFilter)
return function (value) {
if(value == null) return false
for(var k in rule)
if(!rule[k](value[k])) return false
return true
}
}
//now only values at the end...
return isBasicLiteral(rule) || make(rule)
},
map: function makeMap (rule) {
if(u.isObject(rule) && !is$(rule)) {
var rules = u.map(rule, makeMap)
return function (value) {
if(value == null) return undefined
var keys = 0
var ret = u.map(rules, function (fn, key) {
if(rule[key] === true) {
keys ++
return value && value[key]
}
keys ++
return fn(value)
})
return keys ? ret : undefined
}
}
return make(rule)
},
reduce: function (rule) {
var gets = [], sets = []
var reduce = (function makeReduce (rule, path) {
if(u.isObject(rule) && !is$(rule)) {
var rules = u.map(rule, function (rule, k) {
return makeReduce(rule, path.concat(k))
})
return function (a, b) {
if(!a) a = {}
return u.map(rules, function (reduce, key) {
return a[key] = reduce.length === 1
? reduce(b) : reduce(a[key], b)
})
}
}
else {
var fn = make(rule)
if(fn.length === 1) { gets.push(fn); sets.push(path.length == 1 ? path[0] : path) }
return fn
}
})(rule, [])
return gets.length
? arrayGroup(sets, gets, reduce) : reduce
}
}
var isSpecial = applyFirst(map$(special, function (fn) {
return function (query) { return fn(query) }
}))
function make (rule) {
return isBasicMap(rule) || isFun(rule) || isArrayCompose(rule) ||
isMap(rule) || isReduce(rule) || isSpecial(rule) || isUnknown(rule)
}
module.exports = make