-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchParam.js
More file actions
63 lines (52 loc) · 1.63 KB
/
SearchParam.js
File metadata and controls
63 lines (52 loc) · 1.63 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
const valueSymbol = Symbol('param:value');
const nameSymbol = Symbol('param:name');
/**
* Class representing a URL search parameter accessor.
* Extends `EventTarget` to support listening for updates on the parameter.
*/
export class SearchParam extends EventTarget {
#name;
#multiple = false;
#fallbackValue = '';
/**
* Creates a search parameter accessor.
* @param {string} name - The name of the URL search parameter to manage.
* @param {string|number|Array} fallbackValue - The default value if the search parameter is not set. An array if `multiple` is true.
*/
constructor(name, fallbackValue, { multiple = false } = {}) {
super();
this.#name = name;
this.#fallbackValue = multiple && ! Array.isArray(fallbackValue) ? Object.freeze([fallbackValue]) : Object.freeze(fallbackValue);
this.#multiple = multiple === true;
}
toString() {
return this[SearchParam.valueSymbol];
}
[Symbol.iterator]() {
return this.#multiple ? this[valueSymbol] : [this[valueSymbol]];
}
get [Symbol.toStringTag]() {
return 'SearchParam';
}
[Symbol.toPrimitive](hint = 'default') {
return hint === 'number' ? parseFloat(this[valueSymbol]) : this[valueSymbol];
}
get [valueSymbol]() {
const params = new URLSearchParams(globalThis?.location.search);
if (this.#multiple) {
const values = Object.freeze(params.getAll(this.#name));
return values.length === 0 ? this.#fallbackValue : values;
} else {
return params.get(this.#name) ?? this.#fallbackValue?.toString() ?? '';
}
}
get [nameSymbol]() {
return this.#name;
}
static get nameSymbol() {
return nameSymbol;
}
static get valueSymbol() {
return valueSymbol;
}
}