zustand-querystring 0.3.0 → 0.3.2
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/LICENSE +21 -0
- package/README.md +22 -2
- package/dist/format/readable.d.mts +8 -2
- package/dist/format/readable.d.ts +8 -2
- package/dist/format/readable.js +115 -112
- package/dist/format/readable.mjs +115 -112
- package/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +16 -6
- package/dist/index.mjs +16 -6
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 nitedani
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -8,7 +8,6 @@ Examples:
|
|
|
8
8
|
|
|
9
9
|
- [React](./examples/react/)
|
|
10
10
|
- [NextJS](./examples/next/)
|
|
11
|
-
- [Rakkas](./examples/rakkas/)
|
|
12
11
|
|
|
13
12
|
Quickstart:
|
|
14
13
|
|
|
@@ -60,5 +59,26 @@ export const useStore = create<Store>()(
|
|
|
60
59
|
querystring options:
|
|
61
60
|
|
|
62
61
|
- <b>select</b> - the select option controls what part of the state is synced with the query string
|
|
63
|
-
- <b>key: string</b> - the key option controls how the state is stored in the querystring (default:
|
|
62
|
+
- <b>key: string</b> - the key option controls how the state is stored in the querystring (default: 'state')
|
|
64
63
|
- <b>url</b> - the url option is used to provide the request url on the server side render
|
|
64
|
+
- <b>format</b> - custom format for stringify/parse (default: JSON-based format)
|
|
65
|
+
- <b>syncNull: boolean</b> - when true, null values that differ from initial state are synced to URL (default: false)
|
|
66
|
+
- <b>syncUndefined: boolean</b> - when true, undefined values that differ from initial state are synced to URL (default: false)
|
|
67
|
+
|
|
68
|
+
## Important Notes
|
|
69
|
+
|
|
70
|
+
### State Diffing
|
|
71
|
+
|
|
72
|
+
Only values that differ from the initial state are synced to the URL.
|
|
73
|
+
|
|
74
|
+
### Null and Undefined Handling
|
|
75
|
+
|
|
76
|
+
By default (`syncNull: false`, `syncUndefined: false`), `null` and `undefined` values are **not** synced to the URL. This means setting a value to `null` or `undefined` effectively "clears" it back to the initial state on page refresh.
|
|
77
|
+
|
|
78
|
+
If you want to preserve `null` or `undefined` values in the URL (so they persist across refreshes), set `syncNull: true` or `syncUndefined: true` in options.
|
|
79
|
+
|
|
80
|
+
### State Types
|
|
81
|
+
|
|
82
|
+
- **Plain objects** (created with `{}`) are recursively compared with initial state - only changed properties are synced
|
|
83
|
+
- **Arrays, Dates, RegExp, Maps, Sets, and class instances** are compared as atomic values - if any part changes, the entire value is synced
|
|
84
|
+
- **Functions** are never synced to the URL
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* URL-Safe Serialization
|
|
3
|
+
*
|
|
4
|
+
* Inspired by URLON (https://github.com/cerebral/urlon)
|
|
5
|
+
* Copyright (c) 2021 Cerebral - MIT License
|
|
6
|
+
*/
|
|
7
|
+
declare function stringify(value: unknown): string;
|
|
8
|
+
declare function parse<T = unknown>(input: string): T;
|
|
3
9
|
|
|
4
10
|
export { parse, stringify };
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* URL-Safe Serialization
|
|
3
|
+
*
|
|
4
|
+
* Inspired by URLON (https://github.com/cerebral/urlon)
|
|
5
|
+
* Copyright (c) 2021 Cerebral - MIT License
|
|
6
|
+
*/
|
|
7
|
+
declare function stringify(value: unknown): string;
|
|
8
|
+
declare function parse<T = unknown>(input: string): T;
|
|
3
9
|
|
|
4
10
|
export { parse, stringify };
|
package/dist/format/readable.js
CHANGED
|
@@ -23,139 +23,142 @@ __export(readable_exports, {
|
|
|
23
23
|
stringify: () => stringify
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(readable_exports);
|
|
26
|
-
var
|
|
27
|
-
var
|
|
28
|
-
var
|
|
29
|
-
var
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
var TYPE_OBJECT = ".";
|
|
27
|
+
var TYPE_ARRAY = "@";
|
|
28
|
+
var TYPE_STRING = "=";
|
|
29
|
+
var TYPE_PRIMITIVE = ":";
|
|
30
|
+
var SEPARATOR = ",";
|
|
31
|
+
var TERMINATOR = "~";
|
|
32
|
+
var ESCAPE = "/";
|
|
33
|
+
var DATE_PREFIX = "!Date:";
|
|
34
|
+
var SEP_CHAR = SEPARATOR[0];
|
|
35
|
+
var esc = (c) => /[.$^*+?()[\]{}|\\]/.test(c) ? `\\${c}` : c;
|
|
36
|
+
var KEY_STOP = new RegExp(`[${TYPE_STRING}${TYPE_PRIMITIVE}${TYPE_ARRAY}${esc(TYPE_OBJECT)}${esc(SEP_CHAR)}]`);
|
|
37
|
+
var VALUE_STOP = new RegExp(`[${esc(SEP_CHAR)}${TERMINATOR}]`);
|
|
38
|
+
var KEY_ESCAPE = new RegExp(`([${TYPE_STRING}${TYPE_PRIMITIVE}${TYPE_ARRAY}${esc(TYPE_OBJECT)}${ESCAPE}${esc(SEP_CHAR)}])`, "g");
|
|
39
|
+
var VALUE_ESCAPE = new RegExp(`([${esc(SEP_CHAR)}${TERMINATOR}${ESCAPE}])`, "g");
|
|
40
|
+
function escapeStr(str, pattern) {
|
|
41
|
+
return encodeURI(str.replace(pattern, `${ESCAPE}$1`));
|
|
32
42
|
}
|
|
33
|
-
function
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!recursive) {
|
|
38
|
-
return trim(stringify(input, true));
|
|
43
|
+
function cleanResult(str) {
|
|
44
|
+
while (str.endsWith(TERMINATOR)) str = str.slice(0, -1);
|
|
45
|
+
if (str.startsWith(TYPE_OBJECT) || str.startsWith(TYPE_ARRAY)) {
|
|
46
|
+
str = str.slice(1);
|
|
39
47
|
}
|
|
40
|
-
|
|
41
|
-
|
|
48
|
+
return str;
|
|
49
|
+
}
|
|
50
|
+
function stringify(value) {
|
|
51
|
+
return cleanResult(serialize(value));
|
|
52
|
+
}
|
|
53
|
+
function serialize(value) {
|
|
54
|
+
if (value === null) return `${TYPE_PRIMITIVE}null`;
|
|
55
|
+
if (value === void 0) return `${TYPE_PRIMITIVE}undefined`;
|
|
56
|
+
if (typeof value === "function") return "";
|
|
57
|
+
if (typeof value === "number") {
|
|
58
|
+
return `${TYPE_PRIMITIVE}${String(value).replace(/\./g, `${ESCAPE}.`)}`;
|
|
42
59
|
}
|
|
43
|
-
if (typeof
|
|
44
|
-
|
|
45
|
-
return ":" + value.replace(/\./g, "/.");
|
|
60
|
+
if (typeof value === "boolean") {
|
|
61
|
+
return `${TYPE_PRIMITIVE}${value}`;
|
|
46
62
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
for (const elem of input) {
|
|
50
|
-
typeof elem === "undefined" ? res.push(":null") : res.push(stringify(elem, true));
|
|
51
|
-
}
|
|
52
|
-
return "@" + res.join("..") + "~";
|
|
63
|
+
if (value instanceof Date) {
|
|
64
|
+
return `${TYPE_STRING}${DATE_PREFIX}${escapeStr(value.toISOString(), VALUE_ESCAPE)}`;
|
|
53
65
|
}
|
|
54
|
-
if (
|
|
55
|
-
|
|
66
|
+
if (Array.isArray(value)) {
|
|
67
|
+
const items = value.map((v) => serialize(v));
|
|
68
|
+
return `${TYPE_ARRAY}${items.join(SEPARATOR)}${TERMINATOR}`;
|
|
56
69
|
}
|
|
57
|
-
if (typeof
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
70
|
+
if (typeof value === "object") {
|
|
71
|
+
const entries = [];
|
|
72
|
+
for (const [k, v] of Object.entries(value)) {
|
|
73
|
+
const val = serialize(v);
|
|
74
|
+
if (val || v === void 0) {
|
|
75
|
+
entries.push(`${escapeStr(k, KEY_ESCAPE)}${val}`);
|
|
62
76
|
}
|
|
63
77
|
}
|
|
64
|
-
return
|
|
78
|
+
return `${TYPE_OBJECT}${entries.join(SEPARATOR)}${TERMINATOR}`;
|
|
65
79
|
}
|
|
66
|
-
|
|
67
|
-
return "";
|
|
68
|
-
}
|
|
69
|
-
return "=" + encodeString(input.toString(), valueStringifyRegexp);
|
|
80
|
+
return `${TYPE_STRING}${escapeStr(String(value), VALUE_ESCAPE)}`;
|
|
70
81
|
}
|
|
71
|
-
function parse(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
82
|
+
function parse(input) {
|
|
83
|
+
const str = decodeURI(input);
|
|
84
|
+
const first = str[0];
|
|
85
|
+
const hasMarker = first === TYPE_STRING || first === TYPE_PRIMITIVE || first === TYPE_ARRAY || first === TYPE_OBJECT;
|
|
75
86
|
let pos = 0;
|
|
76
|
-
|
|
77
|
-
function
|
|
78
|
-
let
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
} else if (str.charAt(pos).match(regexp)) {
|
|
87
|
-
break;
|
|
87
|
+
const source = hasMarker ? str : `${TYPE_OBJECT}${str}`;
|
|
88
|
+
function readUntil(pattern) {
|
|
89
|
+
let result = "";
|
|
90
|
+
while (pos < source.length) {
|
|
91
|
+
const char = source[pos];
|
|
92
|
+
if (char === ESCAPE) {
|
|
93
|
+
pos++;
|
|
94
|
+
result += pos < source.length ? source[pos++] : TERMINATOR;
|
|
95
|
+
continue;
|
|
88
96
|
}
|
|
89
|
-
|
|
97
|
+
if (pattern.test(char)) break;
|
|
98
|
+
result += char;
|
|
99
|
+
pos++;
|
|
90
100
|
}
|
|
91
|
-
return
|
|
101
|
+
return result;
|
|
92
102
|
}
|
|
93
|
-
function
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const value = readToken(valueParseRegexp);
|
|
97
|
-
if (value.startsWith("!Date:")) {
|
|
98
|
-
return new Date(value.slice("!Date:".length));
|
|
99
|
-
}
|
|
100
|
-
return value;
|
|
103
|
+
function skipSeparator() {
|
|
104
|
+
if (source[pos] === SEPARATOR) {
|
|
105
|
+
pos++;
|
|
101
106
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (value === "false") {
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
const parsedValue = parseFloat(value);
|
|
111
|
-
return isNaN(parsedValue) ? null : parsedValue;
|
|
107
|
+
}
|
|
108
|
+
function parseString() {
|
|
109
|
+
const val = readUntil(VALUE_STOP);
|
|
110
|
+
if (val.startsWith(DATE_PREFIX)) {
|
|
111
|
+
return new Date(val.slice(DATE_PREFIX.length));
|
|
112
112
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
113
|
+
return val;
|
|
114
|
+
}
|
|
115
|
+
function parsePrimitive() {
|
|
116
|
+
const val = readUntil(VALUE_STOP);
|
|
117
|
+
if (val === "null") return null;
|
|
118
|
+
if (val === "undefined") return void 0;
|
|
119
|
+
if (val === "true") return true;
|
|
120
|
+
if (val === "false") return false;
|
|
121
|
+
return parseFloat(val);
|
|
122
|
+
}
|
|
123
|
+
function parseArray() {
|
|
124
|
+
const result = [];
|
|
125
|
+
while (pos < source.length && source[pos] !== TERMINATOR) {
|
|
126
|
+
result.push(parseValue());
|
|
127
|
+
if (pos < source.length && source[pos] !== TERMINATOR) {
|
|
128
|
+
skipSeparator();
|
|
130
129
|
}
|
|
131
|
-
pos += 1;
|
|
132
|
-
return res;
|
|
133
130
|
}
|
|
134
|
-
if (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
break loop;
|
|
145
|
-
}
|
|
146
|
-
if (str.charAt(pos) === "." && str.charAt(pos + 1) === ".") {
|
|
147
|
-
pos += 2;
|
|
148
|
-
} else {
|
|
149
|
-
pos += 1;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
131
|
+
if (source[pos] === TERMINATOR) pos++;
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
function parseObject() {
|
|
135
|
+
const result = {};
|
|
136
|
+
while (pos < source.length && source[pos] !== TERMINATOR) {
|
|
137
|
+
const key = readUntil(KEY_STOP);
|
|
138
|
+
result[key] = parseValue();
|
|
139
|
+
if (pos < source.length && source[pos] !== TERMINATOR) {
|
|
140
|
+
skipSeparator();
|
|
152
141
|
}
|
|
153
|
-
pos += 1;
|
|
154
|
-
return res;
|
|
155
142
|
}
|
|
156
|
-
|
|
143
|
+
if (source[pos] === TERMINATOR) pos++;
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
function parseValue() {
|
|
147
|
+
const type = source[pos++];
|
|
148
|
+
switch (type) {
|
|
149
|
+
case TYPE_STRING:
|
|
150
|
+
return parseString();
|
|
151
|
+
case TYPE_PRIMITIVE:
|
|
152
|
+
return parsePrimitive();
|
|
153
|
+
case TYPE_ARRAY:
|
|
154
|
+
return parseArray();
|
|
155
|
+
case TYPE_OBJECT:
|
|
156
|
+
return parseObject();
|
|
157
|
+
default:
|
|
158
|
+
throw new Error(`Unexpected type "${type}" at position ${pos - 1}`);
|
|
159
|
+
}
|
|
157
160
|
}
|
|
158
|
-
return
|
|
161
|
+
return parseValue();
|
|
159
162
|
}
|
|
160
163
|
// Annotate the CommonJS export names for ESM import in node:
|
|
161
164
|
0 && (module.exports = {
|
package/dist/format/readable.mjs
CHANGED
|
@@ -1,137 +1,140 @@
|
|
|
1
1
|
// src/format/readable.ts
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
var TYPE_OBJECT = ".";
|
|
3
|
+
var TYPE_ARRAY = "@";
|
|
4
|
+
var TYPE_STRING = "=";
|
|
5
|
+
var TYPE_PRIMITIVE = ":";
|
|
6
|
+
var SEPARATOR = ",";
|
|
7
|
+
var TERMINATOR = "~";
|
|
8
|
+
var ESCAPE = "/";
|
|
9
|
+
var DATE_PREFIX = "!Date:";
|
|
10
|
+
var SEP_CHAR = SEPARATOR[0];
|
|
11
|
+
var esc = (c) => /[.$^*+?()[\]{}|\\]/.test(c) ? `\\${c}` : c;
|
|
12
|
+
var KEY_STOP = new RegExp(`[${TYPE_STRING}${TYPE_PRIMITIVE}${TYPE_ARRAY}${esc(TYPE_OBJECT)}${esc(SEP_CHAR)}]`);
|
|
13
|
+
var VALUE_STOP = new RegExp(`[${esc(SEP_CHAR)}${TERMINATOR}]`);
|
|
14
|
+
var KEY_ESCAPE = new RegExp(`([${TYPE_STRING}${TYPE_PRIMITIVE}${TYPE_ARRAY}${esc(TYPE_OBJECT)}${ESCAPE}${esc(SEP_CHAR)}])`, "g");
|
|
15
|
+
var VALUE_ESCAPE = new RegExp(`([${esc(SEP_CHAR)}${TERMINATOR}${ESCAPE}])`, "g");
|
|
16
|
+
function escapeStr(str, pattern) {
|
|
17
|
+
return encodeURI(str.replace(pattern, `${ESCAPE}$1`));
|
|
8
18
|
}
|
|
9
|
-
function
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (!recursive) {
|
|
14
|
-
return trim(stringify(input, true));
|
|
19
|
+
function cleanResult(str) {
|
|
20
|
+
while (str.endsWith(TERMINATOR)) str = str.slice(0, -1);
|
|
21
|
+
if (str.startsWith(TYPE_OBJECT) || str.startsWith(TYPE_ARRAY)) {
|
|
22
|
+
str = str.slice(1);
|
|
15
23
|
}
|
|
16
|
-
|
|
17
|
-
|
|
24
|
+
return str;
|
|
25
|
+
}
|
|
26
|
+
function stringify(value) {
|
|
27
|
+
return cleanResult(serialize(value));
|
|
28
|
+
}
|
|
29
|
+
function serialize(value) {
|
|
30
|
+
if (value === null) return `${TYPE_PRIMITIVE}null`;
|
|
31
|
+
if (value === void 0) return `${TYPE_PRIMITIVE}undefined`;
|
|
32
|
+
if (typeof value === "function") return "";
|
|
33
|
+
if (typeof value === "number") {
|
|
34
|
+
return `${TYPE_PRIMITIVE}${String(value).replace(/\./g, `${ESCAPE}.`)}`;
|
|
18
35
|
}
|
|
19
|
-
if (typeof
|
|
20
|
-
|
|
21
|
-
return ":" + value.replace(/\./g, "/.");
|
|
36
|
+
if (typeof value === "boolean") {
|
|
37
|
+
return `${TYPE_PRIMITIVE}${value}`;
|
|
22
38
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
for (const elem of input) {
|
|
26
|
-
typeof elem === "undefined" ? res.push(":null") : res.push(stringify(elem, true));
|
|
27
|
-
}
|
|
28
|
-
return "@" + res.join("..") + "~";
|
|
39
|
+
if (value instanceof Date) {
|
|
40
|
+
return `${TYPE_STRING}${DATE_PREFIX}${escapeStr(value.toISOString(), VALUE_ESCAPE)}`;
|
|
29
41
|
}
|
|
30
|
-
if (
|
|
31
|
-
|
|
42
|
+
if (Array.isArray(value)) {
|
|
43
|
+
const items = value.map((v) => serialize(v));
|
|
44
|
+
return `${TYPE_ARRAY}${items.join(SEPARATOR)}${TERMINATOR}`;
|
|
32
45
|
}
|
|
33
|
-
if (typeof
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
46
|
+
if (typeof value === "object") {
|
|
47
|
+
const entries = [];
|
|
48
|
+
for (const [k, v] of Object.entries(value)) {
|
|
49
|
+
const val = serialize(v);
|
|
50
|
+
if (val || v === void 0) {
|
|
51
|
+
entries.push(`${escapeStr(k, KEY_ESCAPE)}${val}`);
|
|
38
52
|
}
|
|
39
53
|
}
|
|
40
|
-
return
|
|
54
|
+
return `${TYPE_OBJECT}${entries.join(SEPARATOR)}${TERMINATOR}`;
|
|
41
55
|
}
|
|
42
|
-
|
|
43
|
-
return "";
|
|
44
|
-
}
|
|
45
|
-
return "=" + encodeString(input.toString(), valueStringifyRegexp);
|
|
56
|
+
return `${TYPE_STRING}${escapeStr(String(value), VALUE_ESCAPE)}`;
|
|
46
57
|
}
|
|
47
|
-
function parse(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
function parse(input) {
|
|
59
|
+
const str = decodeURI(input);
|
|
60
|
+
const first = str[0];
|
|
61
|
+
const hasMarker = first === TYPE_STRING || first === TYPE_PRIMITIVE || first === TYPE_ARRAY || first === TYPE_OBJECT;
|
|
51
62
|
let pos = 0;
|
|
52
|
-
|
|
53
|
-
function
|
|
54
|
-
let
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
} else if (str.charAt(pos).match(regexp)) {
|
|
63
|
-
break;
|
|
63
|
+
const source = hasMarker ? str : `${TYPE_OBJECT}${str}`;
|
|
64
|
+
function readUntil(pattern) {
|
|
65
|
+
let result = "";
|
|
66
|
+
while (pos < source.length) {
|
|
67
|
+
const char = source[pos];
|
|
68
|
+
if (char === ESCAPE) {
|
|
69
|
+
pos++;
|
|
70
|
+
result += pos < source.length ? source[pos++] : TERMINATOR;
|
|
71
|
+
continue;
|
|
64
72
|
}
|
|
65
|
-
|
|
73
|
+
if (pattern.test(char)) break;
|
|
74
|
+
result += char;
|
|
75
|
+
pos++;
|
|
66
76
|
}
|
|
67
|
-
return
|
|
77
|
+
return result;
|
|
68
78
|
}
|
|
69
|
-
function
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const value = readToken(valueParseRegexp);
|
|
73
|
-
if (value.startsWith("!Date:")) {
|
|
74
|
-
return new Date(value.slice("!Date:".length));
|
|
75
|
-
}
|
|
76
|
-
return value;
|
|
79
|
+
function skipSeparator() {
|
|
80
|
+
if (source[pos] === SEPARATOR) {
|
|
81
|
+
pos++;
|
|
77
82
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (value === "false") {
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
const parsedValue = parseFloat(value);
|
|
87
|
-
return isNaN(parsedValue) ? null : parsedValue;
|
|
83
|
+
}
|
|
84
|
+
function parseString() {
|
|
85
|
+
const val = readUntil(VALUE_STOP);
|
|
86
|
+
if (val.startsWith(DATE_PREFIX)) {
|
|
87
|
+
return new Date(val.slice(DATE_PREFIX.length));
|
|
88
88
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
89
|
+
return val;
|
|
90
|
+
}
|
|
91
|
+
function parsePrimitive() {
|
|
92
|
+
const val = readUntil(VALUE_STOP);
|
|
93
|
+
if (val === "null") return null;
|
|
94
|
+
if (val === "undefined") return void 0;
|
|
95
|
+
if (val === "true") return true;
|
|
96
|
+
if (val === "false") return false;
|
|
97
|
+
return parseFloat(val);
|
|
98
|
+
}
|
|
99
|
+
function parseArray() {
|
|
100
|
+
const result = [];
|
|
101
|
+
while (pos < source.length && source[pos] !== TERMINATOR) {
|
|
102
|
+
result.push(parseValue());
|
|
103
|
+
if (pos < source.length && source[pos] !== TERMINATOR) {
|
|
104
|
+
skipSeparator();
|
|
106
105
|
}
|
|
107
|
-
pos += 1;
|
|
108
|
-
return res;
|
|
109
106
|
}
|
|
110
|
-
if (
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
break loop;
|
|
121
|
-
}
|
|
122
|
-
if (str.charAt(pos) === "." && str.charAt(pos + 1) === ".") {
|
|
123
|
-
pos += 2;
|
|
124
|
-
} else {
|
|
125
|
-
pos += 1;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
107
|
+
if (source[pos] === TERMINATOR) pos++;
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
function parseObject() {
|
|
111
|
+
const result = {};
|
|
112
|
+
while (pos < source.length && source[pos] !== TERMINATOR) {
|
|
113
|
+
const key = readUntil(KEY_STOP);
|
|
114
|
+
result[key] = parseValue();
|
|
115
|
+
if (pos < source.length && source[pos] !== TERMINATOR) {
|
|
116
|
+
skipSeparator();
|
|
128
117
|
}
|
|
129
|
-
pos += 1;
|
|
130
|
-
return res;
|
|
131
118
|
}
|
|
132
|
-
|
|
119
|
+
if (source[pos] === TERMINATOR) pos++;
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
function parseValue() {
|
|
123
|
+
const type = source[pos++];
|
|
124
|
+
switch (type) {
|
|
125
|
+
case TYPE_STRING:
|
|
126
|
+
return parseString();
|
|
127
|
+
case TYPE_PRIMITIVE:
|
|
128
|
+
return parsePrimitive();
|
|
129
|
+
case TYPE_ARRAY:
|
|
130
|
+
return parseArray();
|
|
131
|
+
case TYPE_OBJECT:
|
|
132
|
+
return parseObject();
|
|
133
|
+
default:
|
|
134
|
+
throw new Error(`Unexpected type "${type}" at position ${pos - 1}`);
|
|
135
|
+
}
|
|
133
136
|
}
|
|
134
|
-
return
|
|
137
|
+
return parseValue();
|
|
135
138
|
}
|
|
136
139
|
export {
|
|
137
140
|
parse,
|
package/dist/index.d.mts
CHANGED
|
@@ -14,6 +14,8 @@ interface QueryStringOptions<T> {
|
|
|
14
14
|
stringify: (value: DeepPartial<T>) => string;
|
|
15
15
|
parse: (value: string) => DeepPartial<T>;
|
|
16
16
|
};
|
|
17
|
+
syncNull?: boolean;
|
|
18
|
+
syncUndefined?: boolean;
|
|
17
19
|
}
|
|
18
20
|
type QueryString = <T, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []>(initializer: StateCreator<T, Mps, Mcs>, options?: QueryStringOptions<T>) => StateCreator<T, Mps, Mcs>;
|
|
19
21
|
declare const querystring: QueryString;
|
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,8 @@ interface QueryStringOptions<T> {
|
|
|
14
14
|
stringify: (value: DeepPartial<T>) => string;
|
|
15
15
|
parse: (value: string) => DeepPartial<T>;
|
|
16
16
|
};
|
|
17
|
+
syncNull?: boolean;
|
|
18
|
+
syncUndefined?: boolean;
|
|
17
19
|
}
|
|
18
20
|
type QueryString = <T, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []>(initializer: StateCreator<T, Mps, Mcs>, options?: QueryStringOptions<T>) => StateCreator<T, Mps, Mcs>;
|
|
19
21
|
declare const querystring: QueryString;
|
package/dist/index.js
CHANGED
|
@@ -38,17 +38,20 @@ function parse(str) {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
// src/middleware.ts
|
|
41
|
-
var compact = (newState, initialState) => {
|
|
41
|
+
var compact = (newState, initialState, syncNull = false, syncUndefined = false) => {
|
|
42
42
|
const output = {};
|
|
43
43
|
Object.keys(newState).forEach((key) => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
const newValue = newState[key];
|
|
45
|
+
const initialValue = initialState[key];
|
|
46
|
+
if (typeof newValue !== "function" && !(0, import_lodash_es.isEqual)(newValue, initialValue) && (syncNull || newValue !== null) && (syncUndefined || newValue !== void 0)) {
|
|
47
|
+
const isPlainObject = typeof newValue === "object" && newValue !== null && newValue !== void 0 && !Array.isArray(newValue) && newValue.constructor === Object;
|
|
48
|
+
if (isPlainObject && initialValue && typeof initialValue === "object") {
|
|
49
|
+
const value = compact(newValue, initialValue, syncNull, syncUndefined);
|
|
47
50
|
if (value && Object.keys(value).length > 0) {
|
|
48
51
|
output[key] = value;
|
|
49
52
|
}
|
|
50
53
|
} else {
|
|
51
|
-
output[key] =
|
|
54
|
+
output[key] = newValue;
|
|
52
55
|
}
|
|
53
56
|
}
|
|
54
57
|
});
|
|
@@ -80,6 +83,8 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
80
83
|
stringify,
|
|
81
84
|
parse
|
|
82
85
|
},
|
|
86
|
+
syncNull: false,
|
|
87
|
+
syncUndefined: false,
|
|
83
88
|
...options
|
|
84
89
|
};
|
|
85
90
|
const getStateFromUrl = (url) => {
|
|
@@ -132,7 +137,12 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
132
137
|
const setQuery = () => {
|
|
133
138
|
const url = new URL(window.location.href);
|
|
134
139
|
const selectedState = getSelectedState(get(), url.pathname);
|
|
135
|
-
const newCompacted = compact(
|
|
140
|
+
const newCompacted = compact(
|
|
141
|
+
selectedState,
|
|
142
|
+
initialState,
|
|
143
|
+
defaultedOptions.syncNull,
|
|
144
|
+
defaultedOptions.syncUndefined
|
|
145
|
+
);
|
|
136
146
|
const previous = url.search;
|
|
137
147
|
const params = url.search.slice(1).split("&").filter(Boolean);
|
|
138
148
|
let stateIndex = -1;
|
package/dist/index.mjs
CHANGED
|
@@ -10,17 +10,20 @@ function parse(str) {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
// src/middleware.ts
|
|
13
|
-
var compact = (newState, initialState) => {
|
|
13
|
+
var compact = (newState, initialState, syncNull = false, syncUndefined = false) => {
|
|
14
14
|
const output = {};
|
|
15
15
|
Object.keys(newState).forEach((key) => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
const newValue = newState[key];
|
|
17
|
+
const initialValue = initialState[key];
|
|
18
|
+
if (typeof newValue !== "function" && !isEqual(newValue, initialValue) && (syncNull || newValue !== null) && (syncUndefined || newValue !== void 0)) {
|
|
19
|
+
const isPlainObject = typeof newValue === "object" && newValue !== null && newValue !== void 0 && !Array.isArray(newValue) && newValue.constructor === Object;
|
|
20
|
+
if (isPlainObject && initialValue && typeof initialValue === "object") {
|
|
21
|
+
const value = compact(newValue, initialValue, syncNull, syncUndefined);
|
|
19
22
|
if (value && Object.keys(value).length > 0) {
|
|
20
23
|
output[key] = value;
|
|
21
24
|
}
|
|
22
25
|
} else {
|
|
23
|
-
output[key] =
|
|
26
|
+
output[key] = newValue;
|
|
24
27
|
}
|
|
25
28
|
}
|
|
26
29
|
});
|
|
@@ -52,6 +55,8 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
52
55
|
stringify,
|
|
53
56
|
parse
|
|
54
57
|
},
|
|
58
|
+
syncNull: false,
|
|
59
|
+
syncUndefined: false,
|
|
55
60
|
...options
|
|
56
61
|
};
|
|
57
62
|
const getStateFromUrl = (url) => {
|
|
@@ -104,7 +109,12 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
104
109
|
const setQuery = () => {
|
|
105
110
|
const url = new URL(window.location.href);
|
|
106
111
|
const selectedState = getSelectedState(get(), url.pathname);
|
|
107
|
-
const newCompacted = compact(
|
|
112
|
+
const newCompacted = compact(
|
|
113
|
+
selectedState,
|
|
114
|
+
initialState,
|
|
115
|
+
defaultedOptions.syncNull,
|
|
116
|
+
defaultedOptions.syncUndefined
|
|
117
|
+
);
|
|
108
118
|
const previous = url.search;
|
|
109
119
|
const params = url.search.slice(1).split("&").filter(Boolean);
|
|
110
120
|
let stateIndex = -1;
|