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

@rentspree/path

v0.2.3

Published

URL path params/query resolve with ease

Downloads

13

Readme

rentspree-path

Generated with nod NPM version Build Status Coverage Status

URL path params/query resolve with ease

This is a helper librarly that combine the use of path-to-regexp with qs query string library. While those librarly work independently, the former is for generating path with path params, the latter is for generating query string from object (or in the reverse). This library combinses the use of these two path to build url.

The library is written in ES6 most advance syntax.

Please enjoy!

Install

$ npm install --save @rentspree/path

or if you are a fan of Yarn

$ yarn add @rentspree/path

Usage

import { buildPath } from "@rentspree/path";

// return "/api/user/1234?name=John&lastName=Doe"
buildPath("/api/user/:id", {id: "1234"}, {name: "John", lastName: "Doe"})

Or if you are interested in only using the query string append, you can surely do it

import { query } from "@rentspree/path"

// return "?name=John&lastName=Doe"
query.stringify({name: "John", lastName: "Doe"})

// returns {query: "hello", consulta: "hola"}
query.parse("?query=hello&consulta=hola")

API

Table of Contents

buildPath

Build path from path regexp and params and optionally append query suffix to the path

Parameters

  • path String the path regex
  • params Object the params to interpolate in path
  • query Object? the query object to be converted to string

Examples

// return /api/user/123456?name=John&lastName=Doe
buildPath("/api/user/:id", {id: "123456"}, {name: "John", lastName: "Doe"}
// return /content/post/this-is-a-good-post
buildPath("/content/post/:slug", {slug: "this-is-a-good-post"})

Returns (String | null) the built path

ParamInterpolatedError

Default error class

setErrorClass

Set the Error class which would be thrown when buildPath function failed

Parameters

  • error Class an Error class which would be thrown

Examples

// if you set
setErrorClass(SomeClass)
// when the buildPath function failed it will throw something like this
throw new SomeClass()

query

parse

This method parse a query string into object

Parameters
  • str String the string to parse (optional, default "")
Examples
// returns {str1: "hello", str2: "hola"}
parse("?str1=hello&str2=hola")
// returns {name: "John", lastName: "Doe"}
parse("name=John&lastName=Doe")

Returns Object the query object parsed from query string

stringify

This method stringify object into query string

Parameters
  • obj Object the object to be stringify (optional, default {})
Examples
// returns "?name=John&lastName=Doe"
stringify({name: "John", lastName: "Doe"})

Returns String the stringified query result

urlJoin

Build URL from URL-JOIN library

Parameters

  • args String all path what you need to join

Examples

// return /api/user/123456?name=John&lastName=Doe
urlJoin("/api/user/123456", "?name=John&lastName=Doe")
// return /content/post/this-is-a-good-post
urlJoin("/content/post///", "//this-is-a-good-post")

Returns String the built path

License

MIT © Potsawee Vechpanich