zustand-querystring 0.0.13 → 0.0.15
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/README.md +1 -1
- package/lib/middleware.js +17 -18
- package/package.json +2 -2
package/README.md
CHANGED
package/lib/middleware.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parse, stringify } from './parser.js';
|
|
2
|
-
import { mergeWith, isEqual } from 'lodash-es';
|
|
2
|
+
import { mergeWith, isEqual, cloneDeep } from 'lodash-es';
|
|
3
3
|
const compact = (newState, initialState) => {
|
|
4
4
|
const output = {};
|
|
5
5
|
Object.keys(newState).forEach(key => {
|
|
@@ -51,12 +51,13 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
51
51
|
key: '$',
|
|
52
52
|
...options,
|
|
53
53
|
};
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
const
|
|
54
|
+
const escapedKey = escapeStringRegexp(defaultedOptions.key);
|
|
55
|
+
const stateMatcher = new RegExp(`${escapedKey}=(.*);;|${escapedKey}=(.*)$`);
|
|
56
|
+
const splitMatcher = new RegExp(`${escapedKey}=.*;;|${escapedKey}=.*$`);
|
|
57
|
+
const parseQueryString = (querystring) => {
|
|
57
58
|
const match = querystring.match(stateMatcher);
|
|
58
59
|
if (match) {
|
|
59
|
-
let m = match[1];
|
|
60
|
+
let m = match[1] ?? match[2];
|
|
60
61
|
if (!m.startsWith('$')) {
|
|
61
62
|
m = '$' + m;
|
|
62
63
|
}
|
|
@@ -65,7 +66,6 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
65
66
|
return null;
|
|
66
67
|
};
|
|
67
68
|
const url = defaultedOptions.url;
|
|
68
|
-
const initialState = get() ?? fn(set, get, api);
|
|
69
69
|
const getSelectedState = (state, pathname) => {
|
|
70
70
|
if (defaultedOptions.select) {
|
|
71
71
|
const selection = defaultedOptions.select(pathname);
|
|
@@ -75,29 +75,31 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
75
75
|
}
|
|
76
76
|
return state ?? {};
|
|
77
77
|
};
|
|
78
|
-
const initialize = (url,
|
|
79
|
-
const fallback = () => fn(_set, get, api);
|
|
78
|
+
const initialize = (url, initialState) => {
|
|
80
79
|
try {
|
|
81
80
|
const queryString = url.search.substring(1);
|
|
82
81
|
const pathname = url.pathname;
|
|
83
82
|
if (!queryString) {
|
|
84
|
-
return
|
|
83
|
+
return initialState;
|
|
85
84
|
}
|
|
86
85
|
const parsed = parseQueryString(queryString);
|
|
87
86
|
if (!parsed) {
|
|
88
|
-
return
|
|
87
|
+
return initialState;
|
|
89
88
|
}
|
|
90
|
-
const
|
|
91
|
-
const merged = mergeWith(currentValue, getSelectedState(parsed, pathname));
|
|
89
|
+
const merged = mergeWith(cloneDeep(initialState), getSelectedState(parsed, pathname));
|
|
92
90
|
set(merged, true);
|
|
93
91
|
return merged;
|
|
94
92
|
}
|
|
95
93
|
catch (error) {
|
|
96
94
|
console.error(error);
|
|
97
|
-
return
|
|
95
|
+
return initialState;
|
|
98
96
|
}
|
|
99
97
|
};
|
|
100
98
|
if (typeof window !== 'undefined') {
|
|
99
|
+
const initialState = cloneDeep(fn((...args) => {
|
|
100
|
+
set(...args);
|
|
101
|
+
setQuery();
|
|
102
|
+
}, get, api));
|
|
101
103
|
const setQuery = () => {
|
|
102
104
|
const selectedState = getSelectedState(get(), location.pathname);
|
|
103
105
|
const currentQueryString = location.search;
|
|
@@ -159,13 +161,10 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
159
161
|
originalSetState(...args);
|
|
160
162
|
setQuery();
|
|
161
163
|
};
|
|
162
|
-
return initialize(new URL(location.href),
|
|
163
|
-
set(...args);
|
|
164
|
-
setQuery();
|
|
165
|
-
});
|
|
164
|
+
return initialize(new URL(location.href), initialState);
|
|
166
165
|
}
|
|
167
166
|
else if (url) {
|
|
168
|
-
return initialize(new URL(decodeURIComponent(url), 'http://localhost'));
|
|
167
|
+
return initialize(new URL(decodeURIComponent(url), 'http://localhost'), fn(set, get, api));
|
|
169
168
|
}
|
|
170
169
|
return fn(set, get, api);
|
|
171
170
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zustand-querystring",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.15",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"@types/lodash-es": "^4.17.6",
|
|
36
36
|
"rimraf": "^3.0.2",
|
|
37
37
|
"typescript": "^4.9.3",
|
|
38
|
-
"zustand": "^4.3.
|
|
38
|
+
"zustand": "^4.3.8"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
41
|
"dev": "tsc -b --watch",
|