zustand-querystring 0.0.12 → 0.0.14

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 CHANGED
@@ -1,13 +1,13 @@
1
1
  # zustand-querystring
2
2
 
3
- A zustand middleware that syncs state with the querystring.
3
+ A Zustand middleware that syncs the store with the querystring.
4
4
 
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
  - [React](./examples/react/)
9
9
  - [NextJS](./examples/next/)
10
- - [Rakkas](./example/rakkas/)
10
+ - [Rakkas](./examples/rakkas/)
11
11
 
12
12
  Quickstart:
13
13
  ```ts
package/lib/middleware.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { parse, stringify } from './parser.js';
2
- import { mergeWith, isEqual } from 'lodash-es';
2
+ import { mergeWith, isEqual, cloneDeep } from 'lodash-es';
3
3
  const compact = (newState, initialState) => {
4
4
  const output = {};
5
5
  Object.keys(newState).forEach(key => {
@@ -20,22 +20,26 @@ const compact = (newState, initialState) => {
20
20
  });
21
21
  return output;
22
22
  };
23
- const translateSelectionToState = (selection, state) => Object.keys(selection).reduce((acc, key) => {
24
- // @ts-ignore
25
- if (!(key in state)) {
26
- return acc;
23
+ const translateSelectionToState = (selection, state) => {
24
+ if (typeof state !== 'object' || !state) {
25
+ return {};
27
26
  }
28
- const value = selection[key];
29
- if (typeof value === 'boolean') {
30
- if (value) {
31
- acc[key] = state[key];
27
+ return Object.keys(selection).reduce((acc, key) => {
28
+ if (!(key in state)) {
29
+ return acc;
32
30
  }
33
- }
34
- else {
35
- acc[key] = translateSelectionToState(value, state[key]);
36
- }
37
- return acc;
38
- }, {});
31
+ const value = selection[key];
32
+ if (typeof value === 'boolean') {
33
+ if (value) {
34
+ acc[key] = state[key];
35
+ }
36
+ }
37
+ else {
38
+ acc[key] = translateSelectionToState(value, state[key]);
39
+ }
40
+ return acc;
41
+ }, {});
42
+ };
39
43
  const escapeStringRegexp = string => {
40
44
  if (typeof string !== 'string') {
41
45
  throw new TypeError('Expected a string');
@@ -61,7 +65,6 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
61
65
  return null;
62
66
  };
63
67
  const url = defaultedOptions.url;
64
- const initialState = get() ?? fn(set, get, api);
65
68
  const getSelectedState = (state, pathname) => {
66
69
  if (defaultedOptions.select) {
67
70
  const selection = defaultedOptions.select(pathname);
@@ -71,29 +74,31 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
71
74
  }
72
75
  return state ?? {};
73
76
  };
74
- const initialize = (url, _set = set) => {
75
- const fallback = () => fn(_set, get, api);
77
+ const initialize = (url, initialState) => {
76
78
  try {
77
79
  const queryString = url.search.substring(1);
78
80
  const pathname = url.pathname;
79
81
  if (!queryString) {
80
- return fallback();
82
+ return initialState;
81
83
  }
82
84
  const parsed = parseQueryString(queryString);
83
85
  if (!parsed) {
84
- return fallback();
86
+ return initialState;
85
87
  }
86
- const currentValue = get() ?? fn(_set, get, api);
87
- const merged = mergeWith(currentValue, getSelectedState(parsed, pathname));
88
+ const merged = mergeWith(cloneDeep(initialState), getSelectedState(parsed, pathname));
88
89
  set(merged, true);
89
90
  return merged;
90
91
  }
91
92
  catch (error) {
92
93
  console.error(error);
93
- return fn(_set, get, api);
94
+ return initialState;
94
95
  }
95
96
  };
96
97
  if (typeof window !== 'undefined') {
98
+ const initialState = cloneDeep(fn((...args) => {
99
+ set(...args);
100
+ setQuery();
101
+ }, get, api));
97
102
  const setQuery = () => {
98
103
  const selectedState = getSelectedState(get(), location.pathname);
99
104
  const currentQueryString = location.search;
@@ -155,13 +160,10 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
155
160
  originalSetState(...args);
156
161
  setQuery();
157
162
  };
158
- return initialize(new URL(location.href), (...args) => {
159
- set(...args);
160
- setQuery();
161
- });
163
+ return initialize(new URL(location.href), initialState);
162
164
  }
163
165
  else if (url) {
164
- return initialize(new URL(decodeURIComponent(url), 'http://localhost'));
166
+ return initialize(new URL(decodeURIComponent(url), 'http://localhost'), fn(set, get, api));
165
167
  }
166
168
  return fn(set, get, api);
167
169
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zustand-querystring",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -35,9 +35,10 @@
35
35
  "@types/lodash-es": "^4.17.6",
36
36
  "rimraf": "^3.0.2",
37
37
  "typescript": "^4.9.3",
38
- "zustand": "^4.3.2"
38
+ "zustand": "^4.3.8"
39
39
  },
40
40
  "scripts": {
41
+ "dev": "tsc -b --watch",
41
42
  "build": "rimraf lib && tsc -b"
42
43
  }
43
44
  }