state-in-url 2.2.1 → 2.2.3
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/encodeState/encodeState.d.ts +2 -2
- package/dist/encoder/encoder.d.ts +6 -6
- package/dist/encoder/encoder.mjs +1 -1
- package/dist/next/useUrlState/useUrlState.d.ts +2 -2
- package/dist/useSharedState/useSharedState.d.ts +1 -1
- package/dist/useUrlEncode/useUrlEncode.d.ts +3 -3
- package/dist/useUrlStateBase/useUrlStateBase.d.ts +1 -1
- package/dist/utils.mjs +1 -1
- package/package.json +2 -3
|
@@ -12,7 +12,7 @@ import { type JSONCompatible, type UnknownObj } from '../utils';
|
|
|
12
12
|
* encodeState({ name: 'test' }, form, 'someExistingParam=123');
|
|
13
13
|
* ```
|
|
14
14
|
*
|
|
15
|
-
* *
|
|
15
|
+
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/main/packages/urlstate/encodeState#encodestate}
|
|
16
16
|
*/
|
|
17
17
|
export declare function encodeState<T extends JSONCompatible>(state: T, defaults?: T, paramsToKeep?: string | URLSearchParams): string;
|
|
18
18
|
/**
|
|
@@ -27,6 +27,6 @@ export declare function encodeState<T extends JSONCompatible>(state: T, defaults
|
|
|
27
27
|
* decodeState('key=value&name=Alex', form);
|
|
28
28
|
* ```
|
|
29
29
|
*
|
|
30
|
-
* *
|
|
30
|
+
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/main/packages/urlstate/encodeState#decodestate}
|
|
31
31
|
*/
|
|
32
32
|
export declare function decodeState<T extends JSONCompatible>(uriString: string | URLSearchParams, defaults?: T): undefined extends T ? UnknownObj : T;
|
|
@@ -5,9 +5,11 @@ import { type JSONCompatible } from '../utils';
|
|
|
5
5
|
* @param {unknown} payload any value to serialize
|
|
6
6
|
* @returns {string} URLSearchParams compatible value string
|
|
7
7
|
*
|
|
8
|
-
* *
|
|
8
|
+
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/main/packages/urlstate/encoder#encode}
|
|
9
9
|
*/
|
|
10
10
|
export declare function encode(payload: unknown): string;
|
|
11
|
+
export declare const replacer: (_key: string, value: unknown) => unknown;
|
|
12
|
+
export type Primitive = Exclude<ReturnType<typeof decodePrimitive>, typeof errorSym>;
|
|
11
13
|
/**
|
|
12
14
|
* Decode value part of URLSearchParams back to JS value
|
|
13
15
|
*
|
|
@@ -15,14 +17,12 @@ export declare function encode(payload: unknown): string;
|
|
|
15
17
|
* @param {any} fallback optional fallback value
|
|
16
18
|
* @returns {unknown} decoded object
|
|
17
19
|
*
|
|
18
|
-
* *
|
|
20
|
+
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/main/packages/urlstate/encoder#decode}
|
|
19
21
|
*/
|
|
20
22
|
export declare function decode<T>(payload: string, fallback?: T): JSONCompatible | Primitive;
|
|
21
|
-
export declare const replacer: (key: string, value: unknown) => unknown;
|
|
22
|
-
export type Primitive = Exclude<ReturnType<typeof decodePrimitive>, typeof errorSym>;
|
|
23
23
|
export declare const decodePrimitive: (str: string) => string | number | boolean | Date | typeof errorSym | null | undefined;
|
|
24
24
|
export declare const errorSym: unique symbol;
|
|
25
|
-
export declare const reviver: (
|
|
25
|
+
export declare const reviver: (_key: string, value: unknown) => unknown;
|
|
26
26
|
/**
|
|
27
27
|
* Parses a JSON string into a TypeScript object.
|
|
28
28
|
*
|
|
@@ -30,6 +30,6 @@ export declare const reviver: (key: string, value: unknown) => unknown;
|
|
|
30
30
|
* @param {T} [fallbackValue] - The fallback value to use if parsing fails.
|
|
31
31
|
* @return {T | Primitive | undefined} - The parsed object or a primitive value, or undefined if parsing fails.
|
|
32
32
|
*
|
|
33
|
-
* *
|
|
33
|
+
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url}
|
|
34
34
|
*/
|
|
35
35
|
export declare function parseJSON<T extends JSONCompatible>(jsonString: string, fallbackValue?: T): T | Primitive | undefined;
|
package/dist/encoder/encoder.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{SYMBOLS as e}from"../constants/constants.mjs";import{typeOf as t}from"../utils.mjs";function
|
|
1
|
+
import{SYMBOLS as e}from"../constants/constants.mjs";import{typeOf as t}from"../utils.mjs";function n(n){if(l(n))return String(n);switch(t(n)){case"function":case"symbol":return"";case"date":return s(n);case"string":return`${e.string}${encodeURIComponent(n)}`;case"number":return e.number+String(n);case"boolean":return e.boolean+String(n);case"object":case"array":return JSON.stringify(n,r).replaceAll('"',"'");case"null":return e.null;case"undefined":return e.undefined;default:return String(n)}}const r=(e,r)=>{const o=t(r);let a=structuredClone(r);return"object"!==o&&"array"!==o||Object.keys(a).forEach((e=>{const n=a[e];"date"===t(n)&&(a[e]=s(n))})),"object"!==o&&"array"!==o?n(a):a};function s(t){return e.date+new Date(t).toISOString()}function o(e,t){return i(e.replaceAll("'",'"'),t)}const a=t=>t===e.null?null:t!==e.undefined?t?.startsWith?.(e.number)?Number.parseFloat(t.replace(e.number,"")):t?.startsWith?.(e.boolean)?!!t.includes("true"):t?.startsWith?.(e.date)?new Date(t.slice(1)):t?.startsWith?.(e.string)?decodeURIComponent(t).replace(/^◖/,""):u:void 0,u=Symbol("isError"),c=(e,t)=>{const n="string"==typeof t,r=n&&a(t);return r===u?t:n?r:t};function i(e,t){try{return JSON.parse(e,c)}catch{const n=a(e);return n!==u?n:t}}const l=t=>new RegExp(`^(${e.string}|${e.boolean}|${e.null}|${e.undefined}|${e.number}|${e.date})`).test(String(t));export{o as decode,a as decodePrimitive,n as encode,u as errorSym,i as parseJSON,r as replacer,c as reviver};
|
|
@@ -21,7 +21,7 @@ import { type DeepReadonly, type JSONCompatible } from '../../utils';
|
|
|
21
21
|
* updateUrl(curr => ({ ...curr, name: 'test' }), { replace: true, scroll: true });
|
|
22
22
|
* ```
|
|
23
23
|
*
|
|
24
|
-
* *
|
|
24
|
+
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/main/packages/urlstate/next/useUrlState#api}
|
|
25
25
|
*/
|
|
26
26
|
export declare function useUrlState<T extends JSONCompatible>(defaultState: T, searchParams?: object): {
|
|
27
27
|
state: DeepReadonly<T>;
|
|
@@ -49,7 +49,7 @@ export declare function useUrlState<T extends JSONCompatible>(defaultState: T, s
|
|
|
49
49
|
* updateUrl(curr => ({ ...curr, name: 'test' }), { replace: true, scroll: true });
|
|
50
50
|
* ```
|
|
51
51
|
*
|
|
52
|
-
* *
|
|
52
|
+
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/main/packages/urlstate/next/useUrlState#api}
|
|
53
53
|
*/
|
|
54
54
|
export declare function useUrlState<T extends JSONCompatible>({ defaultState, searchParams, }: {
|
|
55
55
|
defaultState: T;
|
|
@@ -15,7 +15,7 @@ import { type DeepReadonly, type JSONCompatible } from '../utils';
|
|
|
15
15
|
* // OR
|
|
16
16
|
* setState(curr => ({ ...curr, name: 'test' }))
|
|
17
17
|
* ```
|
|
18
|
-
* *
|
|
18
|
+
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/main/packages/urlstate/useSharedState}
|
|
19
19
|
*/
|
|
20
20
|
export declare function useSharedState<T extends JSONCompatible>(defaultState: T, _getInitial?: () => T): {
|
|
21
21
|
state: T;
|
|
@@ -17,7 +17,7 @@ import { type JSONCompatible } from '../utils';
|
|
|
17
17
|
* parse('name=Tom');
|
|
18
18
|
* ```
|
|
19
19
|
*
|
|
20
|
-
* *
|
|
20
|
+
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/main/packages/urlstate/useUrlEncode#api}
|
|
21
21
|
*/
|
|
22
22
|
export declare function useUrlEncode<T extends JSONCompatible>(stateShape: T): {
|
|
23
23
|
/**
|
|
@@ -28,7 +28,7 @@ const state = parse('name=◖Tom');
|
|
|
28
28
|
console.log(state); // Output: { name: 'Tom' }
|
|
29
29
|
* ```
|
|
30
30
|
*
|
|
31
|
-
* *
|
|
31
|
+
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/main/packages/urlstate/useUrlEncode#parse}
|
|
32
32
|
*/
|
|
33
33
|
parse: (strOrSearchParams: string | URLSearchParams) => T;
|
|
34
34
|
/**
|
|
@@ -39,7 +39,7 @@ const queryString = stringify({ name: 'John' }, 'someExistingParamToKeep=123');
|
|
|
39
39
|
console.log(queryString); // Output: name=◖John&someExistingParamToKeep=123
|
|
40
40
|
* ```
|
|
41
41
|
*
|
|
42
|
-
* *
|
|
42
|
+
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/main/packages/urlstate/useUrlEncode#stringify}
|
|
43
43
|
*/
|
|
44
44
|
stringify: (state: typeof stateShape, paramsToKeep?: string | URLSearchParams) => string;
|
|
45
45
|
};
|
|
@@ -13,7 +13,7 @@ import { type DeepReadonly, type JSONCompatible } from '../utils';
|
|
|
13
13
|
* const { state, updateState, updateUrl } = useUrlStateBase(form, router, sp);
|
|
14
14
|
* ```
|
|
15
15
|
*
|
|
16
|
-
* *
|
|
16
|
+
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/integrations/packages/urlstate/useUrlStateBase#api}
|
|
17
17
|
*/
|
|
18
18
|
export declare function useUrlStateBase<T extends JSONCompatible>(defaultState: T, router: Router, searchParams?: object): {
|
|
19
19
|
updateState: (value: Partial<T> | Partial<DeepReadonly<T>> | ((currState: T) => T)) => void;
|
package/dist/utils.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const t=t=>{const
|
|
1
|
+
const t=t=>{const n=typeof t,r=null===t,e=Array.isArray(t),i=t instanceof Date;return(r?"null":i&&"date")||e&&"array"||!r&&!i&&!e&&"object"===n&&"object"||n},n=()=>"undefined"==typeof window,r=t=>new URLSearchParams("string"==typeof t?e(t):t?.toString?.()||""),e=t=>t.split("?")?.[1]||t||"",i=(t,n)=>JSON.stringify(t)===JSON.stringify(n);export{r as getParams,i as isEqual,n as isSSR,t as typeOf};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "state-in-url",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.3",
|
|
4
4
|
"description": "Library to store state in browser URL, includes hook for NextJS, hook for pure React, and low level helpers.",
|
|
5
5
|
"homepage": "https://github.com/asmyshlyaev177/state-in-url#readme",
|
|
6
6
|
"keywords": [
|
|
@@ -197,7 +197,7 @@
|
|
|
197
197
|
"last 5 Firefox versions",
|
|
198
198
|
"last 5 FirefoxAndroid versions",
|
|
199
199
|
"Firefox ESR",
|
|
200
|
-
"last
|
|
200
|
+
"last 2 Safari major versions",
|
|
201
201
|
"last 2 iOS major versions"
|
|
202
202
|
],
|
|
203
203
|
"lint-staged": {
|
|
@@ -258,7 +258,6 @@
|
|
|
258
258
|
"ts-jest": "^29.1.5",
|
|
259
259
|
"ts-node": "^10.9.2",
|
|
260
260
|
"typescript": "^5.3.3",
|
|
261
|
-
"wait-on": "^7.2.0",
|
|
262
261
|
"wireit": "^0.14.4"
|
|
263
262
|
}
|
|
264
263
|
}
|