An object with name and value which accepts pluggable methods as formatters or validators.
npm install rich-param
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
console.log(param.value('pRIsCila')) // PRISCILA
console.log(param.option('uppercase')) // true
console.log(param.option('uppercase', false)) // false
console.log(param.value('prisciLa')) // prisciLa
param.option('format', (value, param) => `${param.name}: ${value}`)
console.log(param.value('PRISCILA')) // firstName: PRISCILA
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'
} */
})
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'
} */
})
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!
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
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() |
MIT © Diego Haz