Socket
Book a DemoInstallSign in
Socket

url-modify

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

url-modify

Modify URL instances cleanly and safely. Works in any environment that supports the URL API.

latest
Source
npmnpm
Version
0.0.5
Version published
Maintainers
1
Created
Source

url-modify

Modify URL instances cleanly and safely. Works in any environment that supports the URL API.

Installation

yarn add url-modify
# Or if you are using NPM:
# npm install --save url-modify

Usage

urlModify(modifications: UrlModifyModifications, base: string | URL, options?: UrlModifyOptions): URL

UrlModifyModifications

{
  hash?: string;
  host?: string;
  password?: string;
  path?: string;
  port?: string;
  protocol?: string;
  search?: string | string[][] | Record<string, string> | URLSearchParams;
  username?: string;
}

UrlModifyOptions

{
  pathBehavior?: 'append' | 'prepend' | 'replace';
  searchBehavior?: 'append' | 'clear' | 'replace';
}

pathBehavior

  • append

    Appends modifications.path to the base URL path. Note that the base URL path is not tampered with; it may or may not end in a trailing slash depending how you provided it.

  • prepend

    Prepend modifications.path to the base URL path. Note that the leading slash from the base URL path is removed, so you may need to ensure modifications.path ends with a slash.

  • replace (default)

    Replace the base URL path with modifications.path.

searchBehavior

  • append

    If a search key provided in modifications.search already exists in the base URL then retain it and append the values provided in modifications.search to the search string.

  • clear

    Clear all search params provided in the base URL before appending the values provided in modifications.search to the search string.

  • replace (default)

    If a search key provided in modifications.search already exists in the base URL then remove it before appending the values provided in modifications.search to the search string.

Examples

import { urlModify } from 'url-modify';
// Or if you are using CommonJS:
// const { urlModify } = require('url-modify');

const base = new URL('https://foo.example?foo=bar');

console.log(urlModify({ protocol: 'wss' }, base).toString())
// "wss://foo.example/?foo=bar"

console.log(urlModify({ search: { foo: 'bar2' } }, base).toString())
// "https://foo.example/?foo=bar2"

console.log(urlModify({ search: { foo: 'bar2' } }, base, { searchBehavior: 'append' }).toString())
// "https://foo.example/?foo=bar&foo=bar2"

console.log(urlModify({ search: [ [ 'foo', 'bar2' ], [ 'foo', 'bar3' ] ] }, base, { searchBehavior: 'append' }).toString())
// "https://foo.example/?foo=bar&foo=bar2&foo=bar3"

console.log(urlModify({ search: [ [ 'foo', 'bar2' ], [ 'foo', 'bar3' ] ] }, base, { searchBehavior: 'replace' }).toString())
// "https://foo.example/?foo=bar2&foo=bar3"

console.log(urlModify({ search: { abc: '123' } }, base).toString())
// "https://foo.example/?foo=bar&abc=123"

console.log(urlModify({ search: { abc: '123' } }, base, { searchBehavior: 'clear' }).toString())
// "https://foo.example/?abc=123"

Rationale

While the URL API simplifies creating URL strings, it's difficult to cleanly mutate URL instances without creating excessive variables and/or risk modifying the original URL instance.

Until the spec allows you to do something like...

const base = new URL('http://foo.example');
new URL({ protocol: 'https' }, base);

...or...

const base = new URL('http://foo.example');
new URL({ ...base, protocol: 'https' });

...then this package is a helpful workaround.

Keywords

extend

FAQs

Package last updated on 27 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts