zustand-querystring 0.0.8 → 0.0.10

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,6 +5,7 @@ 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;
8
9
  }
9
10
  type QueryString = <T, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []>(initializer: StateCreator<T, Mps, Mcs>, options?: QueryStringOptions<T>) => StateCreator<T, Mps, Mcs>;
10
11
  export declare const querystring: QueryString;
package/lib/middleware.js CHANGED
@@ -3,10 +3,6 @@ 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
- }
10
6
  if (newState[key] !== null &&
11
7
  newState[key] !== undefined &&
12
8
  typeof newState[key] !== 'function' &&
@@ -40,10 +36,29 @@ const translateSelectionToState = (selection, state) => Object.keys(selection).r
40
36
  }
41
37
  return acc;
42
38
  }, {});
39
+ const escapeStringRegexp = string => {
40
+ if (typeof string !== 'string') {
41
+ throw new TypeError('Expected a string');
42
+ }
43
+ return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
44
+ };
43
45
  const queryStringImpl = (fn, options) => (set, get, api) => {
44
46
  const defaultedOptions = {
47
+ key: '$',
45
48
  ...options,
46
49
  };
50
+ const matcher = new RegExp(`${escapeStringRegexp(defaultedOptions.key)}=(.*);;`);
51
+ const parseQueryString = querystring => {
52
+ const match = querystring.match(matcher);
53
+ if (match) {
54
+ let m = match[1];
55
+ if (!m.startsWith('$')) {
56
+ m = '$' + m;
57
+ }
58
+ return parse(m);
59
+ }
60
+ return null;
61
+ };
47
62
  const url = defaultedOptions.url;
48
63
  const initialState = get() ?? fn(set, get, api);
49
64
  const getSelectedState = (state, pathname) => {
@@ -64,13 +79,7 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
64
79
  if (!queryString) {
65
80
  return fallback();
66
81
  }
67
- if (!queryString) {
68
- return fallback();
69
- }
70
- if (!queryString.startsWith('$')) {
71
- queryString = '$' + queryString;
72
- }
73
- const parsed = parse(queryString);
82
+ const parsed = parseQueryString(queryString);
74
83
  const currentValue = get() ?? fn(_set, get, api);
75
84
  const merged = mergeWith(currentValue, getSelectedState(parsed, pathname));
76
85
  set(merged, true);
@@ -84,23 +93,19 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
84
93
  if (typeof window !== 'undefined') {
85
94
  const setQuery = () => {
86
95
  const selectedState = getSelectedState(get(), window.location.pathname);
87
- if (!selectedState || Object.keys(selectedState).length === 0) {
88
- return;
89
- }
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;
98
- }
99
- });
96
+ const currentQueryString = window.location.search.slice(1);
97
+ const currentParsed = parseQueryString(currentQueryString);
98
+ const newMerged = {
99
+ ...currentParsed,
100
+ ...selectedState,
101
+ };
100
102
  const newCompacted = compact(newMerged, initialState);
101
103
  if (Object.keys(newCompacted).length) {
102
104
  const stringified = stringify(newCompacted).substring(1);
103
- window.history.replaceState(null, '', `?${stringified}`);
105
+ const newQueryString = `${defaultedOptions.key}=${stringified};;`;
106
+ window.history.replaceState(null, '', `?${currentParsed
107
+ ? currentQueryString.replace(matcher, newQueryString)
108
+ : newQueryString}`);
104
109
  }
105
110
  else {
106
111
  window.history.replaceState(null, '', window.location.pathname);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zustand-querystring",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "keywords": [