npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

assert-tiny

v1.0.2

Published

assert-tiny

Downloads

42

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