@koine/browser 2.0.0-beta.7 → 2.0.0-beta.71
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 +1 -1
- package/createStorage.d.ts +1 -44
- package/getZonedDate.d.ts +1 -16
- package/gtag.d.ts +1 -0
- package/gtagPageview.d.ts +1 -5
- package/index.cjs.d.ts +1 -0
- package/index.cjs.default.js +1 -0
- package/index.cjs.js +61 -0
- package/index.cjs.mjs +2 -0
- package/index.d.ts +3 -0
- package/index.esm.js +40 -0
- package/isIE.d.ts +1 -7
- package/isMobile.d.ts +1 -7
- package/listenUrlSearch.d.ts +3 -0
- package/listenUrlSearchParams.d.ts +1 -0
- package/navigateToHash.d.ts +1 -8
- package/navigateToHashParams.d.ts +2 -9
- package/navigateToMergedHashParams.d.ts +2 -8
- package/navigateToMergedParams.d.ts +2 -9
- package/navigateToParams.d.ts +2 -10
- package/navigateToUrl.d.ts +1 -7
- package/navigateWithoutUrlParam.d.ts +1 -8
- package/package.json +19 -8
- package/redirectTo.d.ts +2 -9
- package/storage.d.ts +1 -5
- package/storageClient.d.ts +1 -5
- package/createStorage.js +0 -146
- package/createStorage.mjs +0 -130
- package/getZonedDate.js +0 -37
- package/getZonedDate.mjs +0 -31
- package/gtagPageview.js +0 -41
- package/gtagPageview.mjs +0 -23
- package/index.js +0 -72
- package/index.mjs +0 -15
- package/isIE.js +0 -31
- package/isIE.mjs +0 -16
- package/isMobile.js +0 -29
- package/isMobile.mjs +0 -14
- package/navigateToHash.js +0 -25
- package/navigateToHash.mjs +0 -11
- package/navigateToHashParams.js +0 -33
- package/navigateToHashParams.mjs +0 -19
- package/navigateToMergedHashParams.js +0 -26
- package/navigateToMergedHashParams.mjs +0 -11
- package/navigateToMergedParams.js +0 -26
- package/navigateToMergedParams.mjs +0 -12
- package/navigateToParams.js +0 -30
- package/navigateToParams.mjs +0 -17
- package/navigateToUrl.js +0 -28
- package/navigateToUrl.mjs +0 -10
- package/navigateWithoutUrlParam.js +0 -32
- package/navigateWithoutUrlParam.mjs +0 -18
- package/redirectTo.js +0 -28
- package/redirectTo.mjs +0 -14
- package/storage.js +0 -25
- package/storage.mjs +0 -8
- package/storageClient.js +0 -112
- package/storageClient.mjs +0 -95
- package/typings.d.ts +0 -12
package/README.md
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
# @koine/
|
|
1
|
+
# @koine/i18n
|
package/createStorage.d.ts
CHANGED
|
@@ -1,54 +1,11 @@
|
|
|
1
1
|
export type CreateStorageConfig = Record<string, any>;
|
|
2
|
-
|
|
3
|
-
* Utility to create a storage instance to interact with `localStorage` using
|
|
4
|
-
* encrypted (encoded) key/values.
|
|
5
|
-
*/
|
|
6
|
-
export declare const createStorage: <T extends CreateStorageConfig>(config: Partial<T>, useSessionStorage?: boolean) => {
|
|
7
|
-
/**
|
|
8
|
-
* Get all storage value (it uses `localStorage.get()`).
|
|
9
|
-
*
|
|
10
|
-
* Unparseable values with `JSON.parse()` return their value as it is.
|
|
11
|
-
* On ssr or if the given `key` argument is not found `defaultValue` is
|
|
12
|
-
* returned, otherwise `null`.
|
|
13
|
-
*/
|
|
2
|
+
export declare let createStorage: <T extends CreateStorageConfig>(config: Partial<T>, useSessionStorage?: boolean) => {
|
|
14
3
|
get<TKey extends Extract<keyof T, string>>(key: TKey, defaultValue?: T[TKey] | null | undefined): T[TKey] | null;
|
|
15
|
-
/**
|
|
16
|
-
* Get all storage values (it uses `localStorage.get()`).
|
|
17
|
-
*
|
|
18
|
-
* `undefined` and `null` values are not returned.
|
|
19
|
-
*/
|
|
20
4
|
getAll(defaultValues?: Partial<T> | undefined): T;
|
|
21
|
-
/**
|
|
22
|
-
* Set a storage value (it uses `localStorage.set()`).
|
|
23
|
-
*
|
|
24
|
-
* Non-string values are stringified with `JSON.stringify()`
|
|
25
|
-
*/
|
|
26
5
|
set<TKey_1 extends Extract<keyof T, string>>(key: TKey_1, value?: T[TKey_1] | undefined): void;
|
|
27
|
-
/**
|
|
28
|
-
* Set all given storage values (it uses `localStorage.set()`).
|
|
29
|
-
*
|
|
30
|
-
* Non-string values are stringified with `JSON.stringify()`, `undefined`
|
|
31
|
-
* and `null` values are removed from the storage
|
|
32
|
-
*/
|
|
33
6
|
setMany(newValues: Partial<T>): void;
|
|
34
|
-
/**
|
|
35
|
-
* Check if a storage value is _truthy_ (it uses `localStorage.get()`).
|
|
36
|
-
*/
|
|
37
7
|
has<TKey_2 extends Extract<keyof T, string>>(key: TKey_2): any;
|
|
38
|
-
/**
|
|
39
|
-
* Remove a storage value (it uses `localStorage.remove()`).
|
|
40
|
-
*/
|
|
41
8
|
remove<TKey_3 extends Extract<keyof T, string>>(key: TKey_3): void;
|
|
42
|
-
/**
|
|
43
|
-
* Clear all storage values (it uses `localStorage.remove()`).
|
|
44
|
-
*/
|
|
45
9
|
clear(): void;
|
|
46
|
-
/**
|
|
47
|
-
* Watch a storage value changes, this needs to be executed only in browser
|
|
48
|
-
* context (it uses `window.addEventListener("storage")`).
|
|
49
|
-
*
|
|
50
|
-
* Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
|
|
51
|
-
*/
|
|
52
10
|
watch: <TKey_4 extends keyof T>(keyToWatch: TKey_4, onRemoved?: () => void, onAdded?: () => void) => () => void | undefined;
|
|
53
11
|
};
|
|
54
|
-
export default createStorage;
|
package/getZonedDate.d.ts
CHANGED
|
@@ -1,16 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* It returns a `Date` object from a date `string` adjusted on the user timeZone,
|
|
3
|
-
* if a timeZone is not provided we try getting it from the `Intl` browwser native
|
|
4
|
-
* API. It gracefully falls back returning a _non-timezone-based_ `Date`.
|
|
5
|
-
*
|
|
6
|
-
* @category date
|
|
7
|
-
*
|
|
8
|
-
* @resources
|
|
9
|
-
* - to get the timeZone client side see [this article](https://attacomsian.com/blog/javascript-current-timezone)
|
|
10
|
-
* - for converting the date based on the time zone [date-fns docs](https://date-fns.org/v2.27.0/docs/Time-Zones) and [date-fns-tz docs](https://github.com/marnusw/date-fns-tz)
|
|
11
|
-
*
|
|
12
|
-
* @param dateString A parseable date as string, `Z` is automatically suffixed if not present to correctly get time zone based time from a UTC date.
|
|
13
|
-
* @param timeZone Optionally pass a timeZone (e.g. from user preference or from the server), it falls back trying to read it from the `Intl` browwser native API.
|
|
14
|
-
*/
|
|
15
|
-
export declare function getZonedDate(dateString?: string, timeZone?: string): Date;
|
|
16
|
-
export default getZonedDate;
|
|
1
|
+
export declare let getZonedDate: (dateString?: string, timeZone?: string) => Date;
|
package/gtag.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare let gtag: <T extends unknown[]>(...args: T) => void;
|
package/gtagPageview.d.ts
CHANGED
|
@@ -3,8 +3,4 @@ export type GtmPageviewArgs = [
|
|
|
3
3
|
page_title?: string,
|
|
4
4
|
page_location?: string
|
|
5
5
|
];
|
|
6
|
-
|
|
7
|
-
* @category analytics-google
|
|
8
|
-
*/
|
|
9
|
-
export declare const gtagPageview: (page_path?: string | undefined, page_title?: string | undefined, page_location?: string | undefined) => void;
|
|
10
|
-
export default gtagPageview;
|
|
6
|
+
export declare let gtagPageview: (page_path?: string | undefined, page_title?: string | undefined, page_location?: string | undefined) => void;
|
package/index.cjs.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./index";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./index.cjs.js').default;
|
package/index.cjs.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var utils = require('@koine/utils');
|
|
6
|
+
var dom = require('@koine/dom');
|
|
7
|
+
|
|
8
|
+
let t$1={g:"getItem",s:"setItem",r:"removeItem"};let storageClient=s=>{let r=(o,r,l)=>utils.isBrowser?window[s?"sessionStorage":"localStorage"][t$1[o]](r,l):()=>{"development"===process.env.NODE_ENV&&console.warn(`[@koine/utils:storageClient]: ${s?"sessionStorage":"localStorage"} does not exists outside of browser.`);};return {get:(o,t,s)=>{let l=s??null;if("development"!==process.env.NODE_ENV||utils.isBrowser||console.log(`[@koine/utils:storage] called 'get' outside of browser with default value '${JSON.stringify(s)}'.`),utils.isBrowser){let e=r("g",o);if(e){e=t?t(e):e;try{let o=JSON.parse(e);o&&(l=o);}catch(o){l=e;}}}return l},set:(t,s,l=e=>e)=>{if("development"!==process.env.NODE_ENV||utils.isBrowser||console.log("[@koine/utils:storage] called 'set' outside of browser does not work."),utils.isBrowser)try{let e=l(utils.isString(s)?s:JSON.stringify(s));r("s",t,e);}catch(e){"development"===process.env.NODE_ENV&&console.warn("[@koine/utils:createStorage]: 'set' error.",e);}},remove:o=>{if("development"!==process.env.NODE_ENV||utils.isBrowser||console.log("[@koine/utils:storage] called 'remove' outside of browser does not work."),utils.isBrowser)try{r("r",o);}catch(e){"development"===process.env.NODE_ENV&&console.warn("[@koine/utils:createStorage]: 'remove' error.",e);}},has:(o,t)=>{let s=t??!1;return "development"!==process.env.NODE_ENV||utils.isBrowser||console.log(`[@koine/utils:storage] called 'has' outside of browser with default value '${JSON.stringify(t)}'.`),utils.isBrowser&&(s=r("g",o)??!1),s}}};
|
|
9
|
+
|
|
10
|
+
let storage={l:storageClient(),s:storageClient(!0)};
|
|
11
|
+
|
|
12
|
+
let createStorage=(n,a)=>{let c=a?storage.s:storage.l,m=Object.keys(n).reduce((e,o)=>({...e,[o]:utils.encode(o)}),{});return {get:(t,o)=>c.get(m[t],utils.decode,o),getAll(e){if(!utils.isBrowser)return "development"===process.env.NODE_ENV&&console.log("[@koine/utils:createStorage] attempt to use 'getAll' outside of browser."),{};let t={};for(let o in m){let s=this.get(o),l=e?.[o];utils.isNullOrUndefined(s)?l&&(t[o]=l):t[o]=s;}return t},set(e,o){c.set(m[e],o,utils.encode);},setMany(e){if("development"!==process.env.NODE_ENV||utils.isBrowser||console.log("[@koine/utils:createStorage] attempt to use 'setMany' outside of browser."),utils.isBrowser)for(let t in e){let o=e[t];utils.isNullOrUndefined(o)?this.remove(t):this.set(t,o);}},has:e=>c.has(m[e]),remove(e){c.remove(m[e]);},clear(){if("development"!==process.env.NODE_ENV||utils.isBrowser||console.log("[@koine/utils:createStorage] attempt to use 'clear' outside of browser."),utils.isBrowser)for(let e in m)c.remove(m[e]);},watch:(e,t,r)=>utils.isBrowser?dom.on(window,"storage",o=>{let{key:s,oldValue:l,newValue:i}=o;s===m[e]&&(l&&!i?t?.():!l&&i&&r?.());}):("development"===process.env.NODE_ENV&&console.log("[@koine/utils:createStorage] attempt to use 'watch' outside of browser."),utils.noop)}};
|
|
13
|
+
|
|
14
|
+
let t=(e,t)=>e;let getZonedDate=(o="",n)=>{if(o.endsWith("Z")||(o+="Z"),!n&&utils.isBrowser)try{n=Intl.DateTimeFormat().resolvedOptions().timeZone;}catch(e){"development"===process.env.NODE_ENV&&console.warn("[@koine/browser:getZonedDate] failed reading timeZone, error",e);}return n?t(new Date(o)):new Date(o)};
|
|
15
|
+
|
|
16
|
+
let gtag=(...g)=>{utils.isUndefined(window)||utils.isUndefined(window.gtag)?utils.noop():window.gtag(...g);};
|
|
17
|
+
|
|
18
|
+
let gtagPageview=(...t)=>gtag("event","page_view",{page_path:t[0]||location.pathname,page_title:t[1]||document.title,page_location:t[2]||location.href});
|
|
19
|
+
|
|
20
|
+
let isIE=(i=!0)=>{if(utils.isServer)return i;let r=window.navigator.userAgent;return !!(r.indexOf("MSIE ")>0||r.indexOf("Trident/")>0)};
|
|
21
|
+
|
|
22
|
+
let isMobile=(e=!0)=>utils.isServer?e:/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(window.navigator.userAgent);
|
|
23
|
+
|
|
24
|
+
let o=(t,e,o)=>function(...r){if(o)return e(),t.apply(this,r);let i=t.apply(this,r);return e(),i},r=utils.isBrowser?location.search:"",i=()=>{let t=location.search;if(t!==r)for(let e of history.__.h.values())e(r,t);r=t;};let listenUrlSearch=t=>(history.__||(history.pushState=o(history.pushState,i),history.replaceState=o(history.replaceState,i),dom.on(window,"popstate",i),history.__={h:new Set}),history.__.h.has(t)||history.__.h.add(t),()=>{history.__.h.delete(t);});
|
|
25
|
+
|
|
26
|
+
let listenUrlSearchParams=(r,a)=>listenUrlSearch((e,t)=>{let l=new URLSearchParams(e),m=new URLSearchParams(t).get(r);l.get(r)!==m&&a(m);});
|
|
27
|
+
|
|
28
|
+
let navigateToUrl=(t="",e)=>{t&&history[e?"replaceState":"pushState"](history.state,"",t);};
|
|
29
|
+
|
|
30
|
+
let navigateToHash=(t="")=>{let{pathname:a,search:e}=location;navigateToUrl(a+(e?"?"+e:"")+(t?"#"+t:""),!0);};
|
|
31
|
+
|
|
32
|
+
let navigateToHashParams=(a={},e="")=>{let i=!e,r="#/"+utils.getUrlHashPathname(e=e||location.hash)+("string"==typeof a?a:utils.buildUrlQueryString(a));return i&&(location.hash=r),r};
|
|
33
|
+
|
|
34
|
+
let navigateToMergedHashParams=(e={},t="")=>navigateToHashParams(utils.mergeUrlQueryParams(utils.getUrlHashParams(t),e),t);
|
|
35
|
+
|
|
36
|
+
let navigateToParams=(a={},e)=>{let i="string"==typeof a?a:utils.buildUrlQueryString(a);return utils.isBrowser&&navigateToUrl(location.pathname+i,e),i};
|
|
37
|
+
|
|
38
|
+
let navigateToMergedParams=(e={},t)=>navigateToParams(utils.mergeUrlQueryParams(utils.getUrlQueryParams(),e),t);
|
|
39
|
+
|
|
40
|
+
let navigateWithoutUrlParam=(o,a)=>{let e={},i=utils.getUrlQueryParams();for(let t in i)t!==o&&(e[t]=i[t]);return navigateToParams(e,a)};
|
|
41
|
+
|
|
42
|
+
let redirectTo=(r,t)=>{if(utils.isBrowser){let o=utils.buildUrlQueryString(t);location.href=r.replace(/\?+$/g,"")+o;}};
|
|
43
|
+
|
|
44
|
+
exports.createStorage = createStorage;
|
|
45
|
+
exports.getZonedDate = getZonedDate;
|
|
46
|
+
exports.gtag = gtag;
|
|
47
|
+
exports.gtagPageview = gtagPageview;
|
|
48
|
+
exports.isIE = isIE;
|
|
49
|
+
exports.isMobile = isMobile;
|
|
50
|
+
exports.listenUrlSearch = listenUrlSearch;
|
|
51
|
+
exports.listenUrlSearchParams = listenUrlSearchParams;
|
|
52
|
+
exports.navigateToHash = navigateToHash;
|
|
53
|
+
exports.navigateToHashParams = navigateToHashParams;
|
|
54
|
+
exports.navigateToMergedHashParams = navigateToMergedHashParams;
|
|
55
|
+
exports.navigateToMergedParams = navigateToMergedParams;
|
|
56
|
+
exports.navigateToParams = navigateToParams;
|
|
57
|
+
exports.navigateToUrl = navigateToUrl;
|
|
58
|
+
exports.navigateWithoutUrlParam = navigateWithoutUrlParam;
|
|
59
|
+
exports.redirectTo = redirectTo;
|
|
60
|
+
exports.storage = storage;
|
|
61
|
+
exports.storageClient = storageClient;
|
package/index.cjs.mjs
ADDED
package/index.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
export { createStorage, type CreateStorageConfig } from "./createStorage";
|
|
2
2
|
export { getZonedDate } from "./getZonedDate";
|
|
3
|
+
export { gtag } from "./gtag";
|
|
3
4
|
export { gtagPageview, type GtmPageviewArgs } from "./gtagPageview";
|
|
4
5
|
export { isIE } from "./isIE";
|
|
5
6
|
export { isMobile } from "./isMobile";
|
|
7
|
+
export { listenUrlSearch } from "./listenUrlSearch";
|
|
8
|
+
export { listenUrlSearchParams } from "./listenUrlSearchParams";
|
|
6
9
|
export { navigateToHash } from "./navigateToHash";
|
|
7
10
|
export { navigateToHashParams } from "./navigateToHashParams";
|
|
8
11
|
export { navigateToMergedHashParams } from "./navigateToMergedHashParams";
|
package/index.esm.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { isBrowser, isString, encode, decode, isNullOrUndefined, noop, isUndefined, isServer, getUrlHashPathname, buildUrlQueryString, mergeUrlQueryParams, getUrlHashParams, getUrlQueryParams } from '@koine/utils';
|
|
2
|
+
import { on } from '@koine/dom';
|
|
3
|
+
|
|
4
|
+
let t$1={g:"getItem",s:"setItem",r:"removeItem"};let storageClient=s=>{let r=(o,r,l)=>isBrowser?window[s?"sessionStorage":"localStorage"][t$1[o]](r,l):()=>{"development"===process.env.NODE_ENV&&console.warn(`[@koine/utils:storageClient]: ${s?"sessionStorage":"localStorage"} does not exists outside of browser.`);};return {get:(o,t,s)=>{let l=s??null;if("development"!==process.env.NODE_ENV||isBrowser||console.log(`[@koine/utils:storage] called 'get' outside of browser with default value '${JSON.stringify(s)}'.`),isBrowser){let e=r("g",o);if(e){e=t?t(e):e;try{let o=JSON.parse(e);o&&(l=o);}catch(o){l=e;}}}return l},set:(t,s,l=e=>e)=>{if("development"!==process.env.NODE_ENV||isBrowser||console.log("[@koine/utils:storage] called 'set' outside of browser does not work."),isBrowser)try{let e=l(isString(s)?s:JSON.stringify(s));r("s",t,e);}catch(e){"development"===process.env.NODE_ENV&&console.warn("[@koine/utils:createStorage]: 'set' error.",e);}},remove:o=>{if("development"!==process.env.NODE_ENV||isBrowser||console.log("[@koine/utils:storage] called 'remove' outside of browser does not work."),isBrowser)try{r("r",o);}catch(e){"development"===process.env.NODE_ENV&&console.warn("[@koine/utils:createStorage]: 'remove' error.",e);}},has:(o,t)=>{let s=t??!1;return "development"!==process.env.NODE_ENV||isBrowser||console.log(`[@koine/utils:storage] called 'has' outside of browser with default value '${JSON.stringify(t)}'.`),isBrowser&&(s=r("g",o)??!1),s}}};
|
|
5
|
+
|
|
6
|
+
let storage={l:storageClient(),s:storageClient(!0)};
|
|
7
|
+
|
|
8
|
+
let createStorage=(n,a)=>{let c=a?storage.s:storage.l,m=Object.keys(n).reduce((e,o)=>({...e,[o]:encode(o)}),{});return {get:(t,o)=>c.get(m[t],decode,o),getAll(e){if(!isBrowser)return "development"===process.env.NODE_ENV&&console.log("[@koine/utils:createStorage] attempt to use 'getAll' outside of browser."),{};let t={};for(let o in m){let s=this.get(o),l=e?.[o];isNullOrUndefined(s)?l&&(t[o]=l):t[o]=s;}return t},set(e,o){c.set(m[e],o,encode);},setMany(e){if("development"!==process.env.NODE_ENV||isBrowser||console.log("[@koine/utils:createStorage] attempt to use 'setMany' outside of browser."),isBrowser)for(let t in e){let o=e[t];isNullOrUndefined(o)?this.remove(t):this.set(t,o);}},has:e=>c.has(m[e]),remove(e){c.remove(m[e]);},clear(){if("development"!==process.env.NODE_ENV||isBrowser||console.log("[@koine/utils:createStorage] attempt to use 'clear' outside of browser."),isBrowser)for(let e in m)c.remove(m[e]);},watch:(e,t,r)=>isBrowser?on(window,"storage",o=>{let{key:s,oldValue:l,newValue:i}=o;s===m[e]&&(l&&!i?t?.():!l&&i&&r?.());}):("development"===process.env.NODE_ENV&&console.log("[@koine/utils:createStorage] attempt to use 'watch' outside of browser."),noop)}};
|
|
9
|
+
|
|
10
|
+
let t=(e,t)=>e;let getZonedDate=(o="",n)=>{if(o.endsWith("Z")||(o+="Z"),!n&&isBrowser)try{n=Intl.DateTimeFormat().resolvedOptions().timeZone;}catch(e){"development"===process.env.NODE_ENV&&console.warn("[@koine/browser:getZonedDate] failed reading timeZone, error",e);}return n?t(new Date(o)):new Date(o)};
|
|
11
|
+
|
|
12
|
+
let gtag=(...g)=>{isUndefined(window)||isUndefined(window.gtag)?noop():window.gtag(...g);};
|
|
13
|
+
|
|
14
|
+
let gtagPageview=(...t)=>gtag("event","page_view",{page_path:t[0]||location.pathname,page_title:t[1]||document.title,page_location:t[2]||location.href});
|
|
15
|
+
|
|
16
|
+
let isIE=(i=!0)=>{if(isServer)return i;let r=window.navigator.userAgent;return !!(r.indexOf("MSIE ")>0||r.indexOf("Trident/")>0)};
|
|
17
|
+
|
|
18
|
+
let isMobile=(e=!0)=>isServer?e:/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(window.navigator.userAgent);
|
|
19
|
+
|
|
20
|
+
let o=(t,e,o)=>function(...r){if(o)return e(),t.apply(this,r);let i=t.apply(this,r);return e(),i},r=isBrowser?location.search:"",i=()=>{let t=location.search;if(t!==r)for(let e of history.__.h.values())e(r,t);r=t;};let listenUrlSearch=t=>(history.__||(history.pushState=o(history.pushState,i),history.replaceState=o(history.replaceState,i),on(window,"popstate",i),history.__={h:new Set}),history.__.h.has(t)||history.__.h.add(t),()=>{history.__.h.delete(t);});
|
|
21
|
+
|
|
22
|
+
let listenUrlSearchParams=(r,a)=>listenUrlSearch((e,t)=>{let l=new URLSearchParams(e),m=new URLSearchParams(t).get(r);l.get(r)!==m&&a(m);});
|
|
23
|
+
|
|
24
|
+
let navigateToUrl=(t="",e)=>{t&&history[e?"replaceState":"pushState"](history.state,"",t);};
|
|
25
|
+
|
|
26
|
+
let navigateToHash=(t="")=>{let{pathname:a,search:e}=location;navigateToUrl(a+(e?"?"+e:"")+(t?"#"+t:""),!0);};
|
|
27
|
+
|
|
28
|
+
let navigateToHashParams=(a={},e="")=>{let i=!e,r="#/"+getUrlHashPathname(e=e||location.hash)+("string"==typeof a?a:buildUrlQueryString(a));return i&&(location.hash=r),r};
|
|
29
|
+
|
|
30
|
+
let navigateToMergedHashParams=(e={},t="")=>navigateToHashParams(mergeUrlQueryParams(getUrlHashParams(t),e),t);
|
|
31
|
+
|
|
32
|
+
let navigateToParams=(a={},e)=>{let i="string"==typeof a?a:buildUrlQueryString(a);return isBrowser&&navigateToUrl(location.pathname+i,e),i};
|
|
33
|
+
|
|
34
|
+
let navigateToMergedParams=(e={},t)=>navigateToParams(mergeUrlQueryParams(getUrlQueryParams(),e),t);
|
|
35
|
+
|
|
36
|
+
let navigateWithoutUrlParam=(o,a)=>{let e={},i=getUrlQueryParams();for(let t in i)t!==o&&(e[t]=i[t]);return navigateToParams(e,a)};
|
|
37
|
+
|
|
38
|
+
let redirectTo=(r,t)=>{if(isBrowser){let o=buildUrlQueryString(t);location.href=r.replace(/\?+$/g,"")+o;}};
|
|
39
|
+
|
|
40
|
+
export { createStorage, getZonedDate, gtag, gtagPageview, isIE, isMobile, listenUrlSearch, listenUrlSearchParams, navigateToHash, navigateToHashParams, navigateToMergedHashParams, navigateToMergedParams, navigateToParams, navigateToUrl, navigateWithoutUrlParam, redirectTo, storage, storageClient };
|
package/isIE.d.ts
CHANGED
package/isMobile.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare let listenUrlSearchParams: (paramName: string, handler: (paramNewValue: string | null) => void) => () => void;
|
package/navigateToHash.d.ts
CHANGED
|
@@ -1,8 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* It updates the browser's location hash by replacing the history state.
|
|
3
|
-
* The non-silent standard way would simply be `location.hash = "#new-hash"`
|
|
4
|
-
*
|
|
5
|
-
* @category location
|
|
6
|
-
*/
|
|
7
|
-
export declare function navigateToHash(hash?: string): void;
|
|
8
|
-
export default navigateToHash;
|
|
1
|
+
export declare let navigateToHash: (hash?: string) => void;
|
|
@@ -1,9 +1,2 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
3
|
-
* It updates the `location.hash` with the given query params, it uses `location.hash`
|
|
4
|
-
* if a second argument `hash` is not provded
|
|
5
|
-
*
|
|
6
|
-
* @category location
|
|
7
|
-
*/
|
|
8
|
-
export declare function navigateToHashParams(params?: string | AnyQueryParams, hash?: string): string;
|
|
9
|
-
export default navigateToHashParams;
|
|
1
|
+
import { type AnyQueryParams } from "@koine/utils";
|
|
2
|
+
export declare let navigateToHashParams: (params?: string | AnyQueryParams, hash?: string) => string;
|
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
3
|
-
* It updates the "query params" within the `location.hash`, it uses `location`
|
|
4
|
-
*
|
|
5
|
-
* @category location
|
|
6
|
-
*/
|
|
7
|
-
export declare function navigateToMergedHashParams(params?: NonNullable<AnyQueryParams>, hash?: string): string;
|
|
8
|
-
export default navigateToMergedHashParams;
|
|
1
|
+
import { type AnyQueryParams } from "@koine/utils";
|
|
2
|
+
export declare let navigateToMergedHashParams: (params?: NonNullable<AnyQueryParams>, hash?: string) => string;
|
|
@@ -1,9 +1,2 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
3
|
-
* Merge current URL query parameters with the given ones, it uses `history`.
|
|
4
|
-
*
|
|
5
|
-
* @category location
|
|
6
|
-
* @param replace Replace URL instead of pushing it in the history stack. By default it pushes it.
|
|
7
|
-
*/
|
|
8
|
-
export declare function navigateToMergedParams(params?: NonNullable<AnyQueryParams>, replace?: boolean): string;
|
|
9
|
-
export default navigateToMergedParams;
|
|
1
|
+
import { type AnyQueryParams } from "@koine/utils";
|
|
2
|
+
export declare let navigateToMergedParams: (params?: NonNullable<AnyQueryParams>, replace?: boolean) => string;
|
package/navigateToParams.d.ts
CHANGED
|
@@ -1,10 +1,2 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
3
|
-
* Change current URL query parameters, it uses `history`.
|
|
4
|
-
*
|
|
5
|
-
* @category location
|
|
6
|
-
* @param replace Replace URL instead of pushing it in the history stack. By default it pushes it.
|
|
7
|
-
* @returns The query string with initial `?`
|
|
8
|
-
*/
|
|
9
|
-
export declare function navigateToParams(params?: string | AnyQueryParams, replace?: boolean): string;
|
|
10
|
-
export default navigateToParams;
|
|
1
|
+
import { type AnyQueryParams } from "@koine/utils";
|
|
2
|
+
export declare let navigateToParams: (params?: string | AnyQueryParams, replace?: boolean) => string;
|
package/navigateToUrl.d.ts
CHANGED
|
@@ -1,7 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* It updates the browser's location URL with global `history` object
|
|
3
|
-
*
|
|
4
|
-
* @category location
|
|
5
|
-
*/
|
|
6
|
-
export declare function navigateToUrl(url?: string, replace?: boolean): void;
|
|
7
|
-
export default navigateToUrl;
|
|
1
|
+
export declare let navigateToUrl: (url?: string, replace?: boolean) => void;
|
|
@@ -1,8 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* Remove URL query parameter, it uses `history`
|
|
3
|
-
*
|
|
4
|
-
* @category location
|
|
5
|
-
* @param replace Replace URL instead of pushing it in the history stack. By default it pushes it.
|
|
6
|
-
*/
|
|
7
|
-
export declare function navigateWithoutUrlParam(paramName?: string, replace?: boolean): string;
|
|
8
|
-
export default navigateWithoutUrlParam;
|
|
1
|
+
export declare let navigateWithoutUrlParam: (paramName?: string, replace?: boolean) => string;
|
package/package.json
CHANGED
|
@@ -2,12 +2,23 @@
|
|
|
2
2
|
"name": "@koine/browser",
|
|
3
3
|
"sideEffects": false,
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@koine/dom": "2.0.0-beta.
|
|
6
|
-
"@koine/utils": "2.0.0-beta.
|
|
5
|
+
"@koine/dom": "2.0.0-beta.71",
|
|
6
|
+
"@koine/utils": "2.0.0-beta.71"
|
|
7
7
|
},
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
"peerDependenciesMeta": {
|
|
9
|
+
"date-fns-tz": {
|
|
10
|
+
"optional": true
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
"./package.json": "./package.json",
|
|
15
|
+
".": {
|
|
16
|
+
"module": "./index.esm.js",
|
|
17
|
+
"import": "./index.cjs.mjs",
|
|
18
|
+
"default": "./index.cjs.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"module": "./index.esm.js",
|
|
22
|
+
"main": "./index.cjs.js",
|
|
23
|
+
"version": "2.0.0-beta.71"
|
|
24
|
+
}
|
package/redirectTo.d.ts
CHANGED
|
@@ -1,9 +1,2 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
3
|
-
* Redirect to url with params {optionally}, removes eventual trailing question
|
|
4
|
-
* marks from the given URL, it uses `location`
|
|
5
|
-
*
|
|
6
|
-
* @category location
|
|
7
|
-
*/
|
|
8
|
-
export declare function redirectTo(url: string, params?: AnyQueryParams): void;
|
|
9
|
-
export default redirectTo;
|
|
1
|
+
import { type AnyQueryParams } from "@koine/utils";
|
|
2
|
+
export declare let redirectTo: (url: string, params?: AnyQueryParams) => void;
|
package/storage.d.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Storage, for `localStorage` and `sessionStorage`
|
|
3
|
-
*/
|
|
4
|
-
export declare const storage: {
|
|
1
|
+
export declare let storage: {
|
|
5
2
|
l: {
|
|
6
3
|
get: <TKey extends string, TValue = any>(key: TKey, transform?: ((value: string) => TValue) | undefined, defaultValue?: TValue | null | undefined) => NonNullable<TValue> | null;
|
|
7
4
|
set: <TKey_1 extends string, TValue_1 = any>(key: TKey_1, value?: TValue_1 | undefined, transform?: (value: any) => string) => void;
|
|
@@ -15,4 +12,3 @@ export declare const storage: {
|
|
|
15
12
|
has: <TKey_3 extends string, TValue_2 = any>(key: TKey_3, defaultValue?: TValue_2 | undefined) => boolean | NonNullable<TValue_2>;
|
|
16
13
|
};
|
|
17
14
|
};
|
|
18
|
-
export default storage;
|
package/storageClient.d.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
export type StorageClientConfig = Record<string, any>;
|
|
2
|
-
|
|
3
|
-
* Super minifiable `local/session Storage` client creator with SSR safety
|
|
4
|
-
*/
|
|
5
|
-
export declare const storageClient: <TConfig extends StorageClientConfig = StorageClientConfig>(useSessionStorage?: boolean) => {
|
|
2
|
+
export declare let storageClient: <TConfig extends StorageClientConfig = StorageClientConfig>(useSessionStorage?: boolean) => {
|
|
6
3
|
get: <TKey extends Extract<keyof TConfig, string>, TValue = TConfig[TKey]>(key: TKey, transform?: ((value: string) => TValue) | undefined, defaultValue?: TValue | null | undefined) => NonNullable<TValue> | null;
|
|
7
4
|
set: <TKey_1 extends Extract<keyof TConfig, string>, TValue_1 = TConfig[TKey_1]>(key: TKey_1, value?: TValue_1 | undefined, transform?: (value: any) => string) => void;
|
|
8
5
|
remove: <TKey_2 extends Extract<keyof TConfig, string>>(key: TKey_2) => void;
|
|
9
6
|
has: <TKey_3 extends Extract<keyof TConfig, string>, TValue_2 = TConfig[TKey_3]>(key: TKey_3, defaultValue?: TValue_2 | undefined) => boolean | NonNullable<TValue_2>;
|
|
10
7
|
};
|
|
11
|
-
export default storageClient;
|
package/createStorage.js
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: all[name]
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
createStorage: function() {
|
|
13
|
-
return createStorage;
|
|
14
|
-
},
|
|
15
|
-
default: function() {
|
|
16
|
-
return _default;
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
|
|
20
|
-
const _decode = /*#__PURE__*/ _interop_require_default._(require("@koine/utils/decode"));
|
|
21
|
-
const _encode = /*#__PURE__*/ _interop_require_default._(require("@koine/utils/encode"));
|
|
22
|
-
const _isBrowser = /*#__PURE__*/ _interop_require_default._(require("@koine/utils/isBrowser"));
|
|
23
|
-
const _isNullOrUndefined = /*#__PURE__*/ _interop_require_default._(require("@koine/utils/isNullOrUndefined"));
|
|
24
|
-
const _noop = /*#__PURE__*/ _interop_require_default._(require("@koine/utils/noop"));
|
|
25
|
-
const _on = /*#__PURE__*/ _interop_require_default._(require("@koine/dom/on"));
|
|
26
|
-
const _storage = /*#__PURE__*/ _interop_require_default._(require("./storage"));
|
|
27
|
-
const createStorage = (config, useSessionStorage)=>{
|
|
28
|
-
const client = useSessionStorage ? _storage.default.s : _storage.default.l;
|
|
29
|
-
const keys = Object.keys(config).reduce((map, key)=>({
|
|
30
|
-
...map,
|
|
31
|
-
[key]: (0, _encode.default)(key)
|
|
32
|
-
}), {});
|
|
33
|
-
return {
|
|
34
|
-
/**
|
|
35
|
-
* Get all storage value (it uses `localStorage.get()`).
|
|
36
|
-
*
|
|
37
|
-
* Unparseable values with `JSON.parse()` return their value as it is.
|
|
38
|
-
* On ssr or if the given `key` argument is not found `defaultValue` is
|
|
39
|
-
* returned, otherwise `null`.
|
|
40
|
-
*/ get (key, defaultValue) {
|
|
41
|
-
return client.get(keys[key], _decode.default, defaultValue);
|
|
42
|
-
},
|
|
43
|
-
/**
|
|
44
|
-
* Get all storage values (it uses `localStorage.get()`).
|
|
45
|
-
*
|
|
46
|
-
* `undefined` and `null` values are not returned.
|
|
47
|
-
*/ getAll (defaultValues) {
|
|
48
|
-
if (!_isBrowser.default) {
|
|
49
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
50
|
-
console.log(`[@koine/utils:createStorage] attempt to use 'getAll' outside of browser.`);
|
|
51
|
-
}
|
|
52
|
-
return {};
|
|
53
|
-
}
|
|
54
|
-
const all = {};
|
|
55
|
-
for(const key in keys){
|
|
56
|
-
const value = this.get(key);
|
|
57
|
-
const defaultValue = defaultValues?.[key];
|
|
58
|
-
if (!(0, _isNullOrUndefined.default)(value)) {
|
|
59
|
-
all[key] = value;
|
|
60
|
-
} else if (defaultValue) {
|
|
61
|
-
all[key] = defaultValue;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return all;
|
|
65
|
-
},
|
|
66
|
-
/**
|
|
67
|
-
* Set a storage value (it uses `localStorage.set()`).
|
|
68
|
-
*
|
|
69
|
-
* Non-string values are stringified with `JSON.stringify()`
|
|
70
|
-
*/ set (key, value) {
|
|
71
|
-
client.set(keys[key], value, _encode.default);
|
|
72
|
-
},
|
|
73
|
-
/**
|
|
74
|
-
* Set all given storage values (it uses `localStorage.set()`).
|
|
75
|
-
*
|
|
76
|
-
* Non-string values are stringified with `JSON.stringify()`, `undefined`
|
|
77
|
-
* and `null` values are removed from the storage
|
|
78
|
-
*/ setMany (newValues) {
|
|
79
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
80
|
-
if (!_isBrowser.default) {
|
|
81
|
-
console.log(`[@koine/utils:createStorage] attempt to use 'setMany' outside of browser.`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
if (_isBrowser.default) {
|
|
85
|
-
for(const key in newValues){
|
|
86
|
-
const value = newValues[key];
|
|
87
|
-
if (!(0, _isNullOrUndefined.default)(value)) {
|
|
88
|
-
this.set(key, value);
|
|
89
|
-
} else {
|
|
90
|
-
this.remove(key);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
},
|
|
95
|
-
/**
|
|
96
|
-
* Check if a storage value is _truthy_ (it uses `localStorage.get()`).
|
|
97
|
-
*/ has (key) {
|
|
98
|
-
return client.has(keys[key]);
|
|
99
|
-
},
|
|
100
|
-
/**
|
|
101
|
-
* Remove a storage value (it uses `localStorage.remove()`).
|
|
102
|
-
*/ remove (key) {
|
|
103
|
-
client.remove(keys[key]);
|
|
104
|
-
},
|
|
105
|
-
/**
|
|
106
|
-
* Clear all storage values (it uses `localStorage.remove()`).
|
|
107
|
-
*/ clear () {
|
|
108
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
109
|
-
if (!_isBrowser.default) {
|
|
110
|
-
console.log(`[@koine/utils:createStorage] attempt to use 'clear' outside of browser.`);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
if (_isBrowser.default) {
|
|
114
|
-
for(const key in keys){
|
|
115
|
-
client.remove(keys[key]);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
},
|
|
119
|
-
/**
|
|
120
|
-
* Watch a storage value changes, this needs to be executed only in browser
|
|
121
|
-
* context (it uses `window.addEventListener("storage")`).
|
|
122
|
-
*
|
|
123
|
-
* Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
|
|
124
|
-
*/ watch: (keyToWatch, onRemoved, onAdded)=>{
|
|
125
|
-
if (!_isBrowser.default) {
|
|
126
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
127
|
-
console.log(`[@koine/utils:createStorage] attempt to use 'watch' outside of browser.`);
|
|
128
|
-
}
|
|
129
|
-
return _noop.default;
|
|
130
|
-
}
|
|
131
|
-
const handler = (event)=>{
|
|
132
|
-
const { key, oldValue, newValue } = event;
|
|
133
|
-
if (key === keys[keyToWatch]) {
|
|
134
|
-
if (oldValue && !newValue) {
|
|
135
|
-
onRemoved?.();
|
|
136
|
-
} else if (!oldValue && newValue) {
|
|
137
|
-
onAdded?.();
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
const listener = (0, _on.default)(window, "storage", handler);
|
|
142
|
-
return listener;
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
};
|
|
146
|
-
const _default = createStorage;
|