zustand-querystring 0.3.1 → 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 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.
@@ -1,4 +1,10 @@
1
- declare function stringify(input: unknown, recursive?: boolean): string;
2
- declare function parse<T = unknown>(str: string): T;
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
- declare function stringify(input: unknown, recursive?: boolean): string;
2
- declare function parse<T = unknown>(str: string): T;
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 };
@@ -23,139 +23,142 @@ __export(readable_exports, {
23
23
  stringify: () => stringify
24
24
  });
25
25
  module.exports = __toCommonJS(readable_exports);
26
- var keyStringifyRegexp = /([=:@$/.])/g;
27
- var valueStringifyRegexp = /([.~/])/g;
28
- var keyParseRegexp = /[=:@$.]/;
29
- var valueParseRegexp = /[.~]/;
30
- function encodeString(str, regexp) {
31
- return encodeURI(str.replace(regexp, "/$1"));
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 trim(res) {
34
- return typeof res === "string" ? res.replace(/~+$/g, "").replace(/^\$/, "") : res;
35
- }
36
- function stringify(input, recursive) {
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
- if (typeof input === "function") {
41
- return "";
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 input === "number" || input === true || input === false || input === null) {
44
- const value = String(input);
45
- return ":" + value.replace(/\./g, "/.");
60
+ if (typeof value === "boolean") {
61
+ return `${TYPE_PRIMITIVE}${value}`;
46
62
  }
47
- const res = [];
48
- if (Array.isArray(input)) {
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 (input instanceof Date) {
55
- return "=!Date:" + encodeString(input.toISOString(), valueStringifyRegexp);
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 input === "object") {
58
- for (const [key, value] of Object.entries(input)) {
59
- const stringifiedValue = stringify(value, true);
60
- if (stringifiedValue) {
61
- res.push(encodeString(key, keyStringifyRegexp) + stringifiedValue);
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 "$" + res.join("..") + "~";
78
+ return `${TYPE_OBJECT}${entries.join(SEPARATOR)}${TERMINATOR}`;
65
79
  }
66
- if (typeof input === "undefined") {
67
- return "";
68
- }
69
- return "=" + encodeString(input.toString(), valueStringifyRegexp);
80
+ return `${TYPE_STRING}${escapeStr(String(value), VALUE_ESCAPE)}`;
70
81
  }
71
- function parse(str) {
72
- if (!str.startsWith("$")) {
73
- str = "$" + str;
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
- str = decodeURI(str);
77
- function readToken(regexp) {
78
- let token = "";
79
- for (; pos !== str.length; ++pos) {
80
- if (str.charAt(pos) === "/") {
81
- pos += 1;
82
- if (pos === str.length) {
83
- token += "~";
84
- break;
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
- token += str.charAt(pos);
97
+ if (pattern.test(char)) break;
98
+ result += char;
99
+ pos++;
90
100
  }
91
- return token;
101
+ return result;
92
102
  }
93
- function parseToken() {
94
- const type = str.charAt(pos++);
95
- if (type === "=") {
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
- if (type === ":") {
103
- const value = readToken(valueParseRegexp);
104
- if (value === "true") {
105
- return true;
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
- if (type === "@") {
114
- const res = [];
115
- loop: {
116
- if (pos >= str.length || str.charAt(pos) === "~") {
117
- break loop;
118
- }
119
- while (true) {
120
- res.push(parseToken());
121
- if (pos >= str.length || str.charAt(pos) === "~") {
122
- break loop;
123
- }
124
- if (str.charAt(pos) === "." && str.charAt(pos + 1) === ".") {
125
- pos += 2;
126
- } else {
127
- pos += 1;
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 (type === "$") {
135
- const res = {};
136
- loop: {
137
- if (pos >= str.length || str.charAt(pos) === "~") {
138
- break loop;
139
- }
140
- while (true) {
141
- const name = readToken(keyParseRegexp);
142
- res[name] = parseToken();
143
- if (pos >= str.length || str.charAt(pos) === "~") {
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
- throw new Error('Unexpected char "' + type + '" at position ' + (pos - 1));
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 parseToken();
161
+ return parseValue();
159
162
  }
160
163
  // Annotate the CommonJS export names for ESM import in node:
161
164
  0 && (module.exports = {
@@ -1,137 +1,140 @@
1
1
  // src/format/readable.ts
2
- var keyStringifyRegexp = /([=:@$/.])/g;
3
- var valueStringifyRegexp = /([.~/])/g;
4
- var keyParseRegexp = /[=:@$.]/;
5
- var valueParseRegexp = /[.~]/;
6
- function encodeString(str, regexp) {
7
- return encodeURI(str.replace(regexp, "/$1"));
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 trim(res) {
10
- return typeof res === "string" ? res.replace(/~+$/g, "").replace(/^\$/, "") : res;
11
- }
12
- function stringify(input, recursive) {
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
- if (typeof input === "function") {
17
- return "";
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 input === "number" || input === true || input === false || input === null) {
20
- const value = String(input);
21
- return ":" + value.replace(/\./g, "/.");
36
+ if (typeof value === "boolean") {
37
+ return `${TYPE_PRIMITIVE}${value}`;
22
38
  }
23
- const res = [];
24
- if (Array.isArray(input)) {
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 (input instanceof Date) {
31
- return "=!Date:" + encodeString(input.toISOString(), valueStringifyRegexp);
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 input === "object") {
34
- for (const [key, value] of Object.entries(input)) {
35
- const stringifiedValue = stringify(value, true);
36
- if (stringifiedValue) {
37
- res.push(encodeString(key, keyStringifyRegexp) + stringifiedValue);
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 "$" + res.join("..") + "~";
54
+ return `${TYPE_OBJECT}${entries.join(SEPARATOR)}${TERMINATOR}`;
41
55
  }
42
- if (typeof input === "undefined") {
43
- return "";
44
- }
45
- return "=" + encodeString(input.toString(), valueStringifyRegexp);
56
+ return `${TYPE_STRING}${escapeStr(String(value), VALUE_ESCAPE)}`;
46
57
  }
47
- function parse(str) {
48
- if (!str.startsWith("$")) {
49
- str = "$" + str;
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
- str = decodeURI(str);
53
- function readToken(regexp) {
54
- let token = "";
55
- for (; pos !== str.length; ++pos) {
56
- if (str.charAt(pos) === "/") {
57
- pos += 1;
58
- if (pos === str.length) {
59
- token += "~";
60
- break;
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
- token += str.charAt(pos);
73
+ if (pattern.test(char)) break;
74
+ result += char;
75
+ pos++;
66
76
  }
67
- return token;
77
+ return result;
68
78
  }
69
- function parseToken() {
70
- const type = str.charAt(pos++);
71
- if (type === "=") {
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
- if (type === ":") {
79
- const value = readToken(valueParseRegexp);
80
- if (value === "true") {
81
- return true;
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
- if (type === "@") {
90
- const res = [];
91
- loop: {
92
- if (pos >= str.length || str.charAt(pos) === "~") {
93
- break loop;
94
- }
95
- while (true) {
96
- res.push(parseToken());
97
- if (pos >= str.length || str.charAt(pos) === "~") {
98
- break loop;
99
- }
100
- if (str.charAt(pos) === "." && str.charAt(pos + 1) === ".") {
101
- pos += 2;
102
- } else {
103
- pos += 1;
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 (type === "$") {
111
- const res = {};
112
- loop: {
113
- if (pos >= str.length || str.charAt(pos) === "~") {
114
- break loop;
115
- }
116
- while (true) {
117
- const name = readToken(keyParseRegexp);
118
- res[name] = parseToken();
119
- if (pos >= str.length || str.charAt(pos) === "~") {
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
- throw new Error('Unexpected char "' + type + '" at position ' + (pos - 1));
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 parseToken();
137
+ return parseValue();
135
138
  }
136
139
  export {
137
140
  parse,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zustand-querystring",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",