zustand-querystring 0.4.0 → 0.5.0

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.
@@ -1,212 +0,0 @@
1
- // src/format/readable.ts
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 = "D";
10
- var esc = (c) => /[.$^*+?()[\]{}|\\]/.test(c) ? `\\${c}` : c;
11
- var KEY_STOP = new RegExp(
12
- `[${TYPE_STRING}${TYPE_PRIMITIVE}${TYPE_ARRAY}${esc(TYPE_OBJECT)}${esc(SEPARATOR)}]`
13
- );
14
- var VALUE_STOP = new RegExp(`[${esc(SEPARATOR)}${TERMINATOR}]`);
15
- var KEY_ESCAPE = new RegExp(
16
- `([${TYPE_STRING}${TYPE_PRIMITIVE}${TYPE_ARRAY}${esc(TYPE_OBJECT)}${ESCAPE}${esc(SEPARATOR)}])`,
17
- "g"
18
- );
19
- var VALUE_ESCAPE = new RegExp(
20
- `([${esc(SEPARATOR)}${TERMINATOR}${ESCAPE}])`,
21
- "g"
22
- );
23
- function escapeStr(str, pattern) {
24
- return encodeURI(str.replace(pattern, `${ESCAPE}$1`));
25
- }
26
- function cleanResult(str, standalone) {
27
- while (str.endsWith(TERMINATOR)) str = str.slice(0, -1);
28
- const datePattern = new RegExp(`^${esc(TYPE_STRING)}${DATE_PREFIX}-?\\d+$`);
29
- if (standalone && datePattern.test(str)) {
30
- return str.slice(1);
31
- }
32
- if (!standalone && str.startsWith(TYPE_OBJECT)) {
33
- return str.slice(1);
34
- }
35
- return str;
36
- }
37
- function stringify(value, standalone = false) {
38
- return cleanResult(serialize(value, standalone), standalone);
39
- }
40
- function serialize(value, standalone, inArray = false) {
41
- if (value === null) return `${TYPE_PRIMITIVE}null`;
42
- if (value === void 0) return `${TYPE_PRIMITIVE}undefined`;
43
- if (typeof value === "function") return "";
44
- if (typeof value === "number") {
45
- return `${TYPE_PRIMITIVE}${String(value).replace(/\./g, `${ESCAPE}.`)}`;
46
- }
47
- if (typeof value === "boolean") {
48
- return `${TYPE_PRIMITIVE}${value}`;
49
- }
50
- if (value instanceof Date) {
51
- return `${TYPE_STRING}${DATE_PREFIX}${value.getTime()}`;
52
- }
53
- if (Array.isArray(value)) {
54
- const items = value.map((v) => serialize(v, standalone, true));
55
- return `${TYPE_ARRAY}${items.join(SEPARATOR)}${TERMINATOR}`;
56
- }
57
- if (typeof value === "object") {
58
- const entries = [];
59
- for (const [k, v] of Object.entries(value)) {
60
- const val = serialize(v, false, false);
61
- if (val || v === void 0) {
62
- entries.push(`${escapeStr(k, KEY_ESCAPE)}${val}`);
63
- }
64
- }
65
- return `${TYPE_OBJECT}${entries.join(SEPARATOR)}${TERMINATOR}`;
66
- }
67
- const strVal = String(value);
68
- let escaped = escapeStr(strVal, VALUE_ESCAPE);
69
- const datePattern = new RegExp(`^${DATE_PREFIX}-?\\d`);
70
- if (datePattern.test(strVal)) {
71
- escaped = ESCAPE + escaped;
72
- }
73
- return standalone || inArray ? escaped : `${TYPE_STRING}${escaped}`;
74
- }
75
- function parse(input, standalone = false) {
76
- const str = decodeURI(input);
77
- if (str.length === 0) {
78
- return standalone ? "" : {};
79
- }
80
- const first = str[0];
81
- const hasMarker = first === TYPE_STRING || first === TYPE_PRIMITIVE || first === TYPE_ARRAY || first === TYPE_OBJECT;
82
- let source;
83
- if (hasMarker) {
84
- source = str;
85
- } else if (standalone) {
86
- const datePattern = new RegExp(`^${DATE_PREFIX}-?\\d+$`);
87
- if (datePattern.test(str)) {
88
- const timestamp = parseInt(str.slice(DATE_PREFIX.length), 10);
89
- if (!isNaN(timestamp)) {
90
- return new Date(timestamp);
91
- }
92
- }
93
- return str;
94
- } else {
95
- const markers = `${TYPE_STRING}${TYPE_PRIMITIVE}${TYPE_ARRAY}${TYPE_OBJECT}`;
96
- const objectPattern = new RegExp(
97
- `[^${ESCAPE}${markers}${SEPARATOR}${TERMINATOR}][${markers}]`
98
- );
99
- source = objectPattern.test(str) ? `${TYPE_OBJECT}${str}` : `${TYPE_STRING}${str}`;
100
- }
101
- let pos = 0;
102
- function peek() {
103
- return source[pos] || "";
104
- }
105
- function advance() {
106
- pos++;
107
- }
108
- function readUntil(stopPattern, checkEscape = false) {
109
- let result = "";
110
- let wasEscaped = false;
111
- while (pos < source.length) {
112
- const ch = peek();
113
- if (ch === ESCAPE) {
114
- if (checkEscape && pos + 1 < source.length && source[pos + 1] === DATE_PREFIX) {
115
- wasEscaped = true;
116
- }
117
- advance();
118
- if (pos < source.length) {
119
- result += peek();
120
- advance();
121
- }
122
- continue;
123
- }
124
- if (stopPattern.test(ch)) break;
125
- result += ch;
126
- advance();
127
- }
128
- return { value: result, wasEscaped };
129
- }
130
- function parseString() {
131
- const { value, wasEscaped } = readUntil(VALUE_STOP, true);
132
- const datePattern = new RegExp(`^${DATE_PREFIX}-?\\d+$`);
133
- if (!wasEscaped && datePattern.test(value)) {
134
- const timestamp = parseInt(value.slice(DATE_PREFIX.length), 10);
135
- if (!isNaN(timestamp)) {
136
- return new Date(timestamp);
137
- }
138
- }
139
- return value;
140
- }
141
- function parsePrimitive() {
142
- const { value } = readUntil(VALUE_STOP, false);
143
- if (value === "null") return null;
144
- if (value === "undefined") return void 0;
145
- if (value === "true") return true;
146
- if (value === "false") return false;
147
- return parseFloat(value);
148
- }
149
- function parseArray() {
150
- const result = [];
151
- let lastWasSeparator = false;
152
- while (pos < source.length && peek() !== TERMINATOR) {
153
- if (peek() === SEPARATOR) {
154
- result.push("");
155
- advance();
156
- lastWasSeparator = true;
157
- continue;
158
- }
159
- lastWasSeparator = false;
160
- const ch = peek();
161
- if (ch === TYPE_PRIMITIVE || ch === TYPE_ARRAY || ch === TYPE_OBJECT || ch === TYPE_STRING) {
162
- result.push(parseValue());
163
- } else {
164
- result.push(parseString());
165
- }
166
- if (pos < source.length && peek() === SEPARATOR) {
167
- advance();
168
- lastWasSeparator = true;
169
- }
170
- }
171
- if (lastWasSeparator && (peek() === TERMINATOR || pos >= source.length)) {
172
- result.push("");
173
- }
174
- if (peek() === TERMINATOR) advance();
175
- return result;
176
- }
177
- function parseObject() {
178
- const result = {};
179
- while (pos < source.length && peek() !== TERMINATOR) {
180
- const { value: key } = readUntil(KEY_STOP, false);
181
- result[key] = parseValue();
182
- if (pos < source.length && peek() !== TERMINATOR && peek() === SEPARATOR) {
183
- advance();
184
- }
185
- }
186
- if (peek() === TERMINATOR) advance();
187
- return result;
188
- }
189
- function parseValue() {
190
- const type = peek();
191
- advance();
192
- switch (type) {
193
- case TYPE_PRIMITIVE:
194
- return parsePrimitive();
195
- case TYPE_ARRAY:
196
- return parseArray();
197
- case TYPE_OBJECT:
198
- return parseObject();
199
- case TYPE_STRING:
200
- return parseString();
201
- default:
202
- throw new Error(`Unexpected type "${type}" at position ${pos}`);
203
- }
204
- }
205
- return parseValue();
206
- }
207
- var readable = { stringify, parse };
208
- export {
209
- parse,
210
- readable,
211
- stringify
212
- };