url-search-params

说明

模拟实现浏览器的URLSearchParamsAPI

源码

用法

urlSearchParams.test.ts
1
import { URLSearchParams } from "../../utility/algorithm/url-search-params";
2
3
// ? URLSearchParams
4
describe('URLSearchParams tests', () => {
5
test('URLSearchParams.handleAppend should add the new value to the tail of the `params` and update the url', () => {
6
// !!! https://ddzy.github.io/login?name=ddzy&age=21&isSelf=true
7
const received = [
8
{
9
key: 'skill',
10
value: 'programmer',
11
},
12
];
13
const expected = [
14
{
15
url: 'https://ddzy.github.io/login?name=ddzy&age=21&isSelf=true&skill=programmer',
16
params: {
17
name: 'ddzy',
18
age: '21',
19
isSelf: 'true',
20
skill: 'programmer',
21
},
22
},
23
];
24
const usp = new URLSearchParams({});
25
26
for (const [i, v] of received.entries()) {
27
const __this__ = usp.handleAppend(v.key, v.value);
28
const { url, params } = usp.state;
29
30
// ? check this
31
expect(__this__ instanceof URLSearchParams).toBeTruthy();
32
// ? check url
33
expect(url).toBe(expected[i].url);
34
// ? check params
35
expect(params).toEqual(expected[i].params);
36
}
37
});
38
39
test('URLSearchParams.handleDelete should remove the special `key` from url and params', () => {
40
const received = [
41
'age',
42
];
43
const expected = [
44
{
45
url: 'https://ddzy.github.io/login?name=ddzy&isSelf=true',
46
params: {
47
name: 'ddzy',
48
isSelf: 'true',
49
},
50
},
51
];
52
const usp = new URLSearchParams({});
53
54
for (const [i, v] of received.entries()) {
55
const __this__ = usp.handleDelete(v);
56
const { url, params } = usp.state;
57
58
// ? check this
59
expect(__this__ instanceof URLSearchParams).toBeTruthy();
60
// ? check url
61
expect(url).toBe(expected[i].url);
62
// ? check params
63
expect(params).toEqual(expected[i].params);
64
}
65
});
66
67
test('URLSearchParams.handleGet should received the `key` and return the value of this key', () => {
68
const received = [
69
'',
70
'skill',
71
'name',
72
'age',
73
];
74
const expected = [
75
null,
76
null,
77
'ddzy',
78
'21',
79
];
80
const usp = new URLSearchParams({});
81
82
for (const [i, v] of received.entries()) {
83
const result = usp.handleGet(v);
84
85
expect(result).toBe(expected[i]);
86
}
87
});
88
89
test('URLSearchParams.handleGetAll should return all of the params liked `[["name", "ddzy"], ...]`', () => {
90
const expected = [
91
['name', 'ddzy'],
92
['age', '21'],
93
['isSelf', 'true'],
94
];
95
const usp = new URLSearchParams({});
96
const result = usp.handleGetAll();
97
98
for (const [i, v] of result.entries()) {
99
expect(v[0]).toBe(expected[i][0]);
100
expect(v[1]).toBe(expected[i][1]);
101
}
102
});
103
104
test('URLSearchParams.handleHas should return `true` if the params has its own property that received', () => {
105
const received = [
106
'',
107
'skill',
108
'name',
109
'age',
110
];
111
const expected = [
112
false,
113
false,
114
true,
115
true,
116
];
117
const usp = new URLSearchParams({});
118
119
for (const [i, v] of received.entries()) {
120
const result = usp.handleHas(v);
121
122
expect(result).toBe(expected[i]);
123
}
124
});
125
126
test('URLSearchParams.handleSet should set all of the new value to the key which was received', () => {
127
const received = [
128
{
129
key: 'name',
130
value: 'duanzhaoyang',
131
},
132
{
133
key: 'age',
134
value: 50,
135
},
136
];
137
const expected = [
138
{
139
url: 'https://ddzy.github.io/login?name=duanzhaoyang&age=21&isSelf=true',
140
params: {
141
name: 'duanzhaoyang',
142
age: '21',
143
isSelf: 'true',
144
},
145
},
146
{
147
url: 'https://ddzy.github.io/login?name=duanzhaoyang&age=50&isSelf=true',
148
params: {
149
name: 'duanzhaoyang',
150
age: '50',
151
isSelf: 'true',
152
},
153
},
154
];
155
const usp = new URLSearchParams({});
156
157
for (const [i, v] of received.entries()) {
158
const __this__ = usp.handleSet(v.key, v.value);
159
const { url, params } = usp.state;
160
161
// ? check this
162
expect(__this__ instanceof URLSearchParams).toBeTruthy();
163
// ? check url
164
expect(url).toBe(expected[i].url);
165
// ? check params
166
expect(params).toEqual(expected[i].params);
167
}
168
});
169
170
test('URLSearchParams.handleKeys should return the `keys-collection` of the params', () => {
171
const expected = [
172
'name',
173
'age',
174
'isSelf',
175
];
176
const usp = new URLSearchParams({});
177
const result = usp.handleKeys();
178
179
for (const [i, v] of expected.entries()) {
180
expect(v).toBe(result[i]);
181
}
182
});
183
184
test('URLSearchParams.handleValues should return the `values-collection` of the params', () => {
185
const expected = [
186
'ddzy',
187
'21',
188
'true',
189
];
190
const usp = new URLSearchParams({});
191
const result = usp.handleValues();
192
193
for (const [i, v] of expected.entries()) {
194
expect(v).toBe(result[i]);
195
}
196
});
197
198
test('URLSearchParams.iterator should being called by the `iterator`', () => {
199
const expected = [
200
['name', 'ddzy'],
201
['age', '21'],
202
['isSelf', 'true'],
203
];
204
const usp = new URLSearchParams({});
205
206
let count = 0;
207
for (const [key, value] of (usp.state.params as any)) {
208
expect(key).toBe(expected[count][0]);
209
expect(value).toBe(expected[count][1]);
210
count++;
211
}
212
});
213
});
Copied!

API

Name
Type
Description
handleAppend
function
追加新的键值对
handleDelete
function
移除某个键值对
handleGet
function
获取指定的参数键值
handleGetAll
function
获取所有的参数值
handleHas
function
判断是否存在指定键名
handleSet
function
更新源 url 中的对应键值(但是 params 中的值是不变的)
handleKeys
function
获取键名数组
handleValues
function
获取键值数组
最近更新 1yr ago
复制链接