zustand-querystring 0.0.13 → 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
@@ -7,7 +7,7 @@ Try on [StackBlitz](https://stackblitz.com/github/nitedani/zustand-querystring/t
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 => {
@@ -65,7 +65,6 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
65
65
  return null;
66
66
  };
67
67
  const url = defaultedOptions.url;
68
- const initialState = get() ?? fn(set, get, api);
69
68
  const getSelectedState = (state, pathname) => {
70
69
  if (defaultedOptions.select) {
71
70
  const selection = defaultedOptions.select(pathname);
@@ -75,29 +74,31 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
75
74
  }
76
75
  return state ?? {};
77
76
  };
78
- const initialize = (url, _set = set) => {
79
- const fallback = () => fn(_set, get, api);
77
+ const initialize = (url, initialState) => {
80
78
  try {
81
79
  const queryString = url.search.substring(1);
82
80
  const pathname = url.pathname;
83
81
  if (!queryString) {
84
- return fallback();
82
+ return initialState;
85
83
  }
86
84
  const parsed = parseQueryString(queryString);
87
85
  if (!parsed) {
88
- return fallback();
86
+ return initialState;
89
87
  }
90
- const currentValue = get() ?? fn(_set, get, api);
91
- const merged = mergeWith(currentValue, getSelectedState(parsed, pathname));
88
+ const merged = mergeWith(cloneDeep(initialState), getSelectedState(parsed, pathname));
92
89
  set(merged, true);
93
90
  return merged;
94
91
  }
95
92
  catch (error) {
96
93
  console.error(error);
97
- return fn(_set, get, api);
94
+ return initialState;
98
95
  }
99
96
  };
100
97
  if (typeof window !== 'undefined') {
98
+ const initialState = cloneDeep(fn((...args) => {
99
+ set(...args);
100
+ setQuery();
101
+ }, get, api));
101
102
  const setQuery = () => {
102
103
  const selectedState = getSelectedState(get(), location.pathname);
103
104
  const currentQueryString = location.search;
@@ -159,13 +160,10 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
159
160
  originalSetState(...args);
160
161
  setQuery();
161
162
  };
162
- return initialize(new URL(location.href), (...args) => {
163
- set(...args);
164
- setQuery();
165
- });
163
+ return initialize(new URL(location.href), initialState);
166
164
  }
167
165
  else if (url) {
168
- return initialize(new URL(decodeURIComponent(url), 'http://localhost'));
166
+ return initialize(new URL(decodeURIComponent(url), 'http://localhost'), fn(set, get, api));
169
167
  }
170
168
  return fn(set, get, api);
171
169
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zustand-querystring",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -35,7 +35,7 @@
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
41
  "dev": "tsc -b --watch",