1.0.5 • Published 7 years ago

rich-param v1.0.5

Weekly downloads
3,117
License
MIT
Repository
github
Last release
7 years ago

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

OptionTypeDefault valueDescription
defaultMixedDefault param value
normalizeBooleanfalseFormats Hey, jÉssiCa! to hey jessica
lowercaseBooleanfalseFormats HeLLo! to hello!
uppercaseBooleanfalseFormats HeLLo! to HELLO!
trimBooleantrueFormats HeLLo ! to HeLLo !
requiredBooleanfalseInvalidates empty values
minNumberInvalidates values lower than it
maxNumberInvalidates values greater than it
minlengthNumberInvalidates values with length lower than it
maxlengthNumberInvalidates values with length greater than it
enumArrayInvalidates values which isn't in it
matchRegExpInvalidates values which doesn't pass the RegExp
multipleBooleanfalseMake the value an array
separatorString,String to be found in the value to split into an array (if multiple = true)
formatFunction(value, param) => valueFunction to be called with param.value()
validateFunction(value, param) => ({ valid: true })Function to be called with param.validate()

License

MIT © Diego Haz