# assert-tiny

> assert-tiny

Latest version **1.0.2** (published 2022-03-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install assert-tiny
pnpm add assert-tiny
yarn add assert-tiny
bun add assert-tiny
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2022-03-12 |
| First published | 2021-12-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 38.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | https://github.com/zhao-huo-long |
| Maintainers | zhao-huo-long |
| Keywords | assert, value, test, tiny-assert, type |

## Links

- npm: https://www.npmjs.com/package/assert-tiny
- Repository: https://github.com/zhao-huo-long/assert-tiny
- Homepage: https://github.com/zhao-huo-long/assert-tiny#readme
- Issues: https://github.com/zhao-huo-long/assert-tiny/issues
- npm.io page: https://npm.io/package/assert-tiny

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2022-03-12
- 1.0.1 — 2021-12-28
- 1.0.0 — 2021-12-20

## README

# Assert-Tiny
language: [EN](./README_en.md)

#### 快速开始
1. 安装
```shell
npm install assert-tiny

yarn add assert-tiny
```
2. 快速使用
```ts
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` 
```ts
import val from 'assert-tiny'

val({}).isEqual({})   // false, will throw error
val(NaN).isEqual(NaN) // true
```

2. `isShallowEq(except, msg )`  - 浅等于  
```ts
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
```

3. `isOk(msg)`  - 断言if判断为false的值  
将会断言错误的值: NaN, 0,false, null, undefined,'' & ""
```ts
import val from 'assert-tiny'
val({}).isOk('ok') // true
val('').isOk('not ok') //  false, will throw error with message 'not ok'
```

4. `isInt(msg)` - 断言整数  
if value is not a int number, will throw error with message
```ts
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'
```
5. `isPositiveInt(msg)` - 断言正整数  
if value is not a postitive int number, will throw error with message
```ts
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'
```
6. `isTypeOf(constructor, msg)` - 断言类型  
```ts
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'
```
7. `isInRange(Range, msg)` - 断言数字范围  
Range is a Array, [minNumber, maxNumber]
```ts
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]'
```

8. `isOneOf(Array, msg)` - 断言是否存在数组中  
```ts
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']'
```

9. `isTruth(msg)` - 断言真值  
error value: null, undefined, false, NaN
```ts
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`将取反真为假，假为真。  

```ts
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打印错误
```ts
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
```ts
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
```

---
_Source: https://npm.io/package/assert-tiny · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
