-
Notifications
You must be signed in to change notification settings - Fork 66
/
util.js
36 lines (31 loc) · 950 Bytes
/
util.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
exports.createId =
function () {
return [1,1,1].map(function () {
return Math.random().toString(16).substring(2).toUpperCase()
}).join('')
}
exports.filter = function (update, sources) {
var ts = update[1]
var source = update[2]
return (!sources || !sources[source] || sources[source] < ts)
}
exports.protoIsIllegal = function (s) {
s.emit('invalid', new Error('"__proto__" is illegal property name'))
return null
}
function invalidUpdate(t) {
t.emit('invalid', new Error('invalid update'))
}
exports.validUpdate = function (t, update) {
if(!Array.isArray(update)) return invalidUpdate(t)
if('string' !== typeof update[1] || 'number' !== typeof update[2])
return invalidUpdate(t)
}
exports.sort = function (hist) {
return hist.sort(function (a, b) {
//sort by timestamps, then ids.
//there should never be a pair with equal timestamps
//and ids.
return a[1] - b[1] || (a[2] > b[2] ? 1 : -1)
})
}