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

tiny-uri

v9.1.5

Published

Lightweight Javascript library for handling URLs

Downloads

264

Readme

tiny-uri

tiny-uri is yet another Javascript library for working with URLs. It offers a fluent interface, method chaining, and sensible means of manipulating Url parts and a file size of less than 5K, gzipped.

Installation

npm install https://github.com/robhicks/tiny-uri.git

# or

yarn add https://github.com/robhicks/tiny-uri.git

Use

You can use the library in the browser or on NodeJs.

In the browser you can load dist/tiny-uri.iife.js in a script tag, or if you are using an es6 aware bundler like RollupJs, you can import it into your entry file.

In NodeJs, require it as usual.

Examples

Getting URL parts

import TinyUri from 'tiny-uri';

const uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');

console.log('scheme', uri.scheme()); // 'http'
console.log('host', uri.host()); // 'example.org'
console.log('path', uri.path.toString()); // /path/to/foo/index.html
console.log('query', uri.query.toString()); // hello=world

Manipulating URL parts

import TinyUri from 'tiny-uri';

const uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');

uri
  .scheme('https')
  .host('example.com')
  .authority('user:[email protected]')
  .path.set('path/to/bar/index.html')
  .query.merge({q, 'foo'});

console.log('scheme', uri.scheme()); // 'https'
console.log('host', uri.host()); // 'example.com'
console.log('authority', uri.authority()) // 'user:[email protected]'
console.log('path', uri.path.toString()); // /path/to/bar/index.html
console.log('query', uri.query.toString()); // hello=world

Manipulating paths

// remove the last segment of a path

const url = 'https://user:[email protected]/path/to/file.xml';
const uri = new TinyUri(url);

expect(uri.path.delete().path.toString()).to.be.equal('to/file.xml');
// remove a specific segment of the the path

const url = 'https://user:[email protected]/path/to/file.xml';
const uri = new TinyUri(url);

expect(uri.path.delete(0).path.toString()).to.be.equal('to/file.xml');
// should remove several segments of the the path
// delete accepts an array of segment locations. segment locations must be
// in ascending order and within the range of path segments

const url = 'https://user:[email protected]/really/long/path/to/file.xml';
const uri = new TinyUri(url);

expect(uri.path.delete([0, 1, 2, 3]).path.toString()).to.be.equal('file.xml');

Manipulating query strings

import TinyUri from 'tiny-uri';

const uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');

console.log('query', uri.query.toString()); // hello=world

Setting the query string

import TinyUri from 'tiny-uri';

const uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');

uri.query.set({goodby: 'world'});

console.log('query', uri.query.toString()); // goodby=world

Clearing the query string

import TinyUri from 'tiny-uri';

const uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');

uri.query.clear();

console.log('query', uri.query.toString()); //

Adding to the query string

import TinyUri from 'tiny-uri';

const uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');

uri.query.add({goodby: 'world'});

console.log('query', uri.query.toString()); // hello=world&goodby=world

Merging the query string

import TinyUri from 'tiny-uri';

const uri = new TinyUri('http://example.org/path/to/foo/index.html?hello=world');

uri.
  query.add({goodby: 'world'})
  query.merge({hello: 'universe'})

console.log('query', uri.query.toString()); // hello=universe&goodby=world

Getting a specific query string parameter

Let's say you want to get the value of a context query string parameter

const url = 'https://user:[email protected]/path/to/file.xml?context=foo&credentials=bar';
const uri = new TinyUri(url);
const credentials = uri.query.get('credentials');
const context = uri.query.get('context');
const hot = uri.query.get('hot');

console.log(credentials === 'bar') // true
console.log(context === 'foo') // true
console.log(hot === null) // true

Chaining Methods

You can chain Uri, Path and Query methods.

expect(uri.scheme().toString()).toBe('https');
expect(uri.host().toString()).toBe('big.example.com');
expect(uri.port().toString()).toBe('');
expect(Array.isArray(uri.path.get())).toEqual(true);
expect(uri.path.toString()).toEqual('path/to/file.xml');
expect(uri.query).toEqual(jasmine.any(Object));
expect(uri.query.add({foo: 'bar'}).query.toString()).toEqual('context=foo&credentials=bar&foo=bar');
expect(uri.query.add({foo: 'bar'}).query.merge({foo: 'bars'}).query.toString()).toEqual('context=foo&credentials=bar&foo=bars');
expect(uri.query.clear().query.add({foo: 'bar'}).query.merge({foo: 'bars'}).query.toString()).toEqual('foo=bars');
expect(uri.query.clear().query.add({foo: 'bar'}).query.merge({foo: 'bars'}).query.toString(true)).toEqual('https://user:[email protected]/path/to/file.xml?foo=bars');

Changes

  • 9.0.0 - the library hasn't changed but the build system has. This library only supports modern browsers and Node JS. Importing the library in the browser, pulls in an minified esm build. If you want to import the esm source directly, use import TinyUri from 'tiny-uri/src/TinyUri.js'. In node, you can use the default cjs or if you are using esm import TinyUri from 'tiny-uri/index.mjs'.