JSPM

assert-tiny

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q56088F
  • License MIT

assert-tiny

Package Exports

  • assert-tiny
  • assert-tiny/es/index.js
  • assert-tiny/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (assert-tiny) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Assert-Tiny

language: EN

快速开始

  1. 安装
npm install assert-tiny

yarn add assert-tiny
  1. 快速使用
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 - 断言

  1. isEqual(except, msg ) - 严格等于
    比较方式:a === b
import val from 'assert-tiny'

val({}).isEqual({})   // false, will throw error
val(NaN).isEqual(NaN) // true
  1. 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
  1. 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'
  1. 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'
  1. 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'
  1. 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'
  1. 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]'
  1. 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']'
  1. 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 - 扩展

自定义断言方法

  1. 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