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) • 522 B
JavaScript
;var i=require("../encoder/encoder.js");var d=require("../utils.js");function u(c,s,r){const t=new URLSearchParams(d.getParams(r));const n=Object.keys(c||{});for(let o=0;o<n.length;o++){const e=n[o];const a=c[e];const f=s?.[e];if(JSON.stringify(a)!==JSON.stringify(f)){t.set(e,i.encode(a))}else{t.delete(e)}}return t.toString()}function S(c,s){const r=Object.assign({},s||{});const t=d.getParams(c);for(const[n,o]of t){const e=s?.[n];r[n]=i.decode(o,e)??e}return r}exports.decodeState=S;exports.encodeState=u;