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

urlgrey

v2.1.0

Published

urlgrey is a library for url querying and manipulation

Downloads

2,223,835

Readme

urlgrey

Build Status Coverage Status NPM version

browser support

Urlgrey is a library for url manipulation. It's got a chainable/fluent interface that makes a number of methods available for querying different aspects of a url, and even modifying it to create new urls.

Most methods are named after different parts of the url and allow you to read that part from the current url if you don't pass any parameters, or they allow you to generate a new url with a change to that part in the current url if you do pass a parameter.

For the examples below, we'll use the following url:

https://user:[email protected]/path/kid?asdf=1234#frag

To create a new urlgrey object, just pass a url to urlgrey like so:

var url = urlgrey("https://user:[email protected]/path/kid?asdf=1234#frag")

API specifics:

url.child([lastPart])

Setter/getter for the last part of a path:

  url.child(); // returns "kid" 
  url.child("grandkid"); // returns a new uri object with the uri 
                             // https://user:[email protected]/path/kid/grandkid?asdf=1234#frag

url.decode(encodedString);

Returns the decoded version of the input string using node's standard querystring.unescape().

      url.decode('this%20is%20a%20test');  // returns "this is a test"

url.encode(unencodedString);

Returns the encoded version of the input string using node's standard querystring.escape().

      url.encode('this is a test'); // returns 'this%20is%20a%20test'

url.hash([newHash])

Setter/getter for the url fragment/anchor/hash of a path.

      url.hash(); // returns 'frag'
      url.hash("blah"); // returns a new uri object with the uri
                            // https://user:[email protected]/path/kid/?asdf=1234#blah

url.hostname([newHostname])

Setter/getter for the url hostname.

      url.hostname(); // returns 'subdomain.asdf.com'
      url.hostname("geocities.com"); // returns a new uri object with the uri
                            // https://user:[email protected]/path/kid/?asdf=1234#frag

url.parent();

Get the parent URI of the current URI. (This property is read-only).

      url.parent();  // returns a new uri object with the uri
                     // https://user:[email protected]/path/

url.password([newPassword]);

Setter/getter for the password portion of the url.

      url.password(); // returns 'pass'
      url.password("newpass"); // returns a new uri object with the uri
                            // https://user:[email protected]/path/kid/?asdf=1234#frag

url.extendedPath([string]);

Setter/getter for the path, querystring and fragment portion of the url all at once.

      url.extendedPath(); // returns '/path/kid?asdf=1234#frag'
      url.extendedPath("/newpath?new=query#newfrag"); // returns a new uri object with the uri
                               // https://user:[email protected]/newpath?new=query#newfrag

url.path([mixed]);

Setter/getter for the path portion of the url.

      url.path(); // returns '/path/kid'
      url.path("newpath"); // returns a new uri object with the uri
                               // https://user:[email protected]/newpath

      // ALSO, .path() can take arrays of strings as input as well:
      url.path(['qwer', '/asdf'], 'qwer/1234/', '/1234/'); 
                      // this returns a new uri object with the uri
                      // https://user:[email protected]/qwer/asdf/qwer/1234/1234

Note: changing the path will remove the querystring and hash, since they rarely make sense on a new path.

url.port([newPort]);

Setter/getter for the port portion of the url.

      url.port(); // returns 80
      url.port(8080); // returns a new uri object with the uri
                          // https://user:[email protected]:8080/path/kid/?asdf=1234#frag

url.protocol([newProtocol]);

Setter/getter for the protocol portion of the url.

      url.protocol(); // returns 'https'
      url.protocol("http"); // returns a new uri object with the uri
                                // http://user:[email protected]/path/kid/?asdf=1234#frag

url.query([mixed]);

Setter/getter for the querystring using javascript objects.

      url.query(); // returns {asdf : 1234}
      url.query(false); // returns a new uri object with the querystring-free uri
                            // https://user:[email protected]/path/kid#frag
      url.query({spaced : 'space test'})
                                // returns a new uri object with the input object serialized
                                // and merged into the querystring like so:
                                // https://user:[email protected]/path/kid/?asdf=1234&spaced=space%20test#frag

NOTE: escaping and unescaping of applicable characters happens automatically. (eg " " to "%20", and vice versa)

NOTE: an input object will overwrite an existing querystring where they have the same names.

NOTE: an input object will remove an existing name-value pair where they have the same names and the value in the input name-value pair is null.

url.queryString([newQueryString]);

Setter/getter for the querystring using a plain string representation. This is lower-level than .query(), but allows complete control of the querystring.

      url.queryString(); // returns asdf=1234  (notice there is no leading '?')
      url.queryString("blah"); // returns a new uri object with a new querystring
                               // https://user:[email protected]/path/kid?blah#frag

NOTE: no escaping/unescaping of applicable characters will occur. This must be done manually.

url.rawChild();

This method is the same as url.child() but does not automatically url-encode any part of the input.

url.rawPath();

This method is the same as url.path() but does not automatically url-encode any part of the path.

url.rawQuery();

This method is the same as url.query() but does not automatically url-encode any of the keys or values in an input object.

url.toJson();

Returns the json representation of the uri object, which is simply the uri as a string. The output is exactly the same as .toString(). This method is read-only.

  url.toJson(); // returns "https://user:[email protected]/path/kid/?asdf=1234#frag"

url.toString();

Returns the string representation of the uri object, which is simply the uri as a string. This method is read-only.

      url.toString(); // returns "https://user:[email protected]/path/kid/?asdf=1234#frag"

url.username([newUsername])

Setter/getter for the username portion of the url.

      url.username(); // returns 'user'
      url.username("newuser"); // returns a new uri object with the 
                               // uri https://newuser:[email protected]/path/kid/?asdf=1234#frag

Installation:

node.js:

npm install urlgrey --save

Also! If you're using urlgrey in an http application, see urlgrey-connect. It gives you an urlgrey object already instantiated with the request url as req.uri in all your request handlers.

Contributing:

Testing:

Run the node tests:

  • make test