checkin
v0.0.1
Published
Lint option and merge with default value
Downloads
15
Readme
checkin
Lint option and merge with default value
Getting Started
$ 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' }
Rules
coerce
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 }
default (alias: def)
Return a default value when the key is undefined.
const obj = {}
const lint = {
foo: {
default: 1
}
}
const expected = checkin(obj, lint)
// => { foo: 1 }
equal
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
oneOf
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 ]
required
Check if the parameter is defined.
const obj ={ foo: 1 }
const lint = {
foo: {
required: true
}
}
const expected = checkin(obj, lint)
// => { foo: 1 }
satisfy
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 }
typeOf
const obj ={ foo: 1 }
const lint = {
foo: {
typeOf: ['string', 'number']
}
}
const expected = checkin(obj, lint)
// => { foo: 1 }
within
const obj ={ foo: 1 }
const lint = {
foo: {
within: [0, 2]
}
}
const expected = checkin(obj, lint)
// => { foo: 1 }
Test
$ npm i && npm test
License
MIT