forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb-storage.js
More file actions
158 lines (136 loc) · 3.28 KB
/
web-storage.js
File metadata and controls
158 lines (136 loc) · 3.28 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
import extend from '../utils/extend'
function encode (value) {
if (Object.prototype.toString.call(value) === '[object Date]') {
return '__q_date|' + value.toUTCString()
}
if (Object.prototype.toString.call(value) === '[object RegExp]') {
return '__q_expr|' + value.source
}
if (typeof value === 'number') {
return '__q_numb|' + value
}
if (typeof value === 'boolean') {
return '__q_bool|' + (value ? '1' : '0')
}
if (typeof value === 'string') {
return '__q_strn|' + value
}
if (typeof value === 'function') {
return '__q_strn|' + value.toString()
}
if (value === Object(value)) {
return '__q_objt|' + JSON.stringify(value)
}
// hmm, we don't know what to do with it,
// so just return it as is
return value
}
function decode (value) {
let type, length, source
length = value.length
if (length < 9) {
// then it wasn't encoded by us
return value
}
type = value.substr(0, 8)
source = value.substring(9)
switch (type) {
case '__q_date':
return new Date(source)
case '__q_expr':
return new RegExp(source)
case '__q_numb':
return Number(source)
case '__q_bool':
return Boolean(source === '1')
case '__q_strn':
return '' + source
case '__q_objt':
return JSON.parse(source)
default:
// hmm, we reached here, we don't know the type,
// then it means it wasn't encoded by us, so just
// return whatever value it is
return value
}
}
function getEmptyStorage () {
const fn = () => null
return {
has: fn,
get: {
length: fn,
item: fn,
index: fn,
all: fn
},
set: fn,
remove: fn,
clear: fn,
isEmpty: fn
}
}
function getStorage (type) {
const
webStorage = window[type + 'Storage'],
get = key => {
const item = webStorage.getItem(key)
return item
? decode(item)
: null
}
return {
has: key => webStorage.getItem(key) !== null,
get: {
length: () => webStorage.length,
item: get,
index: index => {
if (index < webStorage.length) {
return get(webStorage.key(index))
}
},
all: () => {
let result = {}, key, len = webStorage.length
for (let i = 0; i < len; i++) {
key = webStorage.key(i)
result[key] = get(key)
}
return result
}
},
set: (key, value) => { webStorage.setItem(key, encode(value)) },
remove: key => { webStorage.removeItem(key) },
clear: () => { webStorage.clear() },
isEmpty: () => webStorage.length === 0
}
}
export const LocalStorage = {
__installed: false,
install ({ $q }) {
if (this.__installed) { return }
this.__installed = true
if ($q.platform.has.webStorage) {
const storage = getStorage('local')
$q.localStorage = storage
extend(true, this, storage)
}
else {
$q.localStorage = getEmptyStorage()
}
}
}
export const SessionStorage = {
__installed: false,
install ({ $q }) {
if (this.__installed) { return }
this.__installed = true
if ($q.platform.has.webStorage) {
const storage = getStorage('session')
$q.sessionStorage = storage
extend(true, this, storage)
}
else {
$q.sessionStorage = getEmptyStorage()
}
}
}