-
Notifications
You must be signed in to change notification settings - Fork 19
/
test.html
285 lines (255 loc) · 10.9 KB
/
test.html
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>yux-storage测试用例</title>
<style>
body {
padding: 0 0.5em 100px;
background: #fff;
}
pre {
padding: 12px 1em;
margin: 1em 0;
background-color: #f5f7f9;
font-family: Consolas, monospace;
font-size: 100%;
white-space: pre-wrap;
word-wrap: break-word;
border-radius: 4px;
box-shadow: inset 1px 0 #e3e5e8, inset 0 1px #e3e5e8, inset -1px 0 #eaf0f5, inset 0 -1px #eaf0f5;
tab-size: 2;
}
blockquote {
font-family: 'kaiti', 'STKaiti';
border-left: solid silver;
background-color: #ffffe0;
padding: 0.5em 1em;
background-color: beige;
margin: 0;
}
button,input {
margin: 5px;
}
p {
font-size: 12px;
}
input[type="file" i]{
outline: 0;
}
input[type="file" i]::-webkit-file-upload-button{
display: none;
}
</style>
<link rel="stylesheet" href="https://qidian.gtimg.com/lulu/pure/css/common/ui/LightTip.css">
<link rel="stylesheet" href="https://qidian.gtimg.com/lulu/pure/css/common/ui/Button.css">
<script src="https://qidian.gtimg.com/lulu/pure/js/common/ui/LightTip.js"></script>
</head>
<body>
<h1>yux-storage</h1>
<blockquote>推荐打开控制台查看</blockquote>
<h2>getItem</h2>
<pre>yuxStorage.getItem('A').then(res => {
console.log(result)
});</pre>
<button class="ui-button" data-type="success" onclick="gets('A')">获取key:A</button>
<button class="ui-button" data-type="success" onclick="gets('B')">获取key:B</button>
<button class="ui-button" data-type="success" onclick="gets(['A','B'])">获取key:['A','B']</button>
<h2>setItem</h2>
<pre>yuxStorage.setItem(key, value)</pre>
<button class="ui-button" data-type="primary" onclick="sets('A', 1)">设置 A = 1</button>
<button class="ui-button" data-type="primary" onclick="sets('B', 2)">设置 B = 2</button>
<p>测试键为数组或者数字</p>
<button class="ui-button" data-type="primary" onclick="sets(['A','B'], 3)">设置 ['A','B'] = 3</button>
<button class="ui-button" data-type="primary" onclick="sets(1, 4)">设置 1 = 4</button>
<p>测试值为非字符串</p>
<button class="ui-button" data-type="primary" onclick="sets('C', [1,2])">设置 C = [1,2]</button>
<button class="ui-button" data-type="primary" onclick="sets('C', {name: 'xboxyan'})">设置 C = {name: 'xboxyan'}</button>
<button class="ui-button" data-type="primary" onclick="sets('C', Date())">设置 C = Date()</button>
<button class="ui-button" data-type="primary" onclick="sets('C', new Blob())">设置 C = new Blob()</button>
<button class="ui-button" data-type="primary" onclick="sets('C', new ArrayBuffer())">设置 C = new ArrayBuffer()</button>
<button class="ui-button" data-type="primary" onclick="sets('C', new RegExp())">设置 C = new RegExp()</button>
<button class="ui-button" data-type="primary" onclick="sets('C', new Float32Array())">设置 C = new Float32Array()</button>
<button class="ui-button" data-type="primary" onclick="sets('C', new Int16Array())">设置 C = new Int16Array()</button>
<button class="ui-button" data-type="primary" onclick="sets('C', new Uint16Array())">设置 C = new Uint16Array()</button>
<p>测试值为随意上传的文件,不限大小</p>
<label class="ui-button" data-type="primary" id="label" for="file">选择文件</label>
<input id="file" type="file" onchange="sets('C', event.target.files[0])" />
<p>获取上面的值</p>
<button class="ui-button" data-type="success" onclick="gets('C')">获取key:C</button>
<h2>removeItem</h2>
<pre>yuxStorage.removeItem(key)</pre>
<button class="ui-button" data-type="warning" onclick="removes('A')">删除 A</button>
<button class="ui-button" data-type="warning" onclick="removes('B')">删除 B</button>
<h2>key</h2>
<pre>yuxStorage.key(keyIndex)</pre>
<button class="ui-button" data-type="success" onclick="key(0)">第1个键</button>
<button class="ui-button" data-type="success" onclick="key(1)">第2个键</button>
<button class="ui-button" data-type="success" onclick="key(2)">第3个键</button>
<button class="ui-button" data-type="success" onclick="key(3)">第4个键</button>
<button class="ui-button" data-type="success" onclick="key(4)">第5个键</button>
<h2>keys</h2>
<pre>yuxStorage.keys().then(names => {
console.log(result)
};</pre>
<button class="ui-button" data-type="success" onclick="keys()">所有键</button>
<h2>clear</h2>
<pre>yuxStorage.clear()</pre>
<button class="ui-button" data-type="danger" onclick="clearStorage()">!清空全部数据</button>
<h2>错误捕获</h2>
<pre>// 回调函数
yuxStorage.getItem({},function(err,value){
if(err){
console.log(err)
}else{
console.log(value)
}
});
// promise
yuxStorage.getItem(key).then(value => {
console.log(value)
}).catch(err => console.log(err))</pre>
<p>参数不合法时会报错</p>
<button class="ui-button" data-type="success" onclick="gets({})">获取key:{}</button>
<button class="ui-button" data-type="primary" onclick="sets('M', function(){})">设置 M = function(){}</button>
<script type="module">
import yuxStorage from './yux-storage.js'
function equal(a, b) {
if (a === b) return true;
if (a && b && typeof a == 'object' && typeof b == 'object') {
if (a.constructor !== b.constructor) return false;
var length, i, keys;
if (Array.isArray(a)) {
length = a.length;
if (length != b.length) return false;
for (i = length; i-- !== 0;)
if (!equal(a[i], b[i])) return false;
return true;
}
if ((a instanceof Map) && (b instanceof Map)) {
if (a.size !== b.size) return false;
for (i of a.entries())
if (!b.has(i[0])) return false;
for (i of a.entries())
if (!equal(i[1], b.get(i[0]))) return false;
return true;
}
if ((a instanceof Set) && (b instanceof Set)) {
if (a.size !== b.size) return false;
for (i of a.entries())
if (!b.has(i[0])) return false;
return true;
}
if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
length = a.length;
if (length != b.length) return false;
for (i = length; i-- !== 0;)
if (a[i] !== b[i]) return false;
return true;
}
if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();
keys = Object.keys(a);
length = keys.length;
if (length !== Object.keys(b).length) return false;
for (i = length; i-- !== 0;)
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
for (i = length; i-- !== 0;) {
var key = keys[i];
if (!equal(a[key], b[key])) return false;
}
return true;
}
// true if both NaN, false otherwise
return a!==a && b!==b;
};
function gets(key) {
yuxStorage.getItem(key).then(res => {
console.log(key, '=>', res)
new LightTip().success(JSON.stringify(key)+': '+Object.prototype.toString.call(res)+ ' '+JSON.stringify(res));
}).catch(err => {
console.log(err)
new LightTip().error(JSON.stringify(err));
})
}
function sets(key, value) {
yuxStorage.setItem(key, value).then(res => {
console.log(key, '设置完成')
new LightTip().success('设置完成');
}).catch(err => {
console.log(err)
new LightTip().error(JSON.stringify(err));
})
}
function removes(key) {
yuxStorage.removeItem(key).then(res => {
console.log(key, '已被移除')
new LightTip().success(JSON.stringify(key)+'已被移除');
}).catch(err => {
console.log(err)
new LightTip().error(JSON.stringify(err));
})
}
function clearStorage() {
yuxStorage.clear().then(res => {
console.log('数据已清空')
new LightTip().success('数据已清空');
}).catch(err => {
console.log(err)
new LightTip().error(JSON.stringify(err));
})
}
function key(index) {
yuxStorage.key(index).then(res => {
console.log(index, '=>', res)
new LightTip().success(`第${index}个键: `+JSON.stringify(res));
})
}
function keys() {
yuxStorage.keys().then(res => {
console.log('全部键', res)
new LightTip().success('全部键: '+JSON.stringify(res));
}).catch(err => console.log(err))
}
</script>
<script>
const data1 = [
["key_string", "yux-storage"],
["key_number", 4869],
["key_bool", true],
["key_array", ["html", "css", "javascript"]],
[
"key_object",
{
name: "xboxyan",
email: "[email protected]"
}
],
["key_reg", /\d+/g],
["key_date", new Date(2021, 10, 1)],
["key_set", new Set([1, 2, 3])],
["key_file", new File(["First Line Text", "Second Line Text"], "book.txt")],
["key_blob", new Blob([1, 2, 3])],
["key_arraybuffer", new ArrayBuffer([1, 2, 3])],
["key_float32", new Float32Array([1, 2, 3])],
["key_float64", new Float64Array([1, 2, 3])],
["key_init8", new Int8Array([1, 2, 3])],
["key_init16", new Int16Array([1, 2, 3])],
["key_init32", new Int32Array([1, 2, 3])],
["key_uint8", new Uint8Array([1, 2, 3])],
["key_unit16", new Uint16Array([1, 2, 3])],
["key_unit32", new Uint32Array([1, 2, 3])]
];
function test1() {
data1.forEach(async el => {
await yuxStorage.setItem(el[0], el[1]);
const val = await yuxStorage.getItem(el[0]);
console.log(equal(el[1], val));
new LightTip("成功提示", "success");
});
}
</script>
</body>
</html>