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

psr7-js

v0.3.1

Published

URI and Query String immutable manipulation, support for bracket syntax.

Downloads

322

Readme

Latest Stable Version License CI Workflow Coverage Status Total Downloads

PSR7-JS

URI

Coming from the PHP World, you probably use PSR-7 UriInterface almost every day to manipulate URIs.

But what if you come to front-end development? You can use native URL objects, which are mutable, have no fluent setters and different property names. And only absolute URLs are supported.

So, this library is intended to expose the same methods as PSR-7, in an immutable way:

import {URI} from 'psr7-js';

let uri = new URI('/foo'); // Relative URLs are supported - Defaults to window.location.href
uri = uri.withQuery('foo=bar');

console.log(uri.toString()); // /foo?foo=bar

QueryString

Query Strings aren't part of the PSR-7 specification, but there are still issues when dealing with complex query strings in Javascript.

There are lots of query string manipulation libraries on NPM packages, but I couldn't find one which properly handles PHP's array syntax (sequential and/or associative).

import {QueryString} from 'psr7-js';

let qs = new QueryString('?foo=bar'); // Accepts strings (leading ? is ignored) or objects - Defaults to window.location.search
qs = qs.withParam('bar', 'baz'); // foo=bar&bar=baz
qs = qs.withoutParam('bar'); // foo=bar
qs = qs.withParam('foos', ['foo', 'bar']); // foo=bar&foos[]=foo&foos[]=bar
qs = qs.withParam('sort', {'updated_at': 'desc', 'hits': 'desc'}); // foo=bar&foos[]=foo&foos[]=bar&sort[updated_at]=desc&hits=desc
qs = qs.withoutParam('sort', 'hits'); // foo=bar&foos[]=foo&foos[]=bar&sort[updated_at]=desc

console.log(qs.getParams());

/*
{
  "foo": "bar",
  "foos": [
    "foo",
    "bar"
  ],
  "sort": {
    "updated_at": "desc"
  }
}
 */

console.log(qs.toString()); // foo=bar&foos[]=foo&foos[]=bar&sort[updated_at]=desc

Inspired by bentools/querystring.

Tests

yarn test // or npm run-script test

License

MIT.