npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

url-templater

v1.0.0

Published

create url that you want to use with different data by UrlTemplater

Downloads

15

Readme

url-template

Create url that you want to use with different data.

Installation

$ npm install url-templater --save

Usage

Basic

import the UrlTemplater class, and new a UrlTemplater instance by passing a string parameter, then use resolve method to get the rendered url string.

example:

const UrlTemplater = require('url-templater')

const url = new UrlTemplater('http://localhost:8080/api/name/:name')
url.resolve({
    params: {
        name: 'url-templater'
    },
    query: {
        json: 'true'
    }
})
// http://localhost:8080/api/name/url-templater?json=true

resolve method receive a object include params and query propertise.

The params property resolve the path parameter( like :name), and the query property resolve the url's search part.

params support string and function,

if param property's value is a function, url-templater will use the funciton return value as the property value.

new UrlTemplater('http://localhost:8080/api/name/:name').resolve({
    params: {
        name: function () {
            return 'a'
        }
    }
})
// http://localhost:8080/api/name/a

query support several typeof properties:

  • string use string as the final value type
  • function use function's return value as property value
  • Array url-templater will resolve array as multiple value, the key will be transform to a formatted key like key[index]
  • object when query's property is a object, it will combined current key with previous key like prevKey.currKey

example:

new UrlTemplater('http://localhost:8080/api/query').resolve({
    query: {
        projectInfo: {
            name: 'url-templater',
            teamMember: ['programmer', 'tester'],
            birthday: function () {
                let birthday = new Date(2017, 7, 10)
                return `${birthday.getFullYear()}-${birthday.getMonth() + 1}-${birthday.getDate()}`
            },
            modules: {
                Url: 'parse and format effect',
                UrlTemplater: 'url template class'
            }
        }
    }
})
// http://localhost:8080/api/query?projectInfo.name=url-templater&projectInfo.teamMember[0]=programmer&projectInfo.teamMember[1]=tester&projectInfo.birthday=2017-8-10&projectInfo.modules.Url=parse and format effect&projectInfo.modules.UrlTemplater=url template class

Advanced

if you want to use you own habits to replace the default configuration (such as the character that used to combine object's key), you can add a options object parameter in UrlTemplater's constructor.

example:

const templater = new UrlTemplater('http://localhost:8080/api/name/{{name}}/query', {
    // the character combine with object's key
    objCombine: '#',
    // the characters insert before and after array's index key
    arrCombine: ['{', '}'],
    // the regular expression that used to replace url parameters with real value
    paramsRule: /{{(\w+)}}/g
})
templater.resolve({
    params: {
        name: 'url-templater'
    },
    query: {
        array: ['a', 'b', 'c'],
        modules: {
            Url: 'parse and format effect'
        }
    }
})
// http://localhost:8080/api/name/url-templater/query?array{0}=a&array{1}=b&array{2}=c&modules#Url=parse and format effect

Run Tests

npm run test