zustand-querystring 0.0.17 → 0.0.19
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 +10 -7
- package/dist/{chunk-STY4LBQG.mjs → chunk-PYA77ZUK.mjs} +1 -1
- package/dist/{chunk-4CDMBGCR.mjs → chunk-S5N6DUVJ.mjs} +8 -8
- package/dist/{chunk-77OZJNG4.mjs → chunk-ZM24AXRE.mjs} +8 -1
- package/dist/index.js +15 -8
- package/dist/index.mjs +3 -3
- package/dist/middleware.js +15 -8
- package/dist/middleware.mjs +2 -2
- package/dist/parser.js +8 -1
- package/dist/parser.mjs +1 -1
- package/dist/utils.js +8 -1
- package/dist/utils.mjs +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,14 +5,16 @@ A Zustand middleware that syncs the store with the querystring.
|
|
|
5
5
|
Try on [StackBlitz](https://stackblitz.com/github/nitedani/zustand-querystring/tree/main/examples/react) (You need to click "Open in New Tab")
|
|
6
6
|
|
|
7
7
|
Examples:
|
|
8
|
+
|
|
8
9
|
- [React](./examples/react/)
|
|
9
10
|
- [NextJS](./examples/next/)
|
|
10
11
|
- [Rakkas](./examples/rakkas/)
|
|
11
12
|
|
|
12
13
|
Quickstart:
|
|
14
|
+
|
|
13
15
|
```ts
|
|
14
|
-
import create from
|
|
15
|
-
import { querystring } from
|
|
16
|
+
import create from 'zustand';
|
|
17
|
+
import { querystring } from 'zustand-querystring';
|
|
16
18
|
|
|
17
19
|
interface Store {
|
|
18
20
|
count: number;
|
|
@@ -30,7 +32,7 @@ export const useStore = create<Store>()(
|
|
|
30
32
|
ticks: 0,
|
|
31
33
|
someNestedState: {
|
|
32
34
|
nestedCount: 0,
|
|
33
|
-
hello:
|
|
35
|
+
hello: 'Hello',
|
|
34
36
|
},
|
|
35
37
|
}),
|
|
36
38
|
{
|
|
@@ -43,19 +45,20 @@ export const useStore = create<Store>()(
|
|
|
43
45
|
|
|
44
46
|
someNestedState: {
|
|
45
47
|
nestedCount: true,
|
|
46
|
-
hello:
|
|
48
|
+
hello: '/about' === pathname,
|
|
47
49
|
},
|
|
48
50
|
|
|
49
51
|
// OR select the whole nested state
|
|
50
52
|
// someNestedState: true
|
|
51
53
|
};
|
|
52
54
|
},
|
|
53
|
-
}
|
|
54
|
-
)
|
|
55
|
+
},
|
|
56
|
+
),
|
|
55
57
|
);
|
|
56
58
|
```
|
|
57
59
|
|
|
58
60
|
querystring options:
|
|
61
|
+
|
|
59
62
|
- <b>select</b> - the select option controls what part of the state is synced with the query string
|
|
60
63
|
- <b>key: string</b> - the key option controls how the state is stored in the querystring (default: $)
|
|
61
|
-
- <b>url</b> - the url option is used to provide the request url on the server side render
|
|
64
|
+
- <b>url</b> - the url option is used to provide the request url on the server side render
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
parse,
|
|
3
3
|
stringify
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-ZM24AXRE.mjs";
|
|
5
5
|
|
|
6
6
|
// src/middleware.ts
|
|
7
7
|
import { mergeWith, isEqual, cloneDeep } from "lodash-es";
|
|
@@ -127,10 +127,10 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
127
127
|
ignored += (ignored ? "&" : "?") + str;
|
|
128
128
|
}
|
|
129
129
|
const newCompacted = compact(newMerged, initialState);
|
|
130
|
+
let newQueryString = "";
|
|
130
131
|
if (Object.keys(newCompacted).length) {
|
|
131
132
|
const stringified = stringify(newCompacted);
|
|
132
133
|
const newQueryState = `${defaultedOptions.key}=${stringified};;`;
|
|
133
|
-
let newQueryString = "";
|
|
134
134
|
if (currentParsed) {
|
|
135
135
|
newQueryString = currentQueryString.replace(
|
|
136
136
|
splitMatcher,
|
|
@@ -141,13 +141,13 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
141
141
|
} else {
|
|
142
142
|
newQueryString = "?" + newQueryState;
|
|
143
143
|
}
|
|
144
|
-
history.replaceState(
|
|
145
|
-
history.state,
|
|
146
|
-
"",
|
|
147
|
-
location.pathname + newQueryString
|
|
148
|
-
);
|
|
149
144
|
} else {
|
|
150
|
-
|
|
145
|
+
newQueryString = ignored;
|
|
146
|
+
}
|
|
147
|
+
const currentUrl = location.pathname + location.search;
|
|
148
|
+
const newUrl = location.pathname + newQueryString;
|
|
149
|
+
if (newUrl !== currentUrl) {
|
|
150
|
+
history.replaceState(history.state, "", newUrl);
|
|
151
151
|
}
|
|
152
152
|
};
|
|
153
153
|
if (!api.__ZUSTAND_QUERYSTRING_INIT__) {
|
|
@@ -26,6 +26,9 @@ function stringify(input, recursive) {
|
|
|
26
26
|
}
|
|
27
27
|
return "@" + res.join("&") + ";";
|
|
28
28
|
}
|
|
29
|
+
if (input instanceof Date) {
|
|
30
|
+
return "=!Date:" + encodeString(input.toISOString(), valueStringifyRegexp);
|
|
31
|
+
}
|
|
29
32
|
if (typeof input === "object") {
|
|
30
33
|
for (const [key, value] of Object.entries(input)) {
|
|
31
34
|
const stringifiedValue = stringify(value, true);
|
|
@@ -65,7 +68,11 @@ function parse(str) {
|
|
|
65
68
|
function parseToken() {
|
|
66
69
|
const type = str.charAt(pos++);
|
|
67
70
|
if (type === "=") {
|
|
68
|
-
|
|
71
|
+
const value = readToken(valueParseRegexp);
|
|
72
|
+
if (value.startsWith("!Date:")) {
|
|
73
|
+
return new Date(value.slice("!Date:".length));
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
69
76
|
}
|
|
70
77
|
if (type === ":") {
|
|
71
78
|
const value = readToken(valueParseRegexp);
|
package/dist/index.js
CHANGED
|
@@ -54,6 +54,9 @@ function stringify(input, recursive) {
|
|
|
54
54
|
}
|
|
55
55
|
return "@" + res.join("&") + ";";
|
|
56
56
|
}
|
|
57
|
+
if (input instanceof Date) {
|
|
58
|
+
return "=!Date:" + encodeString(input.toISOString(), valueStringifyRegexp);
|
|
59
|
+
}
|
|
57
60
|
if (typeof input === "object") {
|
|
58
61
|
for (const [key, value] of Object.entries(input)) {
|
|
59
62
|
const stringifiedValue = stringify(value, true);
|
|
@@ -93,7 +96,11 @@ function parse(str) {
|
|
|
93
96
|
function parseToken() {
|
|
94
97
|
const type = str.charAt(pos++);
|
|
95
98
|
if (type === "=") {
|
|
96
|
-
|
|
99
|
+
const value = readToken(valueParseRegexp);
|
|
100
|
+
if (value.startsWith("!Date:")) {
|
|
101
|
+
return new Date(value.slice("!Date:".length));
|
|
102
|
+
}
|
|
103
|
+
return value;
|
|
97
104
|
}
|
|
98
105
|
if (type === ":") {
|
|
99
106
|
const value = readToken(valueParseRegexp);
|
|
@@ -270,10 +277,10 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
270
277
|
ignored += (ignored ? "&" : "?") + str;
|
|
271
278
|
}
|
|
272
279
|
const newCompacted = compact(newMerged, initialState);
|
|
280
|
+
let newQueryString = "";
|
|
273
281
|
if (Object.keys(newCompacted).length) {
|
|
274
282
|
const stringified = stringify(newCompacted);
|
|
275
283
|
const newQueryState = `${defaultedOptions.key}=${stringified};;`;
|
|
276
|
-
let newQueryString = "";
|
|
277
284
|
if (currentParsed) {
|
|
278
285
|
newQueryString = currentQueryString.replace(
|
|
279
286
|
splitMatcher,
|
|
@@ -284,13 +291,13 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
284
291
|
} else {
|
|
285
292
|
newQueryString = "?" + newQueryState;
|
|
286
293
|
}
|
|
287
|
-
history.replaceState(
|
|
288
|
-
history.state,
|
|
289
|
-
"",
|
|
290
|
-
location.pathname + newQueryString
|
|
291
|
-
);
|
|
292
294
|
} else {
|
|
293
|
-
|
|
295
|
+
newQueryString = ignored;
|
|
296
|
+
}
|
|
297
|
+
const currentUrl = location.pathname + location.search;
|
|
298
|
+
const newUrl = location.pathname + newQueryString;
|
|
299
|
+
if (newUrl !== currentUrl) {
|
|
300
|
+
history.replaceState(history.state, "", newUrl);
|
|
294
301
|
}
|
|
295
302
|
};
|
|
296
303
|
if (!api.__ZUSTAND_QUERYSTRING_INIT__) {
|
package/dist/index.mjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
querystring
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-S5N6DUVJ.mjs";
|
|
4
4
|
import {
|
|
5
5
|
createURL
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-PYA77ZUK.mjs";
|
|
7
7
|
import {
|
|
8
8
|
parse,
|
|
9
9
|
stringify
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-ZM24AXRE.mjs";
|
|
11
11
|
export {
|
|
12
12
|
createURL,
|
|
13
13
|
parse,
|
package/dist/middleware.js
CHANGED
|
@@ -51,6 +51,9 @@ function stringify(input, recursive) {
|
|
|
51
51
|
}
|
|
52
52
|
return "@" + res.join("&") + ";";
|
|
53
53
|
}
|
|
54
|
+
if (input instanceof Date) {
|
|
55
|
+
return "=!Date:" + encodeString(input.toISOString(), valueStringifyRegexp);
|
|
56
|
+
}
|
|
54
57
|
if (typeof input === "object") {
|
|
55
58
|
for (const [key, value] of Object.entries(input)) {
|
|
56
59
|
const stringifiedValue = stringify(value, true);
|
|
@@ -90,7 +93,11 @@ function parse(str) {
|
|
|
90
93
|
function parseToken() {
|
|
91
94
|
const type = str.charAt(pos++);
|
|
92
95
|
if (type === "=") {
|
|
93
|
-
|
|
96
|
+
const value = readToken(valueParseRegexp);
|
|
97
|
+
if (value.startsWith("!Date:")) {
|
|
98
|
+
return new Date(value.slice("!Date:".length));
|
|
99
|
+
}
|
|
100
|
+
return value;
|
|
94
101
|
}
|
|
95
102
|
if (type === ":") {
|
|
96
103
|
const value = readToken(valueParseRegexp);
|
|
@@ -267,10 +274,10 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
267
274
|
ignored += (ignored ? "&" : "?") + str;
|
|
268
275
|
}
|
|
269
276
|
const newCompacted = compact(newMerged, initialState);
|
|
277
|
+
let newQueryString = "";
|
|
270
278
|
if (Object.keys(newCompacted).length) {
|
|
271
279
|
const stringified = stringify(newCompacted);
|
|
272
280
|
const newQueryState = `${defaultedOptions.key}=${stringified};;`;
|
|
273
|
-
let newQueryString = "";
|
|
274
281
|
if (currentParsed) {
|
|
275
282
|
newQueryString = currentQueryString.replace(
|
|
276
283
|
splitMatcher,
|
|
@@ -281,13 +288,13 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
281
288
|
} else {
|
|
282
289
|
newQueryString = "?" + newQueryState;
|
|
283
290
|
}
|
|
284
|
-
history.replaceState(
|
|
285
|
-
history.state,
|
|
286
|
-
"",
|
|
287
|
-
location.pathname + newQueryString
|
|
288
|
-
);
|
|
289
291
|
} else {
|
|
290
|
-
|
|
292
|
+
newQueryString = ignored;
|
|
293
|
+
}
|
|
294
|
+
const currentUrl = location.pathname + location.search;
|
|
295
|
+
const newUrl = location.pathname + newQueryString;
|
|
296
|
+
if (newUrl !== currentUrl) {
|
|
297
|
+
history.replaceState(history.state, "", newUrl);
|
|
291
298
|
}
|
|
292
299
|
};
|
|
293
300
|
if (!api.__ZUSTAND_QUERYSTRING_INIT__) {
|
package/dist/middleware.mjs
CHANGED
package/dist/parser.js
CHANGED
|
@@ -50,6 +50,9 @@ function stringify(input, recursive) {
|
|
|
50
50
|
}
|
|
51
51
|
return "@" + res.join("&") + ";";
|
|
52
52
|
}
|
|
53
|
+
if (input instanceof Date) {
|
|
54
|
+
return "=!Date:" + encodeString(input.toISOString(), valueStringifyRegexp);
|
|
55
|
+
}
|
|
53
56
|
if (typeof input === "object") {
|
|
54
57
|
for (const [key, value] of Object.entries(input)) {
|
|
55
58
|
const stringifiedValue = stringify(value, true);
|
|
@@ -89,7 +92,11 @@ function parse(str) {
|
|
|
89
92
|
function parseToken() {
|
|
90
93
|
const type = str.charAt(pos++);
|
|
91
94
|
if (type === "=") {
|
|
92
|
-
|
|
95
|
+
const value = readToken(valueParseRegexp);
|
|
96
|
+
if (value.startsWith("!Date:")) {
|
|
97
|
+
return new Date(value.slice("!Date:".length));
|
|
98
|
+
}
|
|
99
|
+
return value;
|
|
93
100
|
}
|
|
94
101
|
if (type === ":") {
|
|
95
102
|
const value = readToken(valueParseRegexp);
|
package/dist/parser.mjs
CHANGED
package/dist/utils.js
CHANGED
|
@@ -51,6 +51,9 @@ function stringify(input, recursive) {
|
|
|
51
51
|
}
|
|
52
52
|
return "@" + res.join("&") + ";";
|
|
53
53
|
}
|
|
54
|
+
if (input instanceof Date) {
|
|
55
|
+
return "=!Date:" + encodeString(input.toISOString(), valueStringifyRegexp);
|
|
56
|
+
}
|
|
54
57
|
if (typeof input === "object") {
|
|
55
58
|
for (const [key, value] of Object.entries(input)) {
|
|
56
59
|
const stringifiedValue = stringify(value, true);
|
|
@@ -90,7 +93,11 @@ function parse(str) {
|
|
|
90
93
|
function parseToken() {
|
|
91
94
|
const type = str.charAt(pos++);
|
|
92
95
|
if (type === "=") {
|
|
93
|
-
|
|
96
|
+
const value = readToken(valueParseRegexp);
|
|
97
|
+
if (value.startsWith("!Date:")) {
|
|
98
|
+
return new Date(value.slice("!Date:".length));
|
|
99
|
+
}
|
|
100
|
+
return value;
|
|
94
101
|
}
|
|
95
102
|
if (type === ":") {
|
|
96
103
|
const value = readToken(valueParseRegexp);
|
package/dist/utils.mjs
CHANGED