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 🙏

© 2026 – Pkg Stats / Ryan Hefner

param-parser

v1.0.5

Published

parse and validate parameter

Readme

param-parser

A service that can validate, parse and format parameters for you by just calling one function.

Installation

Install package from npm.

$ npm install param-parser --save

Usage

  • param-parser module provides parse() function to validate, parse and format parameter
  • parse() function expects these arguments
    • param {object}: parameters to be parsed
    • specs {object[]}: array of spec (see specs section below)
    • defaultValue {object}: default value for some parameters
  • parse() function return parsed parameters {object}

Example

const parser = require('param-parser')

// define specs
const specs = {
    mobile_no: [
        'required' /* required field */,
        /^\d{10}$/ /* regex */
    ],
    gender: [
        'required' /* required field */, 
        /^(M|F|m|f)$/ /* regex */, 
        String.prototype.toLowerCase /* format function */
    ],
    language: [] /* no 'required' means optional field */,
    amount: [
      parseFloat /* format function */
    ]
}

// input parameters
const input = {
    mobile_no: '1234567890',
    gender: 'M'
}

// define default value here
const defaultValue = { 
  language: 'EN' /* if no lanauge, set to 'EN' as a default value */,
  amount: 0 /* if no amount, set to 0 as a default value */
}

const param = parser.parse(input, specs, defaultValue) // parse input

/** will print
  * {
  *     mobile_no: '1234567890',
  *     gender: 'm', // format to lower case
  *     lanauge: 'EN',
  *     amount: 0
  * }
  */
console.log(param)

specs

A specs is an array of each spec.

  • to define 'required' field, add required to spec, e.g.,
const specs = {
    mobile_no: [ 'required' ] /* required field */,
    gender: [] /* optional field */
}
  • regex is supported to validate each parameter, e.g.,
const specs = {
    mobile_no: [ 'required',  /^\d{10}$/ ] /* validate with regex */,
    gender: [ /^(M|F|m|f)$/ ] /* validate with regex */,
    language: [] /* validate without regex */
}
  • support native format function, e.g.,
const specs = {
    gender: [ 'required', /^(M|F|m|f)$/, String.prototype.toLowerCase ] /* format value to lower case */,
    language: [ String.prototype.toUpperCase ] /* format value to upper case */,
    amount: [ parseFloat ] /* format value to float */,
    name: [ 'required', String.prototype.toLowerCase, String.prototype.trim ] /* format toLowerCase and then trim */
}
  • support custom format function, e.g.,
function formatMoney(num) {
    return `${num} dollar`
}

const specs = {
    amount: [ formatMoney ] /* format using formatMoney function */
}
  • parameter that is not in the specs will be removed, e.g.,
// define specs
const specs = {
    mobile_no: [ 'required', /^\d{10}$/ ],
    gender: [ 'required' ],
    language: [],
}

// input parameters
const input = {
    mobile_no: '1234567890',
    gender: 'M',
    lanauge: 'EN',
    dummy1: 'dummy1',
    dummy2: 'dummy2'
}

const param = parser.parse(input, specs) // parse input

/** will print
  * {
  *     mobile_no: '1234567890',
  *     gender: 'M',
  *     lanauge: 'EN'
  * }
  * 
  * note that dummy1 and dummy2 are removed
  * because they are not defined in specs
  */
console.log(param)
  • support array parameter, e.g.,
const regex = /^(YES|NO)$/
const regexpItems = new parser.RegExpItems(regex)
const specs = {
    status: [ 'required' ],
    result: [ 'required', regexpItems ]
}
const input = {
    status: true,
    result: ['YES', 'YES', 'NO']
}

const param = parser.parse(input, specs)

/** will print
  * {
  *   status: true,
  *   result: ['YES', 'YES', 'NO']
  *  }
  */

License

MIT