# comparejs

> comparejs implements JavaScript's comparison operators the way you would expect them to be.

Latest version **4.0.14** (published 2021-08-16) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 4.0.14 |
| Published | 2021-08-16 |
| First published | 2013-01-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 24.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | thenativeweb-admin, goloroden, yeldir, nhummel, dotkuro |
| Keywords | assert, compare, equal, test |

## Links

- npm: https://www.npmjs.com/package/comparejs
- Repository: https://github.com/thenativeweb/comparejs
- Homepage: https://github.com/thenativeweb/comparejs#readme
- Issues: https://github.com/thenativeweb/comparejs/issues
- npm.io page: https://npm.io/package/comparejs

## Dependencies (2)

- [is-subset-of](https://npm.io/package/is-subset-of.md) 3.1.9
- [typedescriptor](https://npm.io/package/typedescriptor.md) 3.0.2

## 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

- 4.0.14 (latest) — 2021-08-16
- 4.0.13 — 2021-06-15
- 4.0.12 — 2021-06-10
- 4.0.11 — 2021-06-08
- 4.0.10 — 2021-06-08
- 4.0.9 — 2021-05-17
- 4.0.8 — 2021-05-10
- 4.0.7 — 2021-03-25
- 4.0.6 — 2021-03-25
- 4.0.5 — 2021-03-25
- 4.0.4 — 2020-11-03
- 4.0.3 — 2020-11-03
- 4.0.2 — 2020-11-03
- 4.0.1 — 2020-03-19
- 4.0.0 — 2019-11-15
- … 15 more at https://npm.io/package/comparejs/versions

## README

# comparejs

comparejs implements JavaScript's comparison operators the way you would expect them to be.

## Status

| Category         | Status                                                                                                                                         |
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| Version          | [![npm](https://img.shields.io/npm/v/comparejs)](https://www.npmjs.com/package/comparejs)                                                      |
| Dependencies     | ![David](https://img.shields.io/david/thenativeweb/comparejs)                                                                                  |
| Dev dependencies | ![David](https://img.shields.io/david/dev/thenativeweb/comparejs)                                                                              |
| Build            | ![GitHub Actions](https://github.com/thenativeweb/comparejs/workflows/Release/badge.svg?branch=main) |
| License          | ![GitHub](https://img.shields.io/github/license/thenativeweb/comparejs)                                                                        |

## Installation

```shell
$ npm install comparejs
```

## Quick Start

To use comparejs you need to reference it from your application by adding the following line:

```javascript
const { compare } = require('comparejs');
```

If you use TypeScript, use the following code instead:

```typescript
import { compare } from 'comparejs';
```

### Features

-   Supports comparison of `array`, `boolean`, `function`, `null`, `number`, `object`, `string` and `undefined`.
-   Supports comparison of native data types and constructor-created data types, such as `number` and `new Number()`.
-   Supports comparison of objects and arrays using deep-equal.
-   Supports comparison of objects and arrays with `<`, `<=`, `>` and `>=` by handling them as subsets.
-   Supports comparison of objects by structure.
-   Supports comparison with `undefined` correctly, as `<=` and `>=` are problematic in plain JavaScript.
-   Supports comparison in a perfectly type-safe way out-of-the-box.
-   Supports comparison by equality and identity, depending on what makes sense.

### Basic usage

Now you are able to use the various comparison operators. All you need to do is access the `compare` object and use its functions:

| Operator                          |  Alias             | Description           |
| --------------------------------- | ------------------ | --------------------- |
| `equal(left, right)`              | `eq(left, right)`  | equal                 |
| `notEqual(left, right)`           | `ne(left, right)`  | not equal             |
| `lessThan(left, right)`           | `lt(left, right)`  | less than             |
| `lessThanOrEqual(left, right)`    | `lte(left, right)` | less than or equal    |
| `greaterThan(left, right)`        | `gt(left, right)`  | greater than          |
| `greaterThanOrEqual(left, right)` | `gte(left, right)` | greater than or equal |
| `identity(left, right)`           | `id(left, right)`  | identity              |

Please note that each comparison operator works on each combination of types and does what you would expect it to do.

### Structure comparison operators

For objects, there are special operators that compare by structure. Among other things, they can be used to verify objects against interfaces:

| Operator                                     | Alias               | Description                        |
| -------------------------------------------- | ------------------- | ---------------------------------- |
| `equalByStructure(left, right)`              | `eqs(left, right)`  | equal by structure                 |
| `notEqualByStructure(left, right)`           | `nes(left, right)`  | not equal by structure             |
| `lessThanByStructure(left, right)`           | `lts(left, right)`  | less than by structure             |
| `lessThanOrEqualByStructure(left, right)`    | `ltes(left, right)` | less than or equal by structure    |
| `greaterThanByStructure(left, right)`        | `gts(left, right)`  | greater than by structure          |
| `greaterThanOrEqualByStructure(left, right)` | `gtes(left, right)` | greater than or equal by structure |

Please note that these operators only work for objects. For any other type, they return `false`.

## Running quality assurance

To run quality assurance for this module use [roboter](https://www.npmjs.com/package/roboter):

```shell
$ npx roboter
```

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