npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sync-url-search-params

v0.1.6

Published

Sync URL query params. Zero dependency.

Downloads

3

Readme

sync-url-search-params

Sync URL search params

Purposes

  • Initialize the URL query params with default ones
  • Update query params on demand

Installation

  • Using npm
npm install --save sync-url-search-params
  • Using yarn
yarn add sync-url-search-params

Usage examples (using React)

Won't trigger re-render

import SyncURLSearchParams from 'sync-url-search-params'

const susp = new SyncURLSearchParams({ foo: 'bar' })
// Or
const foo = new SyncURLSearchParams(window.location.search).get('foo')

function App() {
  console.count('render count');
  return (
    <>
      <pre>param `foo`: {susp.getParam('foo')}</pre>
      <pre>location.search: {window.location.search}</pre>
      <pre>state: {JSON.stringify(state)}</pre>
      <pre>cache: {JSON.stringify(susp.getAllParams())}</pre>

      <button onClick={() => susp.setParam('foo', 'baz')}>Change bar to baz</button>
      <br/>
      <button onClick={() => susp.clearParam('foo')}>Clear query param</button>
    </>
  )
}

Will trigger re-render

CodeSandbox


APIs

  1. SyncURLSearchParams(defaultParams: { [x: string | number | symbol]: string | number | boolean | null | undefined }, options?: { shouldKeepURLUndeclaredParams?: boolean }) => ({ getParam, getParams, getAllParams, setParam, setParams, clearParam, clearParams, setCallback })

    Initialize the hook with default params.

    Automatic URL query params synchronization will happen only once on mount.

    And take the value from URL search params as priority if it exists.

  2. getParam: (key: string) => string

    Get specific key from query params. Autosuggestion mapped to keys of the default params.

  3. getParams: (...keys: string[]) => Object<string, string>

    Get a set of params.

  4. getAllParams: () => Object

    Get all query params. The result contains all records with keys of the default params except those that were cleared.

  5. setParam: (key: string | number | symbol, value: string | number | boolean | null | undefined, options?: { shouldKeepURLUndeclaredParams?: boolean }) => boolean

    Set a specific key with a value. Empty values (empty string, null, undefined) will be cleared.

    • Return true if successfully set
    • Otherwise false if window.history.pushState is not available
  6. setParams: (Object<key: string | number | symbol, value: string | number | boolean | null | undefined>, options?: { shouldKeepURLUndeclaredParams?: boolean }) => boolean

    Set a set of records. Empty values (empty string, null, undefined) will be cleared.

    • Return true if successfully set
    • Otherwise false if window.history.pushState is not available
  7. clearParam: (key: string, options?: { shouldKeepURLUndeclaredParams?: boolean }) => boolean

    Clear specific key from query params. Same as setParam with empty value.

  8. clearParams: (keys?: string[], options?: { shouldKeepURLUndeclaredParams?: boolean }) => boolean

    Clear a set of keys from query params. Same as setParams with empty values.

    If input is empty, all params will be cleared

  9. setCallback<T>(callback: (result: boolean, params: T) => void, shouldInvokeCallbackImmediately?: boolean)

    Set callback that invokes once change event happens (after initialization), and every time newly set if opt in.