assert-tiny
v1.0.2
Published
assert-tiny
Downloads
42
Maintainers
Readme
Assert-Tiny
language: EN
快速开始
- 安装
npm install assert-tiny
yarn add assert-tiny
- 快速使用
import val from 'assert-tiny'
val(true).isEqual( true, 'ok' ) // do nothing
val('').isOk('is not ok') // will throw a error with message 'is not ok'
val(3).oneOf( [1, 2], '3 is not int [1,2]' ) // will throw a error with message '3 is not int [1,2]'
Assert - 断言
isEqual(except, msg )
- 严格等于
比较方式:a === b
import val from 'assert-tiny'
val({}).isEqual({}) // false, will throw error
val(NaN).isEqual(NaN) // true
isShallowEq(except, msg )
- 浅等于
import val from 'assert-tiny'
val({}).isShallowEq({}) // true
val({a: 3}).isShallowEq({a: 4}) // false, will throw error
val([1, {a: 2, c: [3]}]).isShallowEq([1, {a: 2, c:[3]}]) // true
isOk(msg)
- 断言if判断为false的值
将会断言错误的值: NaN, 0,false, null, undefined,'' & ""
import val from 'assert-tiny'
val({}).isOk('ok') // true
val('').isOk('not ok') // false, will throw error with message 'not ok'
isInt(msg)
- 断言整数
if value is not a int number, will throw error with message
import val from 'assert-tiny'
val(1).isInt('1 is not a int') // true
val(-1).isInt('-1 is not a int') // true
val(2.2).isInt('2.2 not a int') // false, will throw error with message '2.2 not a int'
isPositiveInt(msg)
- 断言正整数
if value is not a postitive int number, will throw error with message
import val from 'assert-tiny'
val(1).isPositiveInt('1 is not a postitive int') // true
val(-1).isInt('-1 is not a postitive int') // false, will throw error with message '-1 not a postitive int'
val(2.2).isInt('2.2 not a postitive int') // false, will throw error with message '2.2 not a postitive int'
isTypeOf(constructor, msg)
- 断言类型
import val from 'assert-tiny'
val('').isTypeOf(String) // true
val(2).isTypeOf(Number) // true
val({}).isTypeOf(Object) // true
val('').isTypeOf(Object, 'ecpect Object') // false, will throw error with message 'ecpect Object'
isInRange(Range, msg)
- 断言数字范围
Range is a Array, [minNumber, maxNumber]
import val from 'assert-tiny'
val(2).isInRange([3, 10], '2 is not in [3, 10]') // false, will throw error with message '2 is not in [3, 10]'
isOneOf(Array, msg)
- 断言是否存在数组中
import val from 'assert-tiny'
val(NaN).isOneOf([0, NaN, 's']) // true
val('test1').isOneOf(['test']) `test1 is not in ['test']`) // false, will throw error with message `test1 is not in ['test']'
isTruth(msg)
- 断言真值
error value: null, undefined, false, NaN
import val from 'assert-tiny'
val(NaN).isTruth('NaN is not a truth value') // false, will throw error with message `test1 is not in `NaN is not a truth val`'
val(null).isTruth('null is not a truth value') // false, will throw error
Not - 逻辑取反属性
.not
将取反真为假,假为真。
import val from 'assert-tiny'
val(NaN).isTruth('NaN is not a truth value') // false, will throw error with message `test1 is not in `NaN is not a truth val`'
val(NaN).not.isTruTh('value is a truth value') // true, nothing
val(null).isTruth('null is not a truth value') // false, will throw error
val(null).not.isTruTh(`value is a truth value`) // true, nothing
Slient - 静默属性
.slient
将不会抛出错误,而是console.warn打印错误
import val from 'assert-tiny'
val(NaN).slient.isTruth('NaN is not a truth value') // false, will console.warn(error) with message `test1 is not in `NaN is not a truth val`'
val(1).slient.isTruth('null is not a truth value') // false, will console.warn(error) with message `null is not a truth value`'
Extends - 扩展
自定义断言方法
- create
import { create } from 'assert-tiny'
const val = create({
/* 会生成自动生成`isArray(msg)`方法 */
array: (value) => {
if(Array.isArray(value)) return true
return false
}
/* 会生成自动生成`isMoreThan(except, msg)`方法*/
moreThan(value, except){
if(value > except) return true
return false
}
})
val(1).isArray('1 is not a array' ) // false, will throw error with message `1 is not a array`
val(2).isMoreThan(3, '2 is not more than 3') // false, will throw error with message `1 is not a array`
val(4).isMoreThan(3, 'ok') // true