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

urlable

v0.0.1

Published

synchronize an object with the URL query string

Downloads

3

Readme

urlable

urlable, is a tiny library, that helps to create a dynamic object, where each mutation results in a query string update. This becomes quite useful, when used in conjuction with state driven frameworks, like React or Vue.

Installation

The package is provided as an ESM build, and can be included as is in the browser.

Disclaimer

This library relies heavily on "cutting-edge" technologies:

  • Proxy
  • Reflect

No effort has been made to include polyfills.

Configuration

You can easily tweak the behavior of the library, by provided options in addition to the initial object.

| option | description | |---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | serialize | a function that serialize the object, into a dictionnary, where each value MUST be a string | | deserialize | the reverse operation of serialize, where we take a dictionnary parsed from the query string, and transform the data into the state object | | readUrl | this is mainly useful for testing purposes, but it allows to override the URL reading behavior | | writeUrl | the function to call when writing the query string to the URL. It receives the new query string when required to update. Again, this is mainly useful for testing purposes |

The type definition can be found as UrlableOptions.

Example

import urlable from 'urlable';

const pagination = urlable({
  page: 1
});

pagination.page = 2; // querystring updates to '?page=2'.

You can find a full example, in the examples folder.

Q&A

use the serialize option:

// display the property `currentPage` as `page` in the URL.
const pagination = urlable(
  { currentPage: 2 },
  { serialize: ({ currentPage }) => ({ page: String(currentPage) }) }
)

remove the property in the serialize option:

// The `totalPages` property will not show in the query string
const pagination = urlable(
  { page: 1, totalPages: 10 },
  { serialize: (x) => ({ page: String(x.page) }) }
);

the deserialize options is here:

// pagination.page will always be a number.
const pagination = urlable(
  { page: 1 },
  { deserialize: (x) => ({ page: Number(x.page) }) }
);