# simplecheck

> Simple variable type checker modeled after Meteor's check function

Latest version **0.1.3** (published 2016-05-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install simplecheck
pnpm add simplecheck
yarn add simplecheck
bun add simplecheck
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2016-05-16 |
| First published | 2015-09-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4.0.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jason Raede |
| Maintainers | jperl, jraede |

## Links

- npm: https://www.npmjs.com/package/simplecheck
- npm.io page: https://npm.io/package/simplecheck

## Recent versions

- 0.1.3 (latest) — 2016-05-16
- 0.1.2 — 2016-01-27
- 0.1.1 — 2015-10-15
- 0.1.0 — 2015-10-15
- 0.0.4 — 2015-10-07
- 0.0.3 — 2015-09-29
- 0.0.2 — 2015-09-29
- 0.0.1 — 2015-09-29

## README

simplecheck
==========

This is a simple variable type-checking library inspired by the `check` and `Match` functions and behaviors in MeteorJS.


## Why?
I love Meteor's `check` function but you can't use it outside of Meteor due to the way it is written, and I want to use it in my pure node projects.

## Usage
### Import the library

```js
import {matches, oneOf, optional, ensure, anything} from 'simplecheck';
```

### Check a variable's type

```js
matches('foo', String); // true
```

### Use `oneOf`
```js
matches('foo', oneOf(String, Number)); // true

matches('foo', oneOf(Number, Boolean)); // false

matches('foo', oneOf('foo', 'bar')); // true

```



### Check an object's schema
```js
matches({
  foo:'bar',
  baz:10
}, {
  foo:String,
  baz:Number
}); // true

```

### Use the `optional` function for optional keys
(if they exist, they must match, but they aren't required)

```js
matches({
  foo:'bar',
  baz:10
}, {
  foo:String,
  baz:optional(Number)
}); // true

matches({
  foo:'bar'
}, {
  foo:String,
  baz:optional(Number)
}); // true

matches({
  foo:'bar',
  baz:'baz'
}, {
  foo:String,
  baz:optional(Number)
}); // false ("baz" is not a number)
```

### Check for array of type

Note that this library doesn't support type-checking of elements at specific indices in the array (yet)

```js
matches(['foo'], [String]); // true

matches(['foo', 10], [String]); // false

matches(['foo', 10], [oneOf(String, Number)]); // true
```

### Check on a regular expression
```js
matches('foo', /^foo$/g); // true

matches('foo', oneOf('bar', /fo/g)); // true

matches('foo', /bar/g); // false

matches({
  foo:'bar'
}, {
  foo:/ba/g
}); // true
```

### Accept `anything`
```js
matches('foo', anything); // true
matches({
  foo:'bar'
}, {
  foo:anything
}); // true

### Throw an error instead of returning a boolean?
Use `ensure`:

```js
ensure('foo', Number);

// "MatchError: Expected 'foo' to be a number"
```

## Find a bug?
Please add a test in `check_test.js` and we will try to fix.

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