JSPM

mod-url

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q28858F
  • License MIT

Modify internals of URL, fast & easy.

Package Exports

  • mod-url

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mod-url) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

mod-url

Build Status DeepScan grade

npm i mod-url
const mod = require('mod-url');

console.log(mod.parse('github.com').done());
// https://github.com/

console.log(mod.parse('http://google.com')
  .protocol('https')
  .subdomain('www')
  .domainext('in')
  .path('search?q=query')
  .fragment('foo')
  .done());
// https://www.google.in/search?q=query#foo

API

.parse(url) -> parse url.

example of accepted url types : github, github.com, www.github.com, http://www.github.com, https://github.com, github.com/path

const gh = mod.parse('github');

.done() -> get modified string.

console.log(gh.done());
// https://github.com/

.protocol(string) -> modify protocol scheme.

let url = mod.parse('github');
url = url.protocol('http');

// or
// url = mod.parse('github').protocol('http').done();

console.log(url.done());
// http://github.com/

.subdomain(string) -> modify subdomain.

const url = mod.parse('github').subdomain('www').done();

console.log(url);
// https://www.github.com/
const url = mod.parse('www.github.com').subdomain('x.y').done();

console.log(url);
// https://x.y.github.com/

To remove subdomain. Do not pass null argument. Instead pass empty string

const url = mod.parse('www.github.com').subdomain('').done();

console.log(url);
// https://github.com/

.domain(string) -> modify domain.

const url = mod.parse('yahoo.com/search?q=query').domain('google').done();

console.log(url);
// https://google.com/search?q=query
const url = mod.parse('google.com').domain('www.google.in').done();

console.log(url);
// https://www.google.in/
const url = mod.parse('google').domain('google.in').done();

console.log(url);
// https://google.in/

You cannot remove domain. ie .domain('') wont work

.domainext(string) -> modify domain extension

const url = mod.parse('google.com/search?q=query').domainext('uk').done();

console.log(url);
// https://google.uk/search?q=query

.port(string | number) -> modify port

const url = mod.parse('google.com').port(80).done();

console.log(url);
// https://google:80.com/

.path(string) -> modify full path

const url = mod.parse('google.com/path/to?q=q1').path('onlypath').done();

console.log(url);
// https://google.com/onlypath
const url = mod.parse('github.com/daunting/long/path/not/required/to/you').path('').done();

console.log(url);
// https://github.com/

.onlypath(string) -> modify path (excluding query & fragment)

const url = mod.parse('google.com/version1?q=query').onlypath('version2').done();

console.log(url);
// https://google.com/version2?q=query

.query(string) -> modify query

const url = mod.parse('google.com/search?q=query').query('foo=bar&baz=foo').done();

console.log(url);
// https://google.com/seach?foo=bar&baz=foo

.fragment(string) -> modify fragment

const url = mod.parse('google.com/hi').fragment('#hello').done();

console.log(url);
// https://google.com/hi#hello

chain modify functions as you want

const url = mod.parse('something').path('/random?query=unknown').protocol('http').subdomain('www').domainext('foo').port('90').domain('something-else').done();

console.log(url);
// http://www.something-else:90.foo/random?query=unknown

Do not forget to add .done() at last

DO NOT - (mod-url is mutable)

const url1 = mod.parse('google');

const url2 = mod.parse('github');

console.log(url1.done());
// https://github.com

console.log(url2.done());
// https://github.com
// solution for above behavior
const url1 = mod.parse('google').done();
const url2 = mod.parse('github').done();

console.log(url1);
console.log(url2);