URI.js - URL Manipulation Library

1.19.11 · maintenance · verified Sun Apr 19

URI.js is a robust JavaScript library designed for parsing, manipulating, and constructing URLs (Uniform Resource Locators) and URIs (Uniform Resource Identifiers). Its current stable version is 1.19.11, with development focused on security patching and maintenance rather than new feature additions. The library has historically seen frequent updates to address various parsing vulnerabilities, particularly concerning malformed URLs, which highlights its commitment to secure URI handling. While it provides a comprehensive and fluent API for complex URL transformations, the project explicitly recommends developers leverage native browser APIs like `URL` and `URLSearchParams` for modern web environments, suggesting URI.js is most suitable for legacy projects, environments lacking native URL APIs, or for advanced scenarios such as URI template expansion. It differentiates itself by offering a mutable, chaining API for intricate URL modifications that can be cumbersome with standard string methods or even native APIs for older browser targets.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates core URI object instantiation, fluent method chaining for URL mutation, query string normalization, and URI template expansion.

import URI from 'urijs';

const originalUrl = "http://example.org/foo.html?hello=world";

// Create a new URI object
const url = new URI(originalUrl);

// Perform a series of fluent manipulations
const modifiedUrl = url
  .username("rodneyrehm") // Add a username
  .directory("bar")     // Change directory segment
  .suffix("xml")      // Change file extension
  .query({ foo: "bar", hello: ["world", "mars"] }) // Set multiple query parameters
  .tld("com");          // Change top-level domain

console.log(`Original URL: ${originalUrl}`);
console.log(`Modified URL: ${modifiedUrl.toString()}`);

// Example of cleaning up query strings
const messyQueryUrl = URI("?&foo=bar&&foo=bar&foo=baz&").normalizeQuery();
console.log(`Cleaned Query: ${messyQueryUrl.toString()}`);

// Example of URI Templates
const expandedUri = URI.expand("/foo/{dir}/{file}", {
  dir: "bar",
  file: "world.html"
});
console.log(`Expanded URI: ${expandedUri}`);

view raw JSON →