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

rich-param

v1.0.5

Published

An object with name and value which accepts pluggable methods as formatters or validators.

Downloads

13,869

Readme

rich-param

NPM version Build Status Coveralls Status Dependency Status Downloads

An object with name and value which accepts pluggable methods as formatters or validators.

Install

npm install rich-param

Usage

Basic

import Param from 'rich-param'

// new Param(name, value, options)
const param = new Param('firstName', 'Jessica', { uppercase: true })

console.log(param.name) // firstName
console.log(param.value()) // JESSICA

Setting and getting value

console.log(param.value('pRIsCila')) // PRISCILA

Setting and getting options

See List of default options

console.log(param.option('uppercase')) // true
console.log(param.option('uppercase', false)) // false

console.log(param.value('prisciLa')) // prisciLa

Formatting the value

param.option('format', (value, param) => `${param.name}: ${value}`)

console.log(param.value('PRISCILA')) // firstName: PRISCILA

Validating the value

param.option('validate', (value, param) => ({
  valid: value === 'Jessica',
  message: 'Jessica is the only valid name'
}))

param.value('Jessica')
console.log(param.validate()) // true

param.value('Priscila')
console.log(param.validate()) // false

// or get the error object
param.validate((err) => {
  console.log(err)
  /* {
    valid: false,
    name: 'validate',
    param: 'firstName',
    value: 'Priscila',
    message: 'Jessica is the only valid name'
  } */
})

Validating using the built-in validators

See List of default options

param.option('required', true)
param.value('')

console.log(param.validate()) // false

// or get the error object
param.validate((err) => {
  console.log(err)
  /* {
    valid: false,
    name: 'required',
    required: true,
    param: 'firstName',
    value: '',
    message: 'firstName is required'
  } */
})

Creating a custom formatter

param.formatter('shout', (shout, value, param) => {
  if (shout && value) {
    value = value.toUpperCase() + '!'
  }
  return value
})

console.log(param.value('Jessica')) // Jessica

param.option('shout', true)

console.log(param.value('Jessica')) // JESSICA!

Creating a custom validator

param.validator('mustBeAShout', (mustBeAShout, value, param) => ({
  valid: !mustBeAShout || !value && value.toUpperCase() === value && value.indexOf('!') !== -1,
  message: 'THE VALUE MUST BE A SHOUT!'
}))

param.option('shout', false)

console.log(param.value('Jessica')) // Jessica
console.log(param.validate()) // true

param.option('mustBeAShout', true)

console.log(param.validate()) // false

param.option('shout', true)

console.log(param.value('Jessica')) // JESSICA!
console.log(param.validate()) // true

Reference

List of default options

Option | Type | Default value | Description -------|------|---------------|------------- default | Mixed | | Default param value normalize | Boolean | false | Formats Hey, jÉssiCa! to hey jessica lowercase | Boolean | false | Formats HeLLo! to hello! uppercase | Boolean | false | Formats HeLLo! to HELLO! trim | Boolean | true | Formats HeLLo ! to HeLLo ! required | Boolean | false | Invalidates empty values min | Number | | Invalidates values lower than it max | Number | | Invalidates values greater than it minlength | Number | | Invalidates values with length lower than it maxlength | Number | | Invalidates values with length greater than it enum | Array | | Invalidates values which isn't in it match | RegExp | | Invalidates values which doesn't pass the RegExp multiple | Boolean | false | Make the value an array separator | String | , | String to be found in the value to split into an array (if multiple = true) format | Function | (value, param) => value | Function to be called with param.value() validate | Function | (value, param) => ({ valid: true }) | Function to be called with param.validate()

License

MIT © Diego Haz