forked from marcuswestin/store.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1-backcompat.js
52 lines (49 loc) · 1.27 KB
/
v1-backcompat.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
var dumpPlugin = require('./dump')
var json2Plugin = require('./json2')
module.exports = [dumpPlugin, json2Plugin, v1BackcompatPlugin]
function v1BackcompatPlugin() {
this.disabled = !this.enabled
return {
has: backcompat_has,
transact: backcompat_transact,
clear: backcompat_clear,
forEach: backcompat_forEach,
getAll: backcompat_getAll,
serialize: backcompat_serialize,
deserialize: backcompat_deserialize,
}
}
function backcompat_has(_, key) {
return this.get(key) !== undefined
}
function backcompat_transact(_, key, defaultVal, transactionFn) {
if (transactionFn == null) {
transactionFn = defaultVal
defaultVal = null
}
if (defaultVal == null) {
defaultVal = {}
}
var val = this.get(key, defaultVal)
var ret = transactionFn(val)
this.set(key, ret === undefined ? val : ret)
}
function backcompat_clear(_) {
return this.clearAll.call(this)
}
function backcompat_forEach(_, fn) {
return this.each.call(this, function(val, key) {
fn(key, val)
})
}
function backcompat_getAll(_) {
return this.dump.call(this)
}
function backcompat_serialize(_, value) {
return JSON.stringify(value)
}
function backcompat_deserialize(_, value) {
if (typeof value != 'string') { return undefined }
try { return JSON.parse(value) }
catch(e) { return value || undefined }
}