Giter VIP home page Giter VIP logo

urlium's Introduction

urlium

Build most standard urls with minimal hassle and minus the string manipulation

Installation

// npm
npm install urlium

// pnpm
pnpm add urlium

// yarn
yarn add urlium
import { Builder } from 'urlium';

const url = 'https://github.com/{user}/{repo}';
const user = 'ikeohachidi';
const repo = 'url-builder';
const builder1 = Builder(url)
				.setParams({
					user,
					repo
				})
				.setQuery('lang', 'en')

console.log(builder.toString())
// expected output: https://github.com/ikeohachidi/url-builder?lang=en

Methods

toObject

Returns raw underlying object

addParam

Adds a parameter to the resulting url

const url = Builder('https://github.com')
			.addParam('hello', 'world');
			.toString();
// result: https://github.com/hello/world

setParams

Can update the value of multiple param placeholders

const url = Builder('https://github.com/{user}/{repo}')
			.setParams({
				user: 'ikeohachidi',
				repo: 'iono'
			})
			.toString();
// result: https://github.com/ikeohachidi/iono

setParam

Update the value of single param placeholder

const url = Builder('https://github.com/{user}/{repo}')
			.setParam('user', 'ikeohachidi');
			.toString()
// result: https://github.com/ikeohachidi

getParams

Returns all param placeholders and their values or returns an object with incrementing keys starting from 0 if placeholders aren't found. Can also optionally return a single param value

const url = Builder('https://github.com/ikeohachidi/iono')
			.getParams();
// result: {
// 	0: 'ikeohachidi',
// 	1: 'iono'
// }

const url2 = Builder('https:github.com/{user}/iono')
			.setParam('user', 'ikeohachidi')
			.getParams('user');
// result: 'ikeohachidi'

setQuery

Adds and updates query values

const url = Builder('https:github.com/ikeohachidi')
			.setQuery('country', 'nigeria')
			.setQuery('state', 'rivers');

// result: https://github.com/ikeohachidi?country=nigeria&state=rivers

getQuery

Retrieves the decoded value of a single query

const url = Builder('https:github.com/ikeohachidi?value=hello%20')
			.getQuery('value')

// result: hello world 

getRawQuery

Retrieves the value of single query as is

const url = Builder('https:github.com/ikeohachidi?value=hello%20world')
			.getQuery('value')

// result: hello%20world 

setHostName

Update the value of the hostname

const url = Builder('https://github.com')
			.setHostName('google.com')
			.toString();

// result: https://google.com

getHostName

Update the value of the url hostname

const url = Builder('https://github.com')
			.getHostName();

// result: github.com

setScheme

Update the value of the url scheme

const url = Builder('https://github.com')
			.setScheme('ws')
			.toString();

// result: ws://github.com

getScheme

Update the value of the url scheme

const url = Builder('https://github.com')
			.getScheme()

// result: https 

toString

Return built url string

const url = Builder('http://google.com')
			.setScheme('https')
			.setHostName('github')
			.toString()

// result: https://github.com

urlium's People

Contributors

ikeohachidi avatar

Stargazers

Roman avatar Shrihari avatar Ilya avatar

Watchers

James Cloos avatar  avatar  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.