zustand-querystring 0.4.0 → 0.4.1

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/dist/index.js CHANGED
@@ -91,12 +91,28 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
91
91
  ...options
92
92
  };
93
93
  const standalone = !defaultedOptions.key;
94
- const getStateFromUrl = (url) => {
94
+ if (typeof window !== "undefined" && standalone) {
95
+ if (!window.__ZUSTAND_QUERYSTRING_KEYS__) {
96
+ window.__ZUSTAND_QUERYSTRING_KEYS__ = /* @__PURE__ */ new Map();
97
+ }
98
+ }
99
+ const getStateFromUrl = (url, initialState) => {
95
100
  if (standalone) {
96
- const params2 = new URLSearchParams(url.search);
101
+ const params2 = url.search.slice(1).split("&").filter(Boolean);
97
102
  const state = {};
98
- params2.forEach((value, key) => {
99
- state[key] = defaultedOptions.format.parse(value, true);
103
+ const initialKeys = new Set(Object.keys(initialState));
104
+ params2.forEach((param) => {
105
+ const eqIndex = param.indexOf("=");
106
+ if (eqIndex === -1) return;
107
+ const key = decodeURI(param.slice(0, eqIndex));
108
+ const value = param.slice(eqIndex + 1);
109
+ if (!initialKeys.has(key)) return;
110
+ try {
111
+ const parsed = defaultedOptions.format.parse(value, true);
112
+ state[key] = parsed;
113
+ } catch (error) {
114
+ console.error("[getStateFromUrl] error parsing key:", key, error);
115
+ }
100
116
  });
101
117
  return Object.keys(state).length > 0 ? state : null;
102
118
  }
@@ -107,7 +123,8 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
107
123
  const key = param.slice(0, eqIndex);
108
124
  if (key === defaultedOptions.key) {
109
125
  const value = param.slice(eqIndex + 1);
110
- return value ? defaultedOptions.format.parse(value, false) : null;
126
+ const parsed = value ? defaultedOptions.format.parse(value, false) : null;
127
+ return parsed;
111
128
  }
112
129
  }
113
130
  return null;
@@ -122,18 +139,25 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
122
139
  };
123
140
  const initialize = (url, initialState) => {
124
141
  try {
125
- const stateFromURl = getStateFromUrl(url);
142
+ const stateFromURl = getStateFromUrl(url, initialState);
126
143
  if (!stateFromURl) {
127
144
  return initialState;
128
145
  }
146
+ const selected = getSelectedState(stateFromURl, url.pathname);
129
147
  const merged = (0, import_lodash_es.mergeWith)(
130
148
  {},
131
149
  initialState,
132
- getSelectedState(stateFromURl, url.pathname)
150
+ selected,
151
+ (_objValue, srcValue) => {
152
+ if (Array.isArray(srcValue)) {
153
+ return srcValue;
154
+ }
155
+ return void 0;
156
+ }
133
157
  );
134
158
  return merged;
135
159
  } catch (error) {
136
- console.error(error);
160
+ console.error("[initialize] error:", error);
137
161
  return initialState;
138
162
  }
139
163
  };
@@ -146,6 +170,29 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
146
170
  get,
147
171
  api
148
172
  );
173
+ if (standalone) {
174
+ const registry = window.__ZUSTAND_QUERYSTRING_KEYS__;
175
+ const stateKeys = Object.keys(initialState).filter(
176
+ (k) => typeof initialState[k] !== "function"
177
+ );
178
+ const conflicts = [];
179
+ for (const key of stateKeys) {
180
+ if (registry.has(key)) {
181
+ const existing = registry.get(key);
182
+ const current = defaultedOptions.format;
183
+ if (existing !== current) {
184
+ conflicts.push(key);
185
+ }
186
+ } else {
187
+ registry.set(key, defaultedOptions.format);
188
+ }
189
+ }
190
+ if (conflicts.length > 0) {
191
+ throw new Error(
192
+ `[zustand-querystring] Standalone mode conflict: Multiple stores are using the following keys with different formats: ${conflicts.map((k) => `"${k}"`).join(", ")}. This will cause parsing errors. Please use unique state keys or the same format for all stores sharing keys.`
193
+ );
194
+ }
195
+ }
149
196
  const setQuery = () => {
150
197
  const url = new URL(window.location.href);
151
198
  const selectedState = getSelectedState(get(), url.pathname);
package/dist/index.mjs CHANGED
@@ -63,12 +63,28 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
63
63
  ...options
64
64
  };
65
65
  const standalone = !defaultedOptions.key;
66
- const getStateFromUrl = (url) => {
66
+ if (typeof window !== "undefined" && standalone) {
67
+ if (!window.__ZUSTAND_QUERYSTRING_KEYS__) {
68
+ window.__ZUSTAND_QUERYSTRING_KEYS__ = /* @__PURE__ */ new Map();
69
+ }
70
+ }
71
+ const getStateFromUrl = (url, initialState) => {
67
72
  if (standalone) {
68
- const params2 = new URLSearchParams(url.search);
73
+ const params2 = url.search.slice(1).split("&").filter(Boolean);
69
74
  const state = {};
70
- params2.forEach((value, key) => {
71
- state[key] = defaultedOptions.format.parse(value, true);
75
+ const initialKeys = new Set(Object.keys(initialState));
76
+ params2.forEach((param) => {
77
+ const eqIndex = param.indexOf("=");
78
+ if (eqIndex === -1) return;
79
+ const key = decodeURI(param.slice(0, eqIndex));
80
+ const value = param.slice(eqIndex + 1);
81
+ if (!initialKeys.has(key)) return;
82
+ try {
83
+ const parsed = defaultedOptions.format.parse(value, true);
84
+ state[key] = parsed;
85
+ } catch (error) {
86
+ console.error("[getStateFromUrl] error parsing key:", key, error);
87
+ }
72
88
  });
73
89
  return Object.keys(state).length > 0 ? state : null;
74
90
  }
@@ -79,7 +95,8 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
79
95
  const key = param.slice(0, eqIndex);
80
96
  if (key === defaultedOptions.key) {
81
97
  const value = param.slice(eqIndex + 1);
82
- return value ? defaultedOptions.format.parse(value, false) : null;
98
+ const parsed = value ? defaultedOptions.format.parse(value, false) : null;
99
+ return parsed;
83
100
  }
84
101
  }
85
102
  return null;
@@ -94,18 +111,25 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
94
111
  };
95
112
  const initialize = (url, initialState) => {
96
113
  try {
97
- const stateFromURl = getStateFromUrl(url);
114
+ const stateFromURl = getStateFromUrl(url, initialState);
98
115
  if (!stateFromURl) {
99
116
  return initialState;
100
117
  }
118
+ const selected = getSelectedState(stateFromURl, url.pathname);
101
119
  const merged = mergeWith(
102
120
  {},
103
121
  initialState,
104
- getSelectedState(stateFromURl, url.pathname)
122
+ selected,
123
+ (_objValue, srcValue) => {
124
+ if (Array.isArray(srcValue)) {
125
+ return srcValue;
126
+ }
127
+ return void 0;
128
+ }
105
129
  );
106
130
  return merged;
107
131
  } catch (error) {
108
- console.error(error);
132
+ console.error("[initialize] error:", error);
109
133
  return initialState;
110
134
  }
111
135
  };
@@ -118,6 +142,29 @@ var queryStringImpl = (fn, options) => (set, get, api) => {
118
142
  get,
119
143
  api
120
144
  );
145
+ if (standalone) {
146
+ const registry = window.__ZUSTAND_QUERYSTRING_KEYS__;
147
+ const stateKeys = Object.keys(initialState).filter(
148
+ (k) => typeof initialState[k] !== "function"
149
+ );
150
+ const conflicts = [];
151
+ for (const key of stateKeys) {
152
+ if (registry.has(key)) {
153
+ const existing = registry.get(key);
154
+ const current = defaultedOptions.format;
155
+ if (existing !== current) {
156
+ conflicts.push(key);
157
+ }
158
+ } else {
159
+ registry.set(key, defaultedOptions.format);
160
+ }
161
+ }
162
+ if (conflicts.length > 0) {
163
+ throw new Error(
164
+ `[zustand-querystring] Standalone mode conflict: Multiple stores are using the following keys with different formats: ${conflicts.map((k) => `"${k}"`).join(", ")}. This will cause parsing errors. Please use unique state keys or the same format for all stores sharing keys.`
165
+ );
166
+ }
167
+ }
121
168
  const setQuery = () => {
122
169
  const url = new URL(window.location.href);
123
170
  const selectedState = getSelectedState(get(), url.pathname);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zustand-querystring",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",