forked from mWater/minimongo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEJSON.ts
More file actions
312 lines (296 loc) · 8.93 KB
/
Copy pathEJSON.ts
File metadata and controls
312 lines (296 loc) · 8.93 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
var _ = require("lodash")
EJSON = {} // Global!
var customTypes = {}
// Add a custom type, using a method of your choice to get to and
// from a basic JSON-able representation. The factory argument
// is a function of JSON-able --> your object
// The type you add must have:
// - A clone() method, so that Meteor can deep-copy it when necessary.
// - A equals() method, so that Meteor can compare it
// - A toJSONValue() method, so that Meteor can serialize it
// - a typeName() method, to show how to look it up in our type table.
// It is okay if these methods are monkey-patched on.
EJSON.addType = function (name: any, factory: any) {
if (_.has(customTypes, name)) throw new Error("Type " + name + " already present")
customTypes[name] = factory
}
var builtinConverters = [
{
// Date
matchJSONValue: function (obj: any) {
return _.has(obj, "$date") && _.size(obj) === 1
},
matchObject: function (obj: any) {
return obj instanceof Date
},
toJSONValue: function (obj: any) {
return { $date: obj.getTime() }
},
fromJSONValue: function (obj: any) {
return new Date(obj.$date)
}
},
{
// Binary
matchJSONValue: function (obj: any) {
return _.has(obj, "$binary") && _.size(obj) === 1
},
matchObject: function (obj: any) {
return (
(typeof Uint8Array !== "undefined" && obj instanceof Uint8Array) || (obj && _.has(obj, "$Uint8ArrayPolyfill"))
)
},
toJSONValue: function (obj: any) {
return { $binary: EJSON._base64Encode(obj) }
},
fromJSONValue: function (obj: any) {
return EJSON._base64Decode(obj.$binary)
}
},
{
// Escaping one level
matchJSONValue: function (obj: any) {
return _.has(obj, "$escape") && _.size(obj) === 1
},
matchObject: function (obj: any) {
if (_.isEmpty(obj) || _.size(obj) > 2) {
return false
}
return _.some(builtinConverters, function (converter: any) {
return converter.matchJSONValue(obj)
})
},
toJSONValue: function (obj: any) {
var newObj = {}
_.each(obj, function (value: any, key: any) {
newObj[key] = EJSON.toJSONValue(value)
})
return { $escape: newObj }
},
fromJSONValue: function (obj: any) {
var newObj = {}
_.each(obj.$escape, function (value: any, key: any) {
newObj[key] = EJSON.fromJSONValue(value)
})
return newObj
}
},
{
// Custom
matchJSONValue: function (obj: any) {
return _.has(obj, "$type") && _.has(obj, "$value") && _.size(obj) === 2
},
matchObject: function (obj: any) {
return EJSON._isCustomType(obj)
},
toJSONValue: function (obj: any) {
return { $type: obj.typeName(), $value: obj.toJSONValue() }
},
fromJSONValue: function (obj: any) {
var typeName = obj.$type
var converter = customTypes[typeName]
return converter(obj.$value)
}
}
]
EJSON._isCustomType = function (obj: any) {
return (
obj &&
typeof obj.toJSONValue === "function" &&
typeof obj.typeName === "function" &&
_.has(customTypes, obj.typeName())
)
}
//for both arrays and objects, in-place modification.
var adjustTypesToJSONValue = (EJSON._adjustTypesToJSONValue = function (obj: any) {
if (obj === null) return null
var maybeChanged = toJSONValueHelper(obj)
if (maybeChanged !== undefined) return maybeChanged
_.each(obj, function (value: any, key: any) {
if (typeof value !== "object" && value !== undefined) return // continue
var changed = toJSONValueHelper(value)
if (changed) {
obj[key] = changed
return // on to the next key
}
// if we get here, value is an object but not adjustable
// at this level. recurse.
adjustTypesToJSONValue(value)
})
return obj
})
// Either return the JSON-compatible version of the argument, or undefined (if
// the item isn't itself replaceable, but maybe some fields in it are)
var toJSONValueHelper = function (item: any) {
for (var i = 0; i < builtinConverters.length; i++) {
var converter = builtinConverters[i]
if (converter.matchObject(item)) {
return converter.toJSONValue(item)
}
}
return undefined
}
EJSON.toJSONValue = function (item: any) {
var changed = toJSONValueHelper(item)
if (changed !== undefined) return changed
if (typeof item === "object") {
item = EJSON.clone(item)
adjustTypesToJSONValue(item)
}
return item
}
//for both arrays and objects. Tries its best to just
// use the object you hand it, but may return something
// different if the object you hand it itself needs changing.
var adjustTypesFromJSONValue = (EJSON._adjustTypesFromJSONValue = function (obj: any) {
if (obj === null) return null
var maybeChanged = fromJSONValueHelper(obj)
if (maybeChanged !== obj) return maybeChanged
_.each(obj, function (value: any, key: any) {
if (typeof value === "object") {
var changed = fromJSONValueHelper(value)
if (value !== changed) {
obj[key] = changed
return
}
// if we get here, value is an object but not adjustable
// at this level. recurse.
adjustTypesFromJSONValue(value)
}
})
return obj
})
// Either return the argument changed to have the non-json
// rep of itself (the Object version) or the argument itself.
// DOES NOT RECURSE. For actually getting the fully-changed value, use
// EJSON.fromJSONValue
var fromJSONValueHelper = function (value: any) {
if (typeof value === "object" && value !== null) {
if (
_.size(value) <= 2 &&
_.every(value, function (v: any, k: any) {
return typeof k === "string" && k.substr(0, 1) === "$"
})
) {
for (var i = 0; i < builtinConverters.length; i++) {
var converter = builtinConverters[i]
if (converter.matchJSONValue(value)) {
return converter.fromJSONValue(value)
}
}
}
}
return value
}
EJSON.fromJSONValue = function (item: any) {
var changed = fromJSONValueHelper(item)
if (changed === item && typeof item === "object") {
item = EJSON.clone(item)
adjustTypesFromJSONValue(item)
return item
} else {
return changed
}
}
EJSON.stringify = function (item: any) {
return JSON.stringify(EJSON.toJSONValue(item))
}
EJSON.parse = function (item: any) {
return EJSON.fromJSONValue(JSON.parse(item))
}
EJSON.isBinary = function (obj: any) {
return (typeof Uint8Array !== "undefined" && obj instanceof Uint8Array) || (obj && obj.$Uint8ArrayPolyfill)
}
EJSON.equals = function (a: any, b: any, options: any) {
var i: any
var keyOrderSensitive = !!(options && options.keyOrderSensitive)
if (a === b) return true
if (!a || !b)
// if either one is falsy, they'd have to be === to be equal
return false
if (!(typeof a === "object" && typeof b === "object")) return false
if (a instanceof Date && b instanceof Date) return a.valueOf() === b.valueOf()
if (EJSON.isBinary(a) && EJSON.isBinary(b)) {
if (a.length !== b.length) return false
for (i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false
}
return true
}
if (typeof a.equals === "function") return a.equals(b, options)
if (a instanceof Array) {
if (!(b instanceof Array)) return false
if (a.length !== b.length) return false
for (i = 0; i < a.length; i++) {
if (!EJSON.equals(a[i], b[i], options)) return false
}
return true
}
// fall back to structural equality of objects
var ret
if (keyOrderSensitive) {
var bKeys: any = []
_.each(b, function (val: any, x: any) {
bKeys.push(x)
})
i = 0
ret = _.every(a, function (val: any, x: any) {
if (i >= bKeys.length) {
return false
}
if (x !== bKeys[i]) {
return false
}
if (!EJSON.equals(val, b[bKeys[i]], options)) {
return false
}
i++
return true
})
return ret && i === bKeys.length
} else {
i = 0
ret = _.every(a, function (val: any, key: any) {
if (!_.has(b, key)) {
return false
}
if (!EJSON.equals(val, b[key], options)) {
return false
}
i++
return true
})
return ret && _.size(b) === i
}
}
EJSON.clone = function (v: any) {
var ret
if (typeof v !== "object") return v
if (v === null) return null // null has typeof "object"
if (v instanceof Date) return new Date(v.getTime())
if (EJSON.isBinary(v)) {
ret = EJSON.newBinary(v.length)
for (var i = 0; i < v.length; i++) {
ret[i] = v[i]
}
return ret
}
if (_.isArray(v) || _.isArguments(v)) {
// For some reason, _.map doesn't work in this context on Opera (weird test
// failures).
ret = []
for (i = 0; i < v.length; i++) ret[i] = EJSON.clone(v[i])
return ret
}
// handle general user-defined typed Objects if they have a clone method
if (typeof v.clone === "function") {
return v.clone()
}
// handle other objects
ret = {}
_.each(v, function (value: any, key: any) {
ret[key] = EJSON.clone(value)
})
return ret
}
module.exports = EJSON