GithubHelp home page GithubHelp logo

yenhub / immurl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tom-sherman/immurl

0.0 0.0 0.0 699 KB

๐Ÿ”— A tiny immutable URL library, backed by the native whatwg URL.

Home Page: https://immurl.netlify.app/

License: MIT License

JavaScript 1.24% TypeScript 98.76%

immurl's Introduction

immurl

๐Ÿ”— A tiny (< 500B), 0-dependency, immutable URL library, backed by the native whatwg URL.

Install

npm install immurl

Because immurl uses the native whatwg URL API under the hood you'll need a polyfill to support environments that don't implment this API eg. IE11.

Usage

ImmutableURL

ImmutableURL works as you expect, it contains all of the properties of the native URL API.

import { ImmutableURL } from 'immurl';

const url = new ImmutableURL('https://example.com');

console.log(url.href); // 'https://example.com'

// Set properties with the .set() method

let newUrl = url.set('pathname', '/login');

// Because the set API is immutable you can chain calls to .set()

newUrl = url.set('pathname', '/bar').set('hash', '#heading'); // https://example.com/bar#heading

ImmutableURLSearchParams

immurl also contains an immutable version of the URLSearchParams API; ImmutableURLSearchParams.

The API for ImmutableURLSearchParams is exactly the same as the native version except the methods that usually mutate (.append(), .delete(), .sort()) return a new ImmutableURLSearchParams instance.

import { ImmutableURLSearchParams } from 'immurl';

let params = new ImmutableURLSearchParams('q=URLUtils.searchParams&topic=api');

params = params.append('foo', 'bar').delete('q'); // topic=api&foo=bar

The searchParams property of ImmutableURL returns an ImmutableURLSearchParams.

const url = new ImmutableURL('https://example.com?foo=bar');

const newParams = url.searchParams
  .append('q', 'search-term')
  .set('foo', 'fuz')
  .sort();

// url.searchParams is unaffected (thanks to immutability ๐ŸŽ‰)

// We can pass our newParams into url.set() to create a new url with the updated params
const newUrl = url.set('searchParams', newParams);

// The following code is equvalent to the above

const newUrl2 = url.set(
  'searchParams',
  url.searchParams.append('q', 'search-term').set('foo', 'fuz').sort()
);

API

See the docs at https://immurl.netlify.app/

immurl's People

Contributors

damiensedgwick avatar dependabot[bot] avatar tom-sherman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.