Skip to content

Manage query parameters in URL hash.

License

afeiship/hash-query

Repository files navigation

hash-query

Manage query parameters in URL hash.

version license size download

installation

yarn add @jswork/hash-query

usage

import HashQuery from '@jswork/hash-query';

const hq = new HashQuery();

// Get query parameters
const params = hq.get();
console.log(params.get('id')); // null

// Set query parameters
hq.set(new URLSearchParams({ id: '123', name: 'test' }));
// URL will be updated to #/?id=123&name=test

// Update query parameters
hq.update({ name: 'new_test', age: '30' });
// URL will be updated to #/?id=123&name=new_test&age=30

hq.update({ age: null });
// URL will be updated to #/?id=123&name=new_test

// Convert to JSON
const json = hq.toJson();
console.log(json); // { id: '123', name: 'new_test' }

// Create from JSON
const newParams = HashQuery.fromJson({ city: 'shanghai' });
hq.set(newParams);
// URL will be updated to #/?city=shanghai

// Static toJson
const staticJson = HashQuery.toJson(new URLSearchParams('key=value'));
console.log(staticJson); // { key: 'value' }

license

Code released under the MIT license.

About

Manage query parameters in URL hash.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published