-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
105 lines (89 loc) · 2.2 KB
/
helpers.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
const cuid = require('cuid')
module.exports.id = function id (prf) {
let c = cuid.slug()
return prf + c.slice(0, -4) + c.slice(-2)
}
module.exports.debounceWithArgs = function debounceWithArgs (func, wait, arghasher) {
var cache = {}
function call (key, args) {
clearTimeout(cache[key])
func.apply(this, args)
}
var debounced = function () {
let key = arghasher(arguments)
clearTimeout(cache[key])
cache[key] = setTimeout(call.bind(this, key, arguments), wait)
}
return debounced
}
module.exports.toSimplified = function toSimplified (elmRecord) {
var o = {}
for (let i = 0; i < elmRecord.k.length; i++) {
let k = elmRecord.k[i]
if (k.length) {
let calcValue = elmRecord.c[i]
try {
o[k] = JSON.parse(calcValue)
} catch (e) {
o[k] = calcValue
}
}
}
return o
}
module.exports.toPouch = function toPouch (elmRecord) {
return {
_id: elmRecord.id,
kv: elmRecord.k.map((k, i) => [k, elmRecord.v[i]]),
f: elmRecord.f.map(({linked}) => ({l: linked})),
pos: elmRecord.pos,
width: elmRecord.width,
kind: elmRecord.kind
}
// still needs the _rev
}
module.exports.fromPouch = function fromPouch (doc) {
return {
id: doc._id,
pos: doc.pos,
width: doc.width || 180,
kind: doc.kind === undefined ? null : doc.kind,
k: doc.kv.map(kv => kv[0]),
v: doc.kv.map(kv => kv[1]),
c: doc.kv.map(kv => kv[1]),
e: doc.kv.map(() => false),
f: doc.f
? doc.f
.map(({l}) => ({linked: l}))
: doc.kv.map(() => ({linked: false})),
focused: false
}
}
module.exports.setAt = function setAt (object, path, value) {
var o = object
while (path.length > 1) {
let k = path.shift()
let nextO = o[k]
if (!nextO) {
nextO = {}
o[k] = nextO
}
o = nextO
}
o[path[0]] = value
}
module.exports.getAt = function getAt (object, path) {
var o = object
while (path.length > 1) {
o = o[path.shift()]
if (!o) {
o = {}
}
}
return o[path[0]]
}
module.exports.hash = function hash (_id, idx) { return `${_id}¬${idx}` }
module.exports.unhash = function unhash (h) {
let [_id, idxstr] = h.split('¬')
return [_id, parseInt(idxstr)]
}