This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.js
255 lines (212 loc) · 5.64 KB
/
objects.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// adds all the fields from obj2 onto obj1
//
var each = exports.each = function (obj,iterator){
var keys = Object.keys(obj)
keys.forEach(function (key){
iterator(obj[key],key,obj)
})
}
var merge = exports.merge = function (a, b) {
if(arguments.length <= 1)
return a
if(a == null)
return merge.apply(null, [].slice.call(arguments, 1))
if(b != null)
each(b, function (v,k){
a[k] = v
})
return merge.apply(null, [a].concat([].slice.call(arguments, 2)))
}
/*var merge = exports.merge = function (){
var args = [].slice.call(arguments)
var obj1 = null, obj2 = null
while (args.length && obj1 == null) //skip null and undefined
obj1 = args.shift()
while(args.length) {
obj2 = null
while (args.length && obj1 == null) //skip null and undefined
obj2 = args.shift()
var keys = Object.keys(obj2)
each(obj2, function (v,k){
obj1[k] = v
})
}
return obj1
}*/
var RX = /sadf/.constructor
function rx (iterator ){
return iterator instanceof RX ? function (str) {
var m = iterator.exec(str)
return m && (m[1] ? m[1] : m[0])
} : iterator
}
var times = exports.times = function () {
var args = [].slice.call(arguments)
, iterator = rx(args.pop())
, m = args.pop()
, i = args.shift()
, j = args.shift()
, diff, dir
, a = []
i = 'number' === typeof i ? i : 1
diff = j ? j - i : 1
dir = i < m
for (; dir ? i <= m : m <= i; i += diff)
a.push(iterator(i))
return a
}
var map = exports.map = function (obj, iterator){
iterator = rx(iterator)
if(Array.isArray(obj))
return obj.map(iterator)
if('number' === typeof obj)
return times.apply(null, [].slice.call(arguments))
//return if null ?
var keys = Object.keys(obj)
, r = {}
keys.forEach(function (key){
r[key] = iterator(obj[key],key,obj)
})
return r
}
var findReturn = exports.findReturn = function (obj, iterator) {
iterator = rx(iterator)
if(obj == null)
return
var keys = Object.keys(obj)
, l = keys.length
for (var i = 0; i < l; i ++) {
var key = keys[i]
, value = obj[key]
var r = iterator(value, key)
if(r) return r
}
}
var find = exports.find = function (obj, iterator) {
iterator = rx(iterator)
return findReturn (obj, function (v, k) {
var r = iterator(v, k)
if(r) return v
})
}
var findKey = exports.findKey = function (obj, iterator) {
iterator = rx(iterator)
return findReturn (obj, function (v, k) {
var r = iterator(v, k)
if(r) return k
})
}
var filter = exports.filter = function (obj, iterator){
iterator = rx (iterator)
if(Array.isArray(obj))
return obj.filter(iterator)
var keys = Object.keys(obj)
, r = {}
keys.forEach(function (key){
var v
if(iterator(v = obj[key],key,obj))
r[key] = v
})
return r
}
var mapKeys = exports.mapKeys = function (ary, iterator){
var r = {}
iterator = rx(iterator)
each(ary, function (v,k){
r[v] = iterator(v,k)
})
return r
}
var mapToArray = exports.mapToArray = function (ary, iterator){
var r = []
iterator = rx(iterator)
each(ary, function (v,k){
r.push(iterator(v,k))
})
return r
}
diff = exports.diff = function (old, nw) {
var ab = deepMerge (nw, old)
, s = {}
each(ab, function (ignore, k) {
//if the property is not in the new object, it must have been deleted.
if (nw[k] == null)
s[k] = null //null on a diff means to delete that property.
else if ('object' === typeof nw[k] && 'object' === typeof old[k] && old[k])
s[k] = diff(old[k], nw[k])
else if (nw[k] !== old[k])
s[k] = nw[k] === undefined ? null : nw[k]
})
return s
}
patch = exports.patch = function (old, ptch) {
var nw = deepMerge({}, old)
each(ptch, function (ignore, k) {
//if the property is not in the new object, it must have been deleted.
if (ptch[k] === null)
delete nw[k]
else if ('object' === typeof ptch[k])
nw[k] = patch(old[k], ptch[k])
else
nw[k] = ptch[k]
})
return nw
}
deepMerge = exports.deepMerge = function (old, nw) {
var ab = merge({}, nw, old)
, s = Array.isArray(nw) ? [] : {}
each(ab, function (ignore, k) { //on each key in ab,
s[k] = (nw[k] === undefined ? old[k] : nw[k])
if ('object' === typeof nw[k] && 'object' === typeof old[k] && old[k] && nw[k] && old[k]) {
s[k] = deepMerge (old[k], nw[k])
}
})
return s
}
var path = exports.path = function (object, path) {
for (var i in path) {
if(object == null) return undefined
var key = path[i]
object = object[key]
}
return object
}
var eachPath = exports.eachPath = function (object, opts, iterator) {
// function iterator (value, path)
}
/*
NOTE: naive implementation.
`match` must not contain circular references.
*/
var has = exports.has = function (obj, match) {
if(obj == null && match != null)
return false
for (var key in match) {
//deep/complex checks
if('object' == typeof match[key]) {
if(!has(obj[key], match[key]))
return false
} else if ('function' == typeof match[key]) {
if (!match[key](obj[key]))
return false
} else if(match[key] !== obj[key])
return false
//if match is a function, apply it to the corrisponding obj[key]
}
return true
}
var setPath = exports.setPath = function (object, path, value) {
for (var i in path) {
var key = path[i]
if(object[key] == null) object[key] = (
i + 1 == path.length ? value : {}
)
object = object[key]
}
}
var enqueue = exports.enqueue = function (queue, value) {
if(!~queue.indexOf(value))
return queue.push(value), true
return false
}