# @mojule/is

> Duck typing utility

Latest version **0.3.9** (published 2018-10-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mojule/is
pnpm add @mojule/is
yarn add @mojule/is
bun add @mojule/is
```

## Health

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

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

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.9 |
| Published | 2018-10-03 |
| First published | 2017-03-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 23 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Nik Coughlin |
| Maintainers | andybell, nrkn |

## Links

- npm: https://www.npmjs.com/package/@mojule/is
- Repository: https://github.com/mojule/mojule
- Homepage: https://github.com/mojule/mojule/packages/is#readme
- Issues: https://github.com/mojule/mojule/issues
- npm.io page: https://npm.io/package/@mojule/is

## Recent versions

- 0.3.9 (latest) — 2018-10-03
- 0.3.8 — 2018-05-23
- 0.3.7 — 2018-05-17
- 0.3.6 — 2018-04-04
- 0.3.5 — 2018-04-03
- 0.3.4 — 2018-04-03
- 0.3.3 — 2018-04-03
- 0.3.2 — 2018-04-03
- 0.3.1 — 2018-03-29
- 0.3.0 — 2018-03-29
- 0.2.5 — 2018-02-06
- 0.2.4 — 2018-02-06
- 0.2.3 — 2018-02-06
- 0.2.2 — 2018-01-23
- 0.2.1 — 2017-11-12
- … 13 more at https://npm.io/package/@mojule/is/versions

## README

# is

A type checking facade that doesn't care how you check types - use duck typing,
use JSON schema, whatever.

We needed a consistent interface for type checking, sometimes we use duck
typing, sometimes we use JSON schema, regardless of which, we tend to follow the
same general pattern, __is__ codifies this.

```javascript
is.string( '' ) // true
is.string( 42 ) // false
```

## usage

`npm install @mojule/is`

### Using default predicates:

```javascript
const { is } = require( '@mojule/is' )

// These all return true
is.number( 1.1 )
is.integer( 1.0 )
is.string( '' )
is.boolean( true )
is.array( [] )
is.null( null )
is.undefined( undefined )
is.function( i => i + 1 )
is.object({})
is.empty({})
```

The [default predicates](src/is.ts) are moderately opinionated, e.g. they don't
consider `null` or `[]` to be `is.object`

### With custom predicates

Here we define additional predicates which extend the default predicates.

```javascript
const { is, extendDefaults } = require( '@mojule/is' )

const predicates = {
  domNode: subject =>
    is.object( subject ) && is.string( subject.nodeName ),
  div: subject =>
    predicates.domNode( subject ) && subject.nodeName.toLowerCase() === 'div'
}

const domIs = extendDefaults( predicates )

// using the default
domIs.string( '' )

const div = document.querySelector( 'div' )

// These return true
domIs.domNode( div )
domIs.div( div )
```


### Additional functions

In addition to testing against single predicates by name, as above, we provide
these additional utility functions, `isType`, `isOnly`, `some`, `every`, `of`,
`allOf`.

```javascript
const { is, utils, Utils } = require( '@mojule/is' )

// utils uses the default predicates
console.log( utils.isOnly( 'abc', 'string' ) )

const predicates = {
  domNode: subject =>
    Is.is.object( subject ) && Is.is.string( subject.nodeName ),
  div: subject =>
    predicates.domNode( subject ) && subject.nodeName.toLowerCase() === 'div'
}

// the Utils factory function can create utility functions for custom predicates
const domUtils = Utils( predicates )

const span = document.querySelector( 'span' )
const div = document.querySelector( 'div' )

// Returns true as predicate for span not in predicates above.
domUtils.isOnly( span, 'domNode' )

// Returns false as div is also 'div'
domUtils.isOnly( div, 'domNode' )

// Returns true, as span matches domNode
domUtils.some( span, 'domNode', 'div' )

// Returns true as a div is both a domNode and a div
domUtils.every( div, 'domNode', 'div' )

// Returns 'domNode' i.e. The first predicate that matches div
domUtils.of( div )

// Returns [ 'domNode', 'div' ] i.e. An array of names of all predicates matching div
domUtils.allOf( div )
```

Note: Predicates are checked __in the order__ that the keys are declared i.e. in
the above example `utils.of( div )` returns the predicate name `'domNode'`
and not `'div'` because `'domNode'` is declared first in the passed custom
predicates.

## License

[MIT](https://github.com/mojule/mojule/blob/master/LICENSE)

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