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

2 lines (1 loc) 484 B
"use strict";var a=require("../encoder/encoder.js");var d=require("../utils.js");function u(n,c,s){const r=d.getParams(s);const e=Object.keys(n||{});for(let o=0;o<e.length;o++){const t=e[o];const i=n[t];const f=c?.[t];if(JSON.stringify(i)!==JSON.stringify(f)){r.set(t,a.encode(i))}}return r.toString()}function g(n,c){const s=Object.assign({},c||{});const r=d.getParams(n);for(const[e,o]of r){const t=c?.[e];s[e]=a.decode(o,t)??t}return s}exports.decodeState=g;exports.encodeState=u;