Lint option and merge with default value
$ npm install --save-dev checkin
import checkin from 'checkin'
const option = {
filename: 'test.png',
size: '20',
tag: 'icon'
}
const lint = {
filename: {
typeOf: 'string',
isRequire: true
},
size: {
typeOf: 'number',
coerce: val => Number(val),
within: [1, 20]
},
tag: {
def: 'icon',
oneOf: ['icon', 'human']
},
color: {
def: 'red',
typeOf: ['string', 'number']
}
}
const expected = checkin(option, lint)
//=> { filename: 'test.png', size: 20, size: 20, color: 'red' }
Return a new value by given function.
const obj = { foo: 1 }
const lint = {
foo: {
coerce: val => val + 1
}
}
const expected = checkin(obj, lint)
// => { foo: 2 }
Return a default value when the key is undefined.
const obj = {}
const lint = {
foo: {
default: 1
}
}
const expected = checkin(obj, lint)
// => { foo: 1 }
Check by deep equal.
const obj ={ foo: 1 }
const lint = {
foo: {
equal: 1
}
}
const expected = checkin(obj, lint)
// => { foo: 1 }
const obj ={ foo: 1 }
const lint = {
foo: {
equal: 2
}
}
const expected = checkin(obj, lint)
// => throw AssertionError: expected foo to to be equal to 2
const obj ={ foo: 1 }
const lint = {
foo: {
oneOf: [1, 2]
}
}
const expected = checkin(obj, lint)
// => { foo: 1 }
const obj ={ foo: 1 }
const lint = {
foo: {
oneOf: [3]
}
}
const expected = checkin(obj, lint)
// => throw AssertionError: expected foo to to be one of Array [ 3 ]
Check if the parameter is defined.
const obj ={ foo: 1 }
const lint = {
foo: {
required: true
}
}
const expected = checkin(obj, lint)
// => { foo: 1 }
Check if the parameter is satifsy to the given function.
const obj ={ foo: 1 }
const lint = {
foo: {
satisfy: val => val > 0
}
}
const expected = checkin(obj, lint)
// => { foo: 1 }
const obj ={ foo: 1 }
const lint = {
foo: {
typeOf: ['string', 'number']
}
}
const expected = checkin(obj, lint)
// => { foo: 1 }
const obj ={ foo: 1 }
const lint = {
foo: {
within: [0, 2]
}
}
const expected = checkin(obj, lint)
// => { foo: 1 }
$ npm i && npm test
MIT