Skip to content

A URL builder library for JavaScript, TypeScript

License

Notifications You must be signed in to change notification settings

imjuni/url-naong

Repository files navigation

url-naong

Download Status Github Star Github Issues NPM version License url-naong

url-naong is a simple url builder. The URL class is great, but it doesn't allow you to apply variables to pathnames and arrays to querystrings. This is where url-naong comes in handy. url-naong is url builder that is inspired by urlcat

naong is korean name of Meowth(pokemon monster)

install

npm install url-naong --save

Usage

urlna function take 4 parameters.

  1. (required) host
  2. (required) pathname
  3. (optional) path parameters and querystring
  4. (optional) options of the qs library

Variable in pathname

const urlna = require('url-naong').urlna;

const url = urlna(undefined, '/hero/:hid/comic/:cid/name/:name', 
  { 
    hid: 1, // evaluate in pathname
    cid: 13, // evaluate in pathname
    name: 'ironman', // evaluate in pathname
    tid: '72368bb0-a6cc-4fa1-9288-c903fd45c333' // evaluate in querystring
  }
);

console.log(url);

// created> '/hero/1/comic/13/name/ironman?tid=72368bb0-a6cc-4fa1-9288-c903fd45c333'

Querystring

const urlna = require('url-naong').urlna;

const url = urlna(undefined, '/hero/:hid/comic/:cid/name/:name', 
  { 
    hid: 1, // evaluate in pathname
    cid: 13, // evaluate in pathname
    name: 'ironman', // evaluate in pathname
    tid: '72368bb0-a6cc-4fa1-9288-c903fd45c333' // evaluate in querystring
  }
);

console.log(url);

// created> '/hero/1/comic/13/name/ironman?tid=72368bb0-a6cc-4fa1-9288-c903fd45c333'

TypeScript

import { urlna } from 'url-naong';

const url = urlna(undefined, '/hero/:hid/comic/:cid/name/:name', 
  { name: 'ironman', hid: 1, cid: 13, tid: '72368bb0-a6cc-4fa1-9288-c903fd45c333', }
);

console.log(url);

// created> '/hero/1/comic/13/name/ironman?tid=72368bb0-a6cc-4fa1-9288-c903fd45c333'

Array of querystring

url-naong using qs for stringify querystring. Section Stringifying how to stringify array in querystring.

const urlna = require('url-naong').default;

 const url = urlna(
  undefined, '/hero/:hid/comic/:cid/name/:name',
  {
    name: 'ironman',
    hid: 1,
    cid: 13,
    tid: '72368bb0-a6cc-4fa1-9288-c903fd45c333',
    category: ['marvel', 'advengers', 'S.H.I.E.L.D'],
  },
  {
    arrayFormat: 'repeat',
  },
);

// created> '/hero/1/comic/13/name/ironman?tid=72368bb0-a6cc-4fa1-9288-c903fd45c333&category=marvel&category=advengers&category=S.H.I.E.L.D',