@jswork/url-state-codec
v1.0.0
Published
A tiny codec for serializing JSON state into shareable URL-safe strings.
Downloads
292
Readme
url-state-codec
A tiny codec for serializing JSON state into shareable URL-safe strings.
installation
pnpm add @jswork/url-state-codecusage
import { encode, decode } from '@jswork/url-state-codec';
const settings = { v: 1, layout: 'grid', zoom: 1.5, title: '蛋白质筛选' };
// object -> URL-safe string (put it in ?state=...)
const encoded = encode(settings);
// -> "eyJ2IjoxLCJsYXlvdXQiOiJncmlkIiwiem9vbSI6MS41LCJ0aXRsZSI6Iuibi-eZvei0qOetm-mAiX0"
// URL-safe string -> object (null when absent or corrupted)
const restored = decode(encoded); // => settingsapi
encode(state)— JSON → base64url string (URL-safe, no padding, non-ASCII friendly).decode(raw)— base64url string → object, returnsnullfor absent/corrupted input.
license
Code released under the MIT license.
