zustand-querystring 0.0.20-beta.0 → 0.1.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.
@@ -0,0 +1,19 @@
1
+ import {
2
+ stringify
3
+ } from "./chunk-OEB5LU3Z.mjs";
4
+
5
+ // src/utils.ts
6
+ var createURL = ({
7
+ baseUrl,
8
+ key,
9
+ state
10
+ }) => {
11
+ const url = new URL(baseUrl);
12
+ const stringified = stringify(state);
13
+ url.searchParams.set(key, stringified);
14
+ return url.href;
15
+ };
16
+
17
+ export {
18
+ createURL
19
+ };
@@ -0,0 +1,133 @@
1
+ import {
2
+ parse,
3
+ stringify
4
+ } from "./chunk-OEB5LU3Z.mjs";
5
+
6
+ // src/middleware.ts
7
+ import { cloneDeep, isEqual, mergeWith } from "lodash-es";
8
+ var compact = (newState, initialState) => {
9
+ const output = {};
10
+ Object.keys(newState).forEach((key) => {
11
+ if (newState[key] !== null && newState[key] !== void 0 && typeof newState[key] !== "function" && !isEqual(newState[key], initialState[key])) {
12
+ if (typeof newState[key] === "object" && !Array.isArray(newState[key])) {
13
+ const value = compact(newState[key], initialState[key]);
14
+ if (value && Object.keys(value).length > 0) {
15
+ output[key] = value;
16
+ }
17
+ } else {
18
+ output[key] = newState[key];
19
+ }
20
+ }
21
+ });
22
+ return output;
23
+ };
24
+ var translateSelectionToState = (selection, state) => {
25
+ if (typeof state !== "object" || !state) {
26
+ return {};
27
+ }
28
+ return Object.keys(selection).reduce((acc, key) => {
29
+ if (!(key in state)) {
30
+ return acc;
31
+ }
32
+ const value = selection[key];
33
+ if (typeof value === "boolean") {
34
+ if (value) {
35
+ acc[key] = state[key];
36
+ }
37
+ } else {
38
+ acc[key] = translateSelectionToState(value, state[key]);
39
+ }
40
+ return acc;
41
+ }, {});
42
+ };
43
+ var queryStringImpl = (fn, options) => (set, get, api) => {
44
+ const defaultedOptions = {
45
+ key: "state",
46
+ ...options
47
+ };
48
+ const { url } = defaultedOptions;
49
+ const getStateFromUrl = (url2) => {
50
+ const match = url2.searchParams.get(defaultedOptions.key);
51
+ if (match) {
52
+ return parse(match);
53
+ }
54
+ return null;
55
+ };
56
+ const getSelectedState = (state, pathname) => {
57
+ if (defaultedOptions.select) {
58
+ const selection = defaultedOptions.select(pathname);
59
+ const selectedState = translateSelectionToState(selection, state);
60
+ return selectedState;
61
+ }
62
+ return state != null ? state : {};
63
+ };
64
+ const initialize = (url2, initialState) => {
65
+ try {
66
+ const stateFromURl = getStateFromUrl(url2);
67
+ if (!stateFromURl) {
68
+ return initialState;
69
+ }
70
+ const merged = mergeWith(
71
+ cloneDeep(initialState),
72
+ getSelectedState(stateFromURl, url2.pathname)
73
+ );
74
+ set(merged, true);
75
+ return merged;
76
+ } catch (error) {
77
+ console.error(error);
78
+ return initialState;
79
+ }
80
+ };
81
+ if (typeof window !== "undefined") {
82
+ const initialState = cloneDeep(
83
+ fn(
84
+ (...args) => {
85
+ set(...args);
86
+ setQuery();
87
+ },
88
+ get,
89
+ api
90
+ )
91
+ );
92
+ const setQuery = () => {
93
+ const url2 = new URL(window.location.href);
94
+ const selectedState = getSelectedState(get(), url2.pathname);
95
+ const newCompacted = compact(selectedState, initialState);
96
+ const previous = url2.search;
97
+ if (Object.keys(newCompacted).length) {
98
+ url2.searchParams.set(defaultedOptions.key, stringify(newCompacted));
99
+ } else {
100
+ url2.searchParams.delete(defaultedOptions.key);
101
+ }
102
+ if (url2.search !== previous) {
103
+ history.replaceState(history.state, "", url2);
104
+ }
105
+ };
106
+ if (!api.__ZUSTAND_QUERYSTRING_INIT__) {
107
+ api.__ZUSTAND_QUERYSTRING_INIT__ = true;
108
+ let previousPathname = "";
109
+ const cb = () => {
110
+ if (location.pathname !== previousPathname) {
111
+ previousPathname = location.pathname;
112
+ setTimeout(setQuery, 100);
113
+ }
114
+ requestAnimationFrame(cb);
115
+ };
116
+ requestAnimationFrame(cb);
117
+ }
118
+ const originalSetState = api.setState;
119
+ api.setState = (...args) => {
120
+ originalSetState(...args);
121
+ setQuery();
122
+ };
123
+ return initialize(new URL(window.location.href), initialState);
124
+ } else if (url) {
125
+ return initialize(new URL(url, "http://localhost"), fn(set, get, api));
126
+ }
127
+ return fn(set, get, api);
128
+ };
129
+ var querystring = queryStringImpl;
130
+
131
+ export {
132
+ querystring
133
+ };
@@ -0,0 +1,12 @@
1
+ // src/parser.ts
2
+ function stringify(input) {
3
+ return encodeURIComponent(JSON.stringify(input));
4
+ }
5
+ function parse(str) {
6
+ return JSON.parse(decodeURIComponent(str));
7
+ }
8
+
9
+ export {
10
+ stringify,
11
+ parse
12
+ };
package/dist/index.js CHANGED
@@ -26,135 +26,18 @@ __export(src_exports, {
26
26
  });
27
27
  module.exports = __toCommonJS(src_exports);
28
28
 
29
+ // src/middleware.ts
30
+ var import_lodash_es = require("lodash-es");
31
+
29
32
  // src/parser.ts
30
- var keyStringifyRegexp = /([=:@$/])/g;
31
- var valueStringifyRegexp = /([&;/])/g;
32
- var keyParseRegexp = /[=:@$]/;
33
- var valueParseRegexp = /[&;]/;
34
- function encodeString(str, regexp) {
35
- return encodeURI(str.replace(regexp, "/$1"));
36
- }
37
- function trim(res) {
38
- return typeof res === "string" ? res.replace(/;+$/g, "").replace(/^\$/, "") : res;
39
- }
40
- function stringify(input, recursive) {
41
- if (!recursive) {
42
- return trim(stringify(input, true));
43
- }
44
- if (typeof input === "function") {
45
- return;
46
- }
47
- if (typeof input === "number" || input === true || input === false || input === null) {
48
- return ":" + input;
49
- }
50
- const res = [];
51
- if (Array.isArray(input)) {
52
- for (const elem of input) {
53
- typeof elem === "undefined" ? res.push(":null") : res.push(stringify(elem, true));
54
- }
55
- return "@" + res.join("&") + ";";
56
- }
57
- if (input instanceof Date) {
58
- return "=!Date:" + encodeString(input.toISOString(), valueStringifyRegexp);
59
- }
60
- if (typeof input === "object") {
61
- for (const [key, value] of Object.entries(input)) {
62
- const stringifiedValue = stringify(value, true);
63
- if (stringifiedValue) {
64
- res.push(encodeString(key, keyStringifyRegexp) + stringifiedValue);
65
- }
66
- }
67
- return "$" + res.join("&") + ";";
68
- }
69
- if (typeof input === "undefined") {
70
- return;
71
- }
72
- return "=" + encodeString(input.toString(), valueStringifyRegexp);
33
+ function stringify(input) {
34
+ return encodeURIComponent(JSON.stringify(input));
73
35
  }
74
36
  function parse(str) {
75
- if (!str.startsWith("$")) {
76
- str = "$" + str;
77
- }
78
- let pos = 0;
79
- str = decodeURI(str);
80
- function readToken(regexp) {
81
- let token = "";
82
- for (; pos !== str.length; ++pos) {
83
- if (str.charAt(pos) === "/") {
84
- pos += 1;
85
- if (pos === str.length) {
86
- token += ";";
87
- break;
88
- }
89
- } else if (str.charAt(pos).match(regexp)) {
90
- break;
91
- }
92
- token += str.charAt(pos);
93
- }
94
- return token;
95
- }
96
- function parseToken() {
97
- const type = str.charAt(pos++);
98
- if (type === "=") {
99
- const value = readToken(valueParseRegexp);
100
- if (value.startsWith("!Date:")) {
101
- return new Date(value.slice("!Date:".length));
102
- }
103
- return value;
104
- }
105
- if (type === ":") {
106
- const value = readToken(valueParseRegexp);
107
- if (value === "true") {
108
- return true;
109
- }
110
- if (value === "false") {
111
- return false;
112
- }
113
- const parsedValue = parseFloat(value);
114
- return isNaN(parsedValue) ? null : parsedValue;
115
- }
116
- if (type === "@") {
117
- const res = [];
118
- loop: {
119
- if (pos >= str.length || str.charAt(pos) === ";") {
120
- break loop;
121
- }
122
- while (1) {
123
- res.push(parseToken());
124
- if (pos >= str.length || str.charAt(pos) === ";") {
125
- break loop;
126
- }
127
- pos += 1;
128
- }
129
- }
130
- pos += 1;
131
- return res;
132
- }
133
- if (type === "$") {
134
- const res = {};
135
- loop: {
136
- if (pos >= str.length || str.charAt(pos) === ";") {
137
- break loop;
138
- }
139
- while (1) {
140
- var name = readToken(keyParseRegexp);
141
- res[name] = parseToken();
142
- if (pos >= str.length || str.charAt(pos) === ";") {
143
- break loop;
144
- }
145
- pos += 1;
146
- }
147
- }
148
- pos += 1;
149
- return res;
150
- }
151
- throw new Error("Unexpected char " + type);
152
- }
153
- return parseToken();
37
+ return JSON.parse(decodeURIComponent(str));
154
38
  }
155
39
 
156
40
  // src/middleware.ts
157
- var import_lodash_es = require("lodash-es");
158
41
  var compact = (newState, initialState) => {
159
42
  const output = {};
160
43
  Object.keys(newState).forEach((key) => {
@@ -190,32 +73,19 @@ var translateSelectionToState = (selection, state) => {
190
73
  return acc;
191
74
  }, {});
192
75
  };
193
- var escapeStringRegexp = (string) => {
194
- if (typeof string !== "string") {
195
- throw new TypeError("Expected a string");
196
- }
197
- return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
198
- };
199
76
  var queryStringImpl = (fn, options) => (set, get, api) => {
200
77
  const defaultedOptions = {
201
- key: "$",
78
+ key: "state",
202
79
  ...options
203
80
  };
204
- if (encodeURIComponent(defaultedOptions.key) === defaultedOptions.key) {
205
- defaultedOptions.key = `:${defaultedOptions.key}`;
206
- }
207
- const escapedKey = escapeStringRegexp(defaultedOptions.key);
208
- const stateMatcher = new RegExp(`${escapedKey}=(.*);;|${escapedKey}=(.*)$`);
209
- const splitMatcher = new RegExp(`${escapedKey}=.*;;|${escapedKey}=.*$`);
210
- const parseQueryString2 = (querystring2) => {
211
- var _a;
212
- const match = querystring2.match(stateMatcher);
81
+ const { url } = defaultedOptions;
82
+ const getStateFromUrl = (url2) => {
83
+ const match = url2.searchParams.get(defaultedOptions.key);
213
84
  if (match) {
214
- return parse((_a = match[1]) != null ? _a : match[2]);
85
+ return parse(match);
215
86
  }
216
87
  return null;
217
88
  };
218
- let url = defaultedOptions.url;
219
89
  const getSelectedState = (state, pathname) => {
220
90
  if (defaultedOptions.select) {
221
91
  const selection = defaultedOptions.select(pathname);
@@ -226,18 +96,13 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
226
96
  };
227
97
  const initialize = (url2, initialState) => {
228
98
  try {
229
- const queryString = url2.search.substring(1);
230
- const pathname = url2.pathname;
231
- if (!queryString) {
232
- return initialState;
233
- }
234
- const parsed = parseQueryString2(queryString);
235
- if (!parsed) {
99
+ const stateFromURl = getStateFromUrl(url2);
100
+ if (!stateFromURl) {
236
101
  return initialState;
237
102
  }
238
103
  const merged = (0, import_lodash_es.mergeWith)(
239
104
  (0, import_lodash_es.cloneDeep)(initialState),
240
- getSelectedState(parsed, pathname)
105
+ getSelectedState(stateFromURl, url2.pathname)
241
106
  );
242
107
  set(merged, true);
243
108
  return merged;
@@ -247,15 +112,6 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
247
112
  }
248
113
  };
249
114
  if (typeof window !== "undefined") {
250
- const decodeHref = () => {
251
- let href = location.href;
252
- let decoded = decodeURIComponent(href);
253
- if (decoded.indexOf(`?${defaultedOptions.key}=`) !== href.indexOf(`?${defaultedOptions.key}=`)) {
254
- href = decoded;
255
- decoded = decodeURIComponent(href);
256
- }
257
- return href;
258
- };
259
115
  const initialState = (0, import_lodash_es.cloneDeep)(
260
116
  fn(
261
117
  (...args) => {
@@ -267,50 +123,17 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
267
123
  )
268
124
  );
269
125
  const setQuery = () => {
270
- const url2 = new URL(decodeHref());
126
+ const url2 = new URL(window.location.href);
271
127
  const selectedState = getSelectedState(get(), url2.pathname);
272
- const currentQueryString = url2.search;
273
- const currentParsed = parseQueryString2(currentQueryString);
274
- const newMerged = {
275
- ...currentParsed,
276
- ...selectedState
277
- };
278
- const splitIgnored = currentQueryString.split(splitMatcher);
279
- let ignored = "";
280
- for (let str of splitIgnored) {
281
- if (!str || str === "?" || str === "&") {
282
- continue;
283
- }
284
- if (str.startsWith("&") || str.startsWith("?")) {
285
- str = str.substring(1);
286
- }
287
- if (str.endsWith("&")) {
288
- str = str.substring(0, str.length - 1);
289
- }
290
- ignored += (ignored ? "&" : "?") + str;
291
- }
292
- const newCompacted = compact(newMerged, initialState);
293
- let newQueryString = "";
128
+ const newCompacted = compact(selectedState, initialState);
129
+ const previous = url2.search;
294
130
  if (Object.keys(newCompacted).length) {
295
- const stringified = stringify(newCompacted);
296
- const newQueryState = `${defaultedOptions.key}=${stringified};;`;
297
- if (currentParsed) {
298
- newQueryString = currentQueryString.replace(
299
- splitMatcher,
300
- newQueryState
301
- );
302
- } else if (ignored) {
303
- newQueryString = ignored + "&" + newQueryState;
304
- } else {
305
- newQueryString = "?" + newQueryState;
306
- }
131
+ url2.searchParams.set(defaultedOptions.key, stringify(newCompacted));
307
132
  } else {
308
- newQueryString = ignored;
133
+ url2.searchParams.delete(defaultedOptions.key);
309
134
  }
310
- const currentUrl = location.pathname + location.search;
311
- const newUrl = location.pathname + newQueryString;
312
- if (newUrl !== currentUrl) {
313
- history.replaceState(history.state, "", newUrl);
135
+ if (url2.search !== previous) {
136
+ history.replaceState(history.state, "", url2);
314
137
  }
315
138
  };
316
139
  if (!api.__ZUSTAND_QUERYSTRING_INIT__) {
@@ -330,14 +153,8 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
330
153
  originalSetState(...args);
331
154
  setQuery();
332
155
  };
333
- return initialize(new URL(decodeHref()), initialState);
156
+ return initialize(new URL(window.location.href), initialState);
334
157
  } else if (url) {
335
- url = decodeURIComponent(url);
336
- const idx = url.indexOf(`?${defaultedOptions.key}=`);
337
- const decoded = decodeURIComponent(url);
338
- if (decoded.indexOf(`?${defaultedOptions.key}=`) !== idx) {
339
- url = decoded;
340
- }
341
158
  return initialize(new URL(url, "http://localhost"), fn(set, get, api));
342
159
  }
343
160
  return fn(set, get, api);
@@ -345,59 +162,15 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
345
162
  var querystring = queryStringImpl;
346
163
 
347
164
  // src/utils.ts
348
- var escapeStringRegexp2 = (string) => {
349
- if (typeof string !== "string") {
350
- throw new TypeError("Expected a string");
351
- }
352
- return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
353
- };
354
- var parseQueryString = (key, querystring2) => {
355
- var _a;
356
- const stateMatcher = new RegExp(`${key}=(.*);;|${key}=(.*)$`);
357
- const match = querystring2.match(stateMatcher);
358
- if (match) {
359
- return parse((_a = match[1]) != null ? _a : match[2]);
360
- }
361
- return null;
362
- };
363
165
  var createURL = ({
364
166
  baseUrl,
365
167
  key,
366
168
  state
367
169
  }) => {
368
- const escapedKey = escapeStringRegexp2(key);
170
+ const url = new URL(baseUrl);
369
171
  const stringified = stringify(state);
370
- const newQueryState = `${key}=${stringified};;`;
371
- const match = baseUrl.indexOf("?");
372
- const currentQueryString = match >= 0 ? baseUrl.substring(match) : "";
373
- const currentParsed = parseQueryString(escapedKey, currentQueryString);
374
- const splitMatcher = new RegExp(`${escapedKey}=.*;;|${escapedKey}=.*$`);
375
- const splitIgnored = currentQueryString.split(splitMatcher);
376
- let ignored = "";
377
- for (let str of splitIgnored) {
378
- if (!str || str === "?" || str === "&") {
379
- continue;
380
- }
381
- if (str.startsWith("&") || str.startsWith("?")) {
382
- str = str.substring(1);
383
- }
384
- if (str.endsWith("&")) {
385
- str = str.substring(0, str.length - 1);
386
- }
387
- ignored += (ignored ? "&" : "?") + str;
388
- }
389
- let newQueryString = "";
390
- if (currentParsed) {
391
- newQueryString = currentQueryString.replace(splitMatcher, newQueryState);
392
- } else if (ignored) {
393
- newQueryString = ignored + "&" + newQueryState;
394
- } else {
395
- newQueryString = "?" + newQueryState;
396
- }
397
- if (currentQueryString) {
398
- return baseUrl.replace(currentQueryString, newQueryString);
399
- }
400
- return baseUrl + newQueryString;
172
+ url.searchParams.set(key, stringified);
173
+ return url.href;
401
174
  };
402
175
  // Annotate the CommonJS export names for ESM import in node:
403
176
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  querystring
3
- } from "./chunk-C7RA6OH5.mjs";
3
+ } from "./chunk-HU2467IM.mjs";
4
4
  import {
5
5
  createURL
6
- } from "./chunk-PYA77ZUK.mjs";
6
+ } from "./chunk-HC4H2XL3.mjs";
7
7
  import {
8
8
  parse,
9
9
  stringify
10
- } from "./chunk-ZM24AXRE.mjs";
10
+ } from "./chunk-OEB5LU3Z.mjs";
11
11
  export {
12
12
  createURL,
13
13
  parse,