Socket
Socket
Sign inDemoInstall

construct-url

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    construct-url

A lightweight library to construct URLs including query parameters, path, protocol, domain and hash.


Version published
Weekly downloads
0
Maintainers
1
Created
Weekly downloads
 

Readme

Source

construct-url

A lightweight NPM module to construct URLs including query parameters, path, protocol, domain and hash.

Installation

Installation using NPM:

$ npm i construct-url --save

Usage

Example usage:

const constructURL = require("construct-url");

// returns "https://example.com/page/details?option_a=test&option_b=test_two#main"
constructURL("http://example.com/", {
  queryParams: {
    option_a: "TEST",
    OPTION_B: "test_two"
  },
  path: "/page/details",
  lowercase: true,
  protocol: "https",
  hash: "main"
});
constructUrl(baseUrl, options);

Configuration Options

baseUrl

The base URL is the domain for the constructed URL and which all query parameters, the path and the hash is appended to.

options.queryParams

Query parameters should be a object passed in. Each key and value in the object will be appended to the URL as a query parameter. The key will be used as the query parameter and the value as the query value.

For example a query parameter object containing a key of "a" and a corresponding value of "b", would result in a query string of "?a=b".

options.path

Path parameter will be added to the URL as a path. The path parameter will avoid duplicate "/" in the path, by removing the last character of the base URL if it is a "/" and the first character of the path if it is a slash "/".

options.hash

The hash parameter will be used to add a hash symbol and the value to the end of a URL. For example, if a hash value of "main" is provided, the constructed URL will end in "#main".

options.lowercase

If the lowercase parameter has a value of true, the path, hash and query parameters will all be converted to lowercase.

options.protocol

If the protocol attribute is equal to "https" or "http", the constructed URL will start with the relevant protocol. If the base URL provided already has a different protocol, the protocol will be replaced. If the protocol attribute is equal to "none", the constructed URL will have no protocol, even if the base URL had a protocol.

License

MIT

Author

TomPrograms

Keywords

FAQs

Last updated on 01 Feb 2021

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc