-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
181 lines (158 loc) · 3.72 KB
/
index.ts
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
/**
* Update url
*
* @param {URL|String} url
* @param {Boolean} saveState if true - session
* will be saved in history as new object,
* else replaced last record
*
* @returns {void}
*/
function changeUrl(url: string | URL, saveState = false) {
const _url = url.toString()
try {
if (window.history.replaceState && !saveState) {
window.history.replaceState(null, document.title, _url);
}
else if (window.history.pushState && saveState) {
window.history.pushState(null, document.title, _url)
}
else {
window.location.href = _url
}
} catch(e) {}
}
export interface IURLParams {
get url(): string
/**
* Set parameter
*
* @param {String} name
* @param {String|Number} value
* @param {Boolean} saveState
*
* @returns {URLParams}
*/
set(
name: string,
value: string | number,
saveState?: boolean
): this
/**
* Appends a specified key/value pair as a new search parameter.
*
* @param {String} name
* @param {any} value
* @returns {URLParams}
*/
append(
name: string,
value: string | number,
saveState?: boolean
): this
/**
* Returns the first value associated with the given search parameter
*
* @param {String} name
*
* @returns {String}
*/
get(name: string): string | null
/**
* Returns all the values associated with a given search parameter
*
* @param {String} name
*
* @returns {Array<String>} An array of values
*/
getAll(name: string): string[]
/**
* Returns all values and parameters in array
*
* @returns {Array<Array<String, String>>} an array of arrays like Object.entries
* with [key, value] structure
*/
getAllParams(): Array<[string, string]>
/**
* Deletes the given search parameter
* and all its associated values,
* from the list of all search parameters.
*
* @param {String} name - The name of the parameter to be deleted.
* @param {Boolean} saveState
*
* @returns {this}
*/
delete(name: string, saveState?: boolean): this
/**
* Returns a USVString containing the whole URL
*
* @returns {String} URL string representation
*/
toString(): string
}
/**
* Managing URL parameters
*/
export class URLParams implements IURLParams {
private _url: URL
constructor(url?: string | URL) {
this._url = new (URL || window.URL)(url || window.location.href)
}
get url() {
return this.toString()
}
set(
name: string,
value: string | number,
saveState = false
) {
this._url.searchParams.set(name, value.toString())
changeUrl(this._url, saveState)
return this
}
append(
name: string,
value: string | number,
saveState = false
) {
this._url.searchParams.append(name, value.toString())
changeUrl(this._url, saveState)
return this
}
get(name: string) {
return this._url.searchParams.get(name)
}
getAll(name: string) {
return this._url.searchParams.getAll(name)
}
getAllParams() {
return Array.from(this._url.searchParams.entries())
}
delete(name: string, saveState = false) {
this._url.searchParams.delete(name)
changeUrl(this._url, saveState)
return this
}
toString() {
return this._url.toString()
}
}
export interface urlParamsType extends IURLParams {
(url?: string | URL): IURLParams
}
// TODO: remove ts-ignore and fix error
// @ts-ignore
export const urlParams: urlParamsType = new Proxy(
(url?: string | URL) => new URLParams(url),
{
get(target, prop) {
const key = prop as keyof IURLParams
if (key in URLParams.prototype) {
const instance = new URLParams()
const value = instance[key]
return typeof value === 'function' ? value.bind(instance) : value
}
}
}
)