# object-predicate

> Use POJOs to represent predicates that operate on objects

Latest version **1.2.1** (published 2018-11-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install object-predicate
pnpm add object-predicate
yarn add object-predicate
bun add object-predicate
```

## Health

**Score 40/100 (D)** — status: abandoned.

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

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2018-11-12 |
| First published | 2018-08-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 447.7 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Mike North |
| Maintainers | northm |
| Keywords | ember-addon |

## Links

- npm: https://www.npmjs.com/package/object-predicate
- Repository: https://github.com/mike-north/object-predicate
- Homepage: https://github.com/mike-north/object-predicate#readme
- Issues: https://github.com/mike-north/object-predicate/issues
- npm.io page: https://npm.io/package/object-predicate

## Dependencies (5)

- [semantic-release](https://npm.io/package/semantic-release.md) ^15.9.5
- [@mike-north/types](https://npm.io/package/@mike-north/types.md) ^1.0.5
- [travis-deploy-once](https://npm.io/package/travis-deploy-once.md) ^5.0.2
- [@mike-north/js-lib-renovate-config](https://npm.io/package/@mike-north/js-lib-renovate-config.md) ^1.0.0
- [@mike-north/js-lib-semantic-release-config](https://npm.io/package/@mike-north/js-lib-semantic-release-config.md) ^1.0.1

## Recent versions

- 1.2.1 (latest) — 2018-11-12
- 1.2.0 — 2018-08-07
- 1.1.0 — 2018-08-07
- 1.0.4 — 2018-08-07
- 1.0.3 — 2018-08-07
- 1.0.2 — 2018-08-07
- 1.0.1 — 2018-08-02
- 1.0.0 — 2018-08-02

## README

# object-predicate

[![Build Status](https://travis-ci.org/mike-north/object-predicate.svg?branch=master)](https://travis-ci.org/mike-north/object-predicate)
[![Version](https://img.shields.io/npm/v/object-predicate.svg)](https://www.npmjs.com/package/object-predicate)

Use simple objects to represent predicates in JavaScript and TypeScript

## Setup

```
npm install object-predicate
```
## Why

Sometimes we need to be able to store a predicate as JSON data. Regex and function support added on top of this for convenience

## Use

```ts
import { toPredicate } from 'object-predicate';

interface Character {
  name: string;
  age: number;
  occupation: string;
};

const characters: Character[] = [
  { name: 'Walter White',  age: 51, occupation: 'Chemistry Teacher' },
  { name: 'Gustavo Freng', age: 53, occupation: 'Restaurant Owner' },
  { name: 'Hank Schrader', age: 48, occupation: 'DEA Agent' },
  { name: 'Skyler White',  age: 47, occupation: 'Accountant' },
]

// Number predicate (greater-than, less-than, greater-or-equal, less-or equal)
const over50 = toPredicate({
  age: { gt: 50 }
});
const charactersOver50 = characters.filter(over50);

// Exact string match
const isAccountant = toPredicate({
  occupation: 'Accountant'
});
const accountants = characters.filter(isAccountant);

// Using a regex
const nameEndsWithWhite = toPredicate({
  name: /White$/
});
const whiteFamily = characters.filter(nameEndsWithWhite);

// Using a property predicate
const isDea = toPredicate({
  occupation: o => o.indexOf('DEA') >= 0
});
const deaAgents = characters.filter(isDea);
```

---
(c) 2018

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