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

nurl

v1.0.0

Published

Module that provides a simple, immutable URL object for access and manipulation

Downloads

57

Readme

nURL - A simple URL object for node.js

This module provides a simple URL object that provides a clean, easy-to-use interface to accessing a URLs values as well as for creating and manipulating URLs. Each URL object is immutable: whenever a value is altered, a new instance is returned leaving the original unchanged.

This module builds on top of the core 'url' and 'querystring' modules.

Installation

Nothing more than:

npm install nurl

Sample usage

URLs are modelled as:

scheme://user:password@hostname:port/pathname?search#fragment

Note that the // following the scheme is optional for certain schema (eg mailto).

Now, starting with:

var nurl = require('nurl');

you can create a URL object using one of:

var u = nurls.parse('http://www.google.com/search?q=node.js#top');
var u = (new nurl.Url()).setScheme('http')
					    .setHostname('www.google.com')
						.setPathname('/search')
						.setQueryParam('q', 'node.js')
						.setFragment('top');
var u = new nurl.Url('http', Null, Null, 'www.google.com', Null, '/search', 'q=node.js', 'top');

The various components of u can be accessed through both read-only properties and getter methods:

u.protocol, u.scheme, u.getProtocol(), u.getScheme() // => 'http'
u.getUser() // => Null
u.getPassword() // => Null
u.auth, u.getAuth() // => Null
u.hostname, u.getHostname() // => 'www.google.com'
u.port, u.getPort() // => 80
u.pathname, u.getPathname() // => '/search'
u.search, u.getSearch() // => '?node.js'
u.fragment, u.hash, u.getFragment(), u.getHash() // => 'top'
u.href, u.getHref() // => 'http://www.google.com/search?q=node.js#top'

Note that:

  • both the properties and getters return Null when the component has no value. w
  • some property names are aliases (such as 'protocol' and 'scheme')

More detailed interrogation can be performed using:

u.getSubdomains() // => ['www', 'google', 'com']
u.getSubdomain(0) // => 'www'
u.hasQueryParam('q') // => True
u.getQueryParam('q') // => 'node.js'
u.getPathSegments() // => ['search']
u.getPathSegment(0) // => 'search'
u.isRelative() // => False
u.isAbsolute() // => True

Setters follow a similar pattern, each returning a new URL object:

u.setProtocol('https'), u.setScheme('https')
u.setAuth('user', 'secret')
u.setHostname('example.com')
u.setSubdomain(0, 'sample') // => 'sample.google.com'
u.setPort('80')
u.setPathname('/')
u.setPathSegment(1, 'extension') // => '/search/extension'
u.setPathSegments(['search', 'some query here'])
u.setQueryParam('q', 'testing')
u.setHash('top')

URL objects can be merged to create a new object - the properties of the passed in URL will fill in any missing components:

var u1 = nurl.parse('http://www.google.com');
var u2 = nurl.parse('/search?q=test.js');
u1.mergeWith(u2) // => 'http://www.google.com/search?q=test.js'

Testing

All tests are written in the excellent vows library. To run them, use

$ cd /path/to/nurl
$ vows --spec

Author

David Winterbottom ([email protected])