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

parsedurl

v1.0.0

Published

A simple JavaScript class/library for parsing/manipulating http URL's.

Downloads

4

Readme

ParsedURL

A simple JavaScript class/library for parsing/manipulating http URL's.

It's especially helpful when you want to easily change a URL's components such as its parameters, hostname, or path while leaving the rest of it intact.

How to use it

Just download and include parsedURL.js with your web page:

<script src="/js/ParsedURL.js"></script>

or install via npm:

npm install parsedurl

Then when you have a URL you need to parse just create a new ParsedURL object:

var url = "https://a.complex-url.com:8080/users/jon/messages?q=cake&when=recent#searchbox"";
var purl = new ParsedURL(url);

Getting the URL string

You can always get the URL back as a string by asking for it:

var newUrl = purl.toString();
// newUrl is now "https://a.complex-url.com:8080/users/jon/messages?q=cake&when=recent#searchbox"

or by using type coercion:

var newUrl = "" + purl;
// newUrl is now "https://a.complex-url.com:8080/users/jon/messages?q=cake&when=recent#searchbox"

Hostname and scheme/protocol

If you need the hostname, get it:

var hostname = purl.hostname;

Or the scheme/protocol:

var scheme = purl.scheme;

If you want to change the hostname (or scheme), just set it:

purl.hostname = "a.different-url.com";

Port

You can get/set the port as you would the hostname, but note that it will be a number, not a string.

For example:

console.log(purl.toString());
purl.port++;
console.log(purl.toString());

Outputs:

https://a.complex-url.com:8080/users/jon/messages?q=pie&when=recent#searchbox

https://a.complex-url.com:8081/users/jon/messages?q=pie&when=recent#searchbox

URL path

The URL's path can be be accessed or changed the same way you would the hostname:

var path = purl.path;
console.log(path);
purl.path = path.replace("jon", "tim");
console.log(purl.toString());

Outputs:

/users/jon/messages
https://a.complex-url.com:8080/users/tim/messages?q=cake&when=recent#searchbox

If you want to get the path parsed into an array:

var pathDirectories = purl.parsedPath();
for (var i = 0; i < pathDirectories.length; i++) {
  console.log(pathDirectories[i]);
}

Outputs:

users
tim
messages

URL parameters

All of the URL's parameters are parsed and placed into the params attribute as an object literal.

For example:

// Print out all URL parameters + values
for (var paramName in purl.params) {
  console.log(paramName + " = " + purl.params[paramName]);
}

// Change the q parameter from "cake" to "pie"
purl.params.q = "pie";
console.log(purl.toString())

Prints out:

q = cake
when = recent
https://a.complex-url.com:8080/users/jon/messages?q=pie&when=recent#searchbox

Hash

You can get/set the way you would guess by now:

var hash = purl.hash;
console.log(hash);

Outputs:

searchbox

Demonstration/Tests

Here's a jsbin using Angluar.js to ouput the components from a bunch of URL's.

License

Use it for anything you want, as long as it is for good and not evil.

Credit

Created by Jon Abrams - Twitter - Github