-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
page-param.ts
135 lines (101 loc) · 3.92 KB
/
page-param.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
// Manage observable URL-param from document.location.search
import { IObservableHistory } from "mobx-observable-history";
export interface PageParamInit<V = any> {
name: string;
defaultValue?: V;
defaultValueStringified?: string | string[]; // serialized version of "defaultValue"
multiValues?: boolean; // false == by default
multiValueSep?: string; // joining multiple values with separator, default: ","
skipEmpty?: boolean; // skip empty value(s), e.g. "?param=", default: true
parse?(value: string[]): V; // deserialize from URL
stringify?(value: V): string | string[]; // serialize params to URL
}
export interface PageSystemParamInit<V = any> extends PageParamInit<V> {
isSystem?: boolean;
}
export class PageParam<V = any> {
static SYSTEM_PREFIX = "lens-";
readonly name: string;
readonly urlName: string;
constructor(readonly init: PageParamInit<V> | PageSystemParamInit<V>, protected history: IObservableHistory) {
const { isSystem, name } = init as PageSystemParamInit;
this.name = name;
this.init.skipEmpty ??= true;
this.init.multiValueSep ??= ",";
// prefixing to avoid collisions with extensions
this.urlName = `${isSystem ? PageParam.SYSTEM_PREFIX : ""}${name}`;
}
isEmpty(value: V | any) {
return [value].flat().every(value => value == "" || value == null);
}
parse(values: string[]): V {
const { parse, multiValues } = this.init;
if (!multiValues) values.splice(1); // reduce values to single item
const parsedValues = [parse ? parse(values) : values].flat();
return multiValues ? parsedValues : parsedValues[0] as any;
}
stringify(value: V = this.get()): string {
const { stringify, multiValues, multiValueSep, skipEmpty } = this.init;
if (skipEmpty && this.isEmpty(value)) {
return "";
}
if (multiValues) {
const values = [value].flat();
const stringValues = [stringify ? stringify(value) : values.map(String)].flat();
return stringValues.join(multiValueSep);
}
return [stringify ? stringify(value) : String(value)].flat()[0];
}
get(): V {
const value = this.parse(this.getRaw());
if (this.init.skipEmpty && this.isEmpty(value)) {
return this.getDefaultValue();
}
return value;
}
set(value: V, { mergeGlobals = true, replaceHistory = false } = {}) {
const search = this.toSearchString({ mergeGlobals, value });
this.history.merge({ search }, replaceHistory);
}
setRaw(value: string | string[]) {
const { history, urlName } = this;
const { multiValues, multiValueSep, skipEmpty } = this.init;
const paramValue = multiValues ? [value].flat().join(multiValueSep) : String(value);
if (skipEmpty && this.isEmpty(paramValue)) {
history.searchParams.delete(urlName);
} else {
history.searchParams.set(urlName, paramValue);
}
}
getRaw(): string[] {
const { history, urlName } = this;
const { multiValueSep } = this.init;
return history.searchParams.getAsArray(urlName, multiValueSep);
}
getDefaultValue() {
const { defaultValue, defaultValueStringified } = this.init;
return defaultValueStringified ? this.parse([defaultValueStringified].flat()) : defaultValue;
}
clear() {
this.history.searchParams.delete(this.urlName);
}
toSearchString({ withPrefix = true, mergeGlobals = true, value = this.get() } = {}): string {
const { history, urlName, init: { skipEmpty } } = this;
const searchParams = new URLSearchParams(mergeGlobals ? history.location.search : "");
searchParams.set(urlName, this.stringify(value));
if (skipEmpty) {
searchParams.forEach((value: any, paramName) => {
if (this.isEmpty(value)) searchParams.delete(paramName);
});
}
if (Array.from(searchParams).length > 0) {
return `${withPrefix ? "?" : ""}${searchParams}`;
}
return "";
}
toObjectParam(value = this.get()): Record<string, V> {
return {
[this.urlName]: value,
};
}
}