# has-shape

> Super simple runtime type-checking for Javascript objects

Latest version **2.0.1** (published 2020-07-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install has-shape
pnpm add has-shape
yarn add has-shape
bun add has-shape
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2020-07-16 |
| First published | 2020-07-08 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 16 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Flame10A |
| Maintainers | flame10a |

## Links

- npm: https://www.npmjs.com/package/has-shape
- Repository: https://github.com/Flame10A/has-shape
- Homepage: https://github.com/Flame10A/has-shape#readme
- Issues: https://github.com/Flame10A/has-shape/issues
- npm.io page: https://npm.io/package/has-shape

## Recent versions

- 2.0.1 (latest) — 2020-07-16
- 2.0.0 — 2020-07-15
- 1.2.2 — 2020-07-10
- 1.2.1 — 2020-07-10
- 1.2.0 — 2020-07-09
- 1.1.0 — 2020-07-09
- 1.0.0 — 2020-07-08

## README

[![hasShape() on NPM](https://img.shields.io/npm/v/has-shape)](https://www.npmjs.com/package/has-shape)
![typings: included](https://img.shields.io/badge/typings-included-brightgreen)
![dependencies: 0](https://img.shields.io/badge/dependencies-0-brightgreen)

Super simple runtime type-checking for Javascript objects.

Also provides relevant typing for Typescript and type-aware IDEs (e.g. VS Code).

# Installation
```sh
npm install has-shape
```

# Basic usage
`hasShape()` receives a value, and a shape specifier. It then determines
    whether the value matches the shape.

```javascript
import hasShape from 'has-shape';

// ...

if (hasShape(formData, { name: 'string', password: isValidPassword })) {
    // ...
}
```

In this example, `formData` must pass the following conditions:
- It is an object.
- It has a `name` property, which is a string.
- It has a `password` property, which passes the `isValidPassword` predicate.

# Supported shapes

- Object shapes, e.g. `{ name: 'string', age: 'number' }`
- `typeof` strings
    - 'number', 'string', etc.
    - Also includes 'any' and 'unknown'. These match any type, and the typings
        map to the matching TS type.
- Nested object shapes
- Regular Expressions
- Predicate functions
- Assertion functions
- Array shapes (via `arrayShape()`)

# Additional functions

### `arrayShape()`

Creates a shape which matches arrays containing elements of the given specifier.

```javascript
if (hasShape(value, arrayShape('number'))) { // True if value is an array of numbers
```

By default, empty arrays are allowed. This can be overridden by passing an
    options object with `disallowEmpty: true`.

```javascript
const teamShape = {
    name: 'string',
    members: arrayShape({
        name: 'string',
        age: 'number'
    }, { disallowEmpty: true })
};

if (hasShape(obj, teamShape)) {
```

## `assertShape()`

The same as `hasShape()`, but rather than returning a boolean, instead throws
    a descriptive error if the value does not match the shape.

```javascript
assertShape(person, personShape); // Throws if person does not match personShape
```

## `shape()`

Creates an assertion function for the given shape.

```javascript
const personShape = shape({ name: 'string', age: 'number' });

personShape(person); // Throws if person does not match personShape
```

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