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
178 lines (149 loc) · 4 KB
/
web-storage.js
File metadata and controls
178 lines (149 loc) · 4 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
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 < 10) {
// 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 generateFunctions (fn) {
return {
local: fn('local'),
session: fn('session')
}
}
let
hasStorageItem = generateFunctions(
(type) => (key) => window[type + 'Storage'].getItem(key) !== null
),
getStorageLength = generateFunctions(
(type) => () => window[type + 'Storage'].length
),
getStorageItem = generateFunctions((type) => {
let
hasFn = hasStorageItem[type],
storage = window[type + 'Storage']
return (key) => {
if (hasFn(key)) {
return decode(storage.getItem(key))
}
return null
}
}),
getStorageAtIndex = generateFunctions((type) => {
let
lengthFn = getStorageLength[type],
getItemFn = getStorageItem[type],
storage = window[type + 'Storage']
return (index) => {
if (index < lengthFn()) {
return getItemFn(storage.key(index))
}
}
}),
getAllStorageItems = generateFunctions((type) => {
let
lengthFn = getStorageLength[type],
storage = window[type + 'Storage'],
getItemFn = getStorageItem[type]
return () => {
let
result = {},
key,
length = lengthFn()
for (let i = 0; i < length; i++) {
key = storage.key(i)
result[key] = getItemFn(key)
}
return result
}
}),
setStorageItem = generateFunctions((type) => {
let storage = window[type + 'Storage']
return (key, value) => { storage.setItem(key, encode(value)) }
}),
removeStorageItem = generateFunctions((type) => {
let storage = window[type + 'Storage']
return (key) => { storage.removeItem(key) }
}),
clearStorage = generateFunctions((type) => {
let storage = window[type + 'Storage']
return () => { storage.clear() }
}),
storageIsEmpty = generateFunctions((type) => {
let getLengthFn = getStorageLength[type]
return () => getLengthFn() === 0
})
export var LocalStorage = {
has: hasStorageItem.local,
get: {
length: getStorageLength.local,
item: getStorageItem.local,
index: getStorageAtIndex.local,
all: getAllStorageItems.local
},
set: setStorageItem.local,
remove: removeStorageItem.local,
clear: clearStorage.local,
isEmpty: storageIsEmpty.local
}
export var SessionStorage = { // eslint-disable-line one-var
has: hasStorageItem.session,
get: {
length: getStorageLength.session,
item: getStorageItem.session,
index: getStorageAtIndex.session,
all: getAllStorageItems.session
},
set: setStorageItem.session,
remove: removeStorageItem.session,
clear: clearStorage.session,
isEmpty: storageIsEmpty.session
}