UNPKG

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-16, react-router@6-7, remix@2, and Astro.

2 lines (1 loc) 506 B
import{decode as d,encode as g}from"../encoder/encoder.mjs";import{getParams as i}from"../utils.mjs";function m(c,s,r){const e=new URLSearchParams(i(r));const o=Object.keys(c||{});for(let n=0;n<o.length;n++){const t=o[n];const f=c[t];const a=s?.[t];if(JSON.stringify(f)!==JSON.stringify(a)){e.set(t,g(f))}else{e.delete(t)}}return e.toString()}function S(c,s){const r=Object.assign({},s||{});const e=i(c);for(const[o,n]of e){const t=s?.[o];r[o]=d(n,t)??t}return r}export{S as decodeState,m as encodeState};