state-in-url
Version:
Store state in URL as in object, types and structure are preserved, with TS validation. Same API as React.useState, wthout any hasssle or boilerplate. Next.js@14-15 and react-router@6-7.
2 lines (1 loc) • 1.26 kB
JavaScript
const a=n=>{const t=typeof n;const s=n===null;const r=Array.isArray(n);const o=n instanceof Date;const e=!s&&!o&&!r&&t==="object";return s&&"null"||o&&"date"||r&&"array"||e&&"object"||t};const c=typeof window==="undefined";const f=n=>new URLSearchParams(typeof n==="string"?l(n):n?.toString?.()||"");const l=n=>n.split("?")?.[1]||n||"";const u=(n,t)=>JSON.stringify(n)===JSON.stringify(t);function w(n,t){const s=new URLSearchParams;const r=i(n,[...new URLSearchParams(t||"").entries()]);for(const[o,e]of r){s.set(o,e)}return s.toString()}function p(n,t){return Object.fromEntries(i(n,Object.entries(t||{})))}function i(n,t){const s=Object.keys(n);const r=[];for(let o=0;o<t.length;o++){if(s.includes(t[o]?.[0])){r.push([t[o][0].replace(/\+/g," "),t[o][1]])}}return r}function y(n,t){const s=Object.assign({},n);for(const[r]of Object.entries(n)){const o=r;const e=t[o]!==void 0;s[o]=e?t[o]:n[o]}return s}const g={push:n=>{window&&window.history.pushState(null,"",n)},replace:n=>{window&&window.history.replaceState(null,"",n)}};const S=/apple/i.test(!c&&navigator?.userAgent||"");export{y as assignValue,i as filterUnknown,p as filterUnknownParams,w as filterUnknownParamsClient,f as getParams,u as isEqual,c as isSSR,S as isSafari,g as routerHistory,a as typeOf};