-
Notifications
You must be signed in to change notification settings - Fork 1
/
toolkit-localStorage.coffee
192 lines (169 loc) · 5.58 KB
/
toolkit-localStorage.coffee
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
((definition) ->
# requirejs
if typeof define is "function" and define.amd
define ["toolkit"], (G) ->
G.localStorage = definition G
G
# CMD and CommandJS
else if exports?
G = require "./toolkit"
if module? and module.exports
module.exports = G
else
exports.G = G
# normal
else
G = @G
G.localStorage = definition G
) (G) ->
ls = @localStorage
ss = @sessionStorage
document = @document
useCookie = false
useSession = false
storageTime = "30d"
# [[[ utils
getInt = (str, hex=10) ->
return 0 if str is ""
parseInt str, hex
parseTime = (str) ->
timeCount = getInt "#{str}".slice 0, -1
timeUnit = str.substr -1
switch timeUnit
when "s" then timeCount * 1000
when "m" then timeCount * 60 * 1000
when "h" then timeCount * 60 * 60 * 1000
when "d" then timeCount * 24 * 60 * 60 * 1000
else
useSession = true
0
jsonSafeParse = (str, defaultValue={}) ->
return defaultValue if not str? or str is ""
try
JSON.parse str
catch error
defaultValue
intoObject = (obj, keys, callback) ->
keys = keys.slice()
resultObj = obj
while keys.length > 1
resultObj = (resultObj[keys.shift()] ?= {})
callback? resultObj, keys.shift()
doActionLoop = (actMethod, args) ->
realArgs = if G.isArray args[0] then args[0] else G.toArray args
return actMethod realArgs[0] if realArgs.length is 1
result = {}
for storageKey in realArgs
storageValue = actMethod storageKey
result[storageKey] = storageValue if storageValue?
result
# ]]]
# [[[ cookie
# s指秒,m指分钟,h指小时,d指天数
# 如30d代表30天,setCookie "name","hayden","20s"
setCookie = (key, value, time) ->
outTime = parseTime time
cookieStr = "#{key}=#{escape value}"
currTime = (exp = new Date).setTime exp.getTime() + outTime
cookieStr += ";expires=#{exp.toGMTString()}" unless useSession
cookieStr += ";path=/"
document.cookie = cookieStr
getCookie = (key) ->
re = ///(?:;\s*|^)#{key}=(.*?)(?:;|$)///g
if (result = re.exec document.cookie) then unescape result[1] else null
delCookie = (key) ->
cookieValue = getCookie key
setCookie key, cookieValue, "-1d"
# ]]]
# [[[ localStorage
getLocalStorage = (key) ->
storage = if useSession then ss else ls
storage.getItem key
setLocalStorage = (key, value) ->
storage = if useSession then ss else ls
storage.setItem key, value
delLocalStorage = (key) ->
storage = if useSession then ss else ls
storage.removeItem key
# ]]]
[setMethod, getMethod, delMethod] = if ls? then \
[setLocalStorage, getLocalStorage, delLocalStorage] else \
[setCookie, getCookie, delCookie]
# ls.set {k1: v1, k2: v2, k3: v3}
# ls.set k1, v1
set: ->
setMethod = setCookie if useCookie
if G.isPlainObject arguments[0]
for key, value of arguments[0]
value = JSON.stringify(value) if G.isObject value
setMethod key, value, storageTime
else
[key, value] = arguments
value = JSON.stringify(value) if G.isObject value
setMethod key, value, storageTime if key? and value?
this
# ls.get ['id1', 'id2', 'id3']
# ls.get 'id1', 'id2', 'id3'
get: ->
getMethod = getCookie if useCookie
doActionLoop getMethod, arguments
# same as @get()
del: ->
delMethod = delCookie if useCookie
doActionLoop delMethod, arguments
this
# {a: {b: {c: {d: 1}}}}
# getObject "a", "b", "c"
# => {d: 1}
getObject: (keys...) ->
result = originalObject = jsonSafeParse @get keys.shift()
return result unless keys.length
intoObject originalObject, keys, (object, key) ->
result = object[key]
result
# {a: {b: {c: {d: 1}}}}
# getObject "a", "b", "c"
# = {a: {b: {c: {}}}}
delObject: (keys...) ->
originalKey = keys.shift()
originalObject = jsonSafeParse @get originalKey
return @del originalKey unless keys.length
intoObject originalObject, keys, (object, key) =>
delete object[key]
@set originalKey, JSON.stringify originalObject
this
# {a: {b: {c: {d: 1}}}, b: 2}
# setObject "a", "b", "c", "d", 2
# = {a: {b: {c: {d: 2}}}, b: 2}
setObject: (keys..., value) ->
originalKey = keys.shift()
return @set originalKey, value unless keys.length
originalObject = @getObject originalKey
intoObject originalObject, keys, (object, key) =>
if G.isPlainObject object
object[key] = value
@set originalKey, JSON.stringify originalObject
else
console.warn object + "is not a object"
this
storageTime: (time) ->
if getInt(time) in [0, NaN]
[storageTime, useSession] = [0, true]
else
time = "#{time}s" if G.isNumber time
if time.slice(-1) in ["s", "m", "h", "d"]
[storageTime, useSession] = [time, false]
this
useCookie: (boolInput) ->
return useCookie unless boolInput?
useCookie = boolInput
this
# The options will only work in workers
# **The options not work in async worker.**
scope: (options, workers...) ->
old = {storageTime, useCookie}
@useCookie options.useCookie ? old.useCookie
@storageTime options.storageTime ? old.storageTime
worker(G.localStorage) for worker in workers
@useCookie(old.useCookie).storageTime old.storageTime
this