zustand-querystring 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/middleware.d.ts +1 -0
- package/lib/middleware.js +30 -21
- package/package.json +1 -1
package/lib/middleware.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ type DeepSelect<T> = T extends object ? {
|
|
|
5
5
|
export interface QueryStringOptions<T> {
|
|
6
6
|
url?: string;
|
|
7
7
|
select?: (pathname: string) => DeepSelect<T>;
|
|
8
|
+
key?: string;
|
|
8
9
|
}
|
|
9
10
|
type QueryString = <T, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []>(initializer: StateCreator<T, Mps, Mcs>, options?: QueryStringOptions<T>) => StateCreator<T, Mps, Mcs>;
|
|
10
11
|
export declare const querystring: QueryString;
|
package/lib/middleware.js
CHANGED
|
@@ -36,10 +36,29 @@ const translateSelectionToState = (selection, state) => Object.keys(selection).r
|
|
|
36
36
|
}
|
|
37
37
|
return acc;
|
|
38
38
|
}, {});
|
|
39
|
+
const escapeStringRegexp = string => {
|
|
40
|
+
if (typeof string !== 'string') {
|
|
41
|
+
throw new TypeError('Expected a string');
|
|
42
|
+
}
|
|
43
|
+
return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
|
|
44
|
+
};
|
|
39
45
|
const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
40
46
|
const defaultedOptions = {
|
|
47
|
+
key: '$',
|
|
41
48
|
...options,
|
|
42
49
|
};
|
|
50
|
+
const matcher = new RegExp(`${escapeStringRegexp(defaultedOptions.key)}=(.*);;`);
|
|
51
|
+
const parseQueryString = querystring => {
|
|
52
|
+
const match = querystring.match(matcher);
|
|
53
|
+
if (match) {
|
|
54
|
+
let m = match[1];
|
|
55
|
+
if (!m.startsWith('$')) {
|
|
56
|
+
m = '$' + m;
|
|
57
|
+
}
|
|
58
|
+
return parse(m);
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
};
|
|
43
62
|
const url = defaultedOptions.url;
|
|
44
63
|
const initialState = get() ?? fn(set, get, api);
|
|
45
64
|
const getSelectedState = (state, pathname) => {
|
|
@@ -60,13 +79,7 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
60
79
|
if (!queryString) {
|
|
61
80
|
return fallback();
|
|
62
81
|
}
|
|
63
|
-
|
|
64
|
-
return fallback();
|
|
65
|
-
}
|
|
66
|
-
if (!queryString.startsWith('$')) {
|
|
67
|
-
queryString = '$' + queryString;
|
|
68
|
-
}
|
|
69
|
-
const parsed = parse(queryString);
|
|
82
|
+
const parsed = parseQueryString(queryString);
|
|
70
83
|
const currentValue = get() ?? fn(_set, get, api);
|
|
71
84
|
const merged = mergeWith(currentValue, getSelectedState(parsed, pathname));
|
|
72
85
|
set(merged, true);
|
|
@@ -80,23 +93,19 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
80
93
|
if (typeof window !== 'undefined') {
|
|
81
94
|
const setQuery = () => {
|
|
82
95
|
const selectedState = getSelectedState(get(), window.location.pathname);
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
const currentParsed = parse(currentQueryString);
|
|
91
|
-
const newMerged = mergeWith(currentParsed, selectedState, (objValue, srcValue) => {
|
|
92
|
-
if (Array.isArray(objValue)) {
|
|
93
|
-
return srcValue;
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
+
const currentQueryString = window.location.search.slice(1);
|
|
97
|
+
const currentParsed = parseQueryString(currentQueryString);
|
|
98
|
+
const newMerged = {
|
|
99
|
+
...currentParsed,
|
|
100
|
+
...selectedState,
|
|
101
|
+
};
|
|
96
102
|
const newCompacted = compact(newMerged, initialState);
|
|
97
103
|
if (Object.keys(newCompacted).length) {
|
|
98
104
|
const stringified = stringify(newCompacted).substring(1);
|
|
99
|
-
|
|
105
|
+
const newQueryString = `${defaultedOptions.key}=${stringified};;`;
|
|
106
|
+
window.history.replaceState(null, '', `?${currentParsed
|
|
107
|
+
? currentQueryString.replace(matcher, newQueryString)
|
|
108
|
+
: newQueryString}`);
|
|
100
109
|
}
|
|
101
110
|
else {
|
|
102
111
|
window.history.replaceState(null, '', window.location.pathname);
|