zustand-querystring 0.0.7 → 0.0.8

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.
@@ -5,7 +5,6 @@ type DeepSelect<T> = T extends object ? {
5
5
  export interface QueryStringOptions<T> {
6
6
  url?: string;
7
7
  select?: (pathname: string) => DeepSelect<T>;
8
- key?: string;
9
8
  }
10
9
  type QueryString = <T, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []>(initializer: StateCreator<T, Mps, Mcs>, options?: QueryStringOptions<T>) => StateCreator<T, Mps, Mcs>;
11
10
  export declare const querystring: QueryString;
package/lib/middleware.js CHANGED
@@ -3,6 +3,10 @@ import { mergeWith, isEqual } from 'lodash-es';
3
3
  const compact = (newState, initialState) => {
4
4
  const output = {};
5
5
  Object.keys(newState).forEach(key => {
6
+ if (!(key in initialState)) {
7
+ output[key] = newState[key];
8
+ return;
9
+ }
6
10
  if (newState[key] !== null &&
7
11
  newState[key] !== undefined &&
8
12
  typeof newState[key] !== 'function' &&
@@ -18,9 +22,6 @@ const compact = (newState, initialState) => {
18
22
  }
19
23
  }
20
24
  });
21
- if (Object.keys(output).length === 0) {
22
- return null;
23
- }
24
25
  return output;
25
26
  };
26
27
  const translateSelectionToState = (selection, state) => Object.keys(selection).reduce((acc, key) => {
@@ -41,8 +42,6 @@ const translateSelectionToState = (selection, state) => Object.keys(selection).r
41
42
  }, {});
42
43
  const queryStringImpl = (fn, options) => (set, get, api) => {
43
44
  const defaultedOptions = {
44
- partialize: state => state,
45
- key: '$',
46
45
  ...options,
47
46
  };
48
47
  const url = defaultedOptions.url;
@@ -54,27 +53,24 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
54
53
  const selectedState = translateSelectionToState(selection, state);
55
54
  return selectedState;
56
55
  }
57
- return state;
56
+ return state ?? {};
58
57
  };
59
58
  const initialize = (url, _set = set) => {
60
59
  const fallback = () => fn(_set, get, api);
61
60
  try {
62
61
  const splitUrl = url.split('?');
63
- const queryString = splitUrl[1];
62
+ let queryString = splitUrl[1];
64
63
  const pathname = splitUrl[0];
65
64
  if (!queryString) {
66
65
  return fallback();
67
66
  }
68
- const idx = queryString.indexOf(defaultedOptions.key + '=');
69
- if (idx === -1) {
67
+ if (!queryString) {
70
68
  return fallback();
71
69
  }
72
- const toParse = queryString.substring(idx + 2);
73
- if (!toParse) {
74
- return fallback();
70
+ if (!queryString.startsWith('$')) {
71
+ queryString = '$' + queryString;
75
72
  }
76
- // console.log('toParse', toParse);
77
- const parsed = parse(toParse);
73
+ const parsed = parse(queryString);
78
74
  const currentValue = get() ?? fn(_set, get, api);
79
75
  const merged = mergeWith(currentValue, getSelectedState(parsed, pathname));
80
76
  set(merged, true);
@@ -82,44 +78,32 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
82
78
  }
83
79
  catch (error) {
84
80
  console.error(error);
85
- if (typeof window !== 'undefined') {
86
- window.history.replaceState(null, '', window.location.pathname);
87
- }
88
81
  return fn(_set, get, api);
89
82
  }
90
83
  };
91
84
  if (typeof window !== 'undefined') {
92
85
  const setQuery = () => {
93
86
  const selectedState = getSelectedState(get(), window.location.pathname);
94
- if (!selectedState) {
87
+ if (!selectedState || Object.keys(selectedState).length === 0) {
95
88
  return;
96
89
  }
97
- const compactedSelectedState = compact(selectedState, initialState);
98
- const stringified = compactedSelectedState && stringify(compactedSelectedState);
99
- const currentQueryString = window.location.search.slice(1);
100
- if (stringified || currentQueryString) {
101
- // console.log('set query', stringified);
102
- // console.log('parse query', parse(stringified));
103
- // parse current querystring
104
- // split query string to key-value
105
- const variables = {};
106
- const currentQuery = currentQueryString.split('&');
107
- for (const variable of currentQuery) {
108
- if (stringified?.includes(variable)) {
109
- continue;
110
- }
111
- const [key, value] = variable.split('=');
112
- variables[key] = value;
90
+ let currentQueryString = window.location.search.slice(1);
91
+ if (!currentQueryString.startsWith('$')) {
92
+ currentQueryString = '$' + currentQueryString;
93
+ }
94
+ const currentParsed = parse(currentQueryString);
95
+ const newMerged = mergeWith(currentParsed, selectedState, (objValue, srcValue) => {
96
+ if (Array.isArray(objValue)) {
97
+ return srcValue;
113
98
  }
114
- variables[defaultedOptions.key] = stringified;
115
- const newQueryString = Object.keys(variables) // filter out empty values
116
- .filter(key => variables[key])
117
- // join key-value pairs
118
- .map(key => `${key}=${variables[key]}`)
119
- // join all pairs with &
120
- .join('&');
121
- // console.log('newQueryString', newQueryString);
122
- window.history.replaceState(null, '', newQueryString ? `?${newQueryString}` : window.location.pathname);
99
+ });
100
+ const newCompacted = compact(newMerged, initialState);
101
+ if (Object.keys(newCompacted).length) {
102
+ const stringified = stringify(newCompacted).substring(1);
103
+ window.history.replaceState(null, '', `?${stringified}`);
104
+ }
105
+ else {
106
+ window.history.replaceState(null, '', window.location.pathname);
123
107
  }
124
108
  };
125
109
  //TODO: find a better way to do this
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zustand-querystring",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "keywords": [