# sorted-array-functions

> Maintain and search through a sorted array using some low level functions

Latest version **1.3.0** (published 2020-08-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install sorted-array-functions
pnpm add sorted-array-functions
yarn add sorted-array-functions
bun add sorted-array-functions
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2020-08-24 |
| First published | 2016-10-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/sorted-array-functions) |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 56 |
| Author | Mathias Buus |
| Maintainers | mafintosh |

## Links

- npm: https://www.npmjs.com/package/sorted-array-functions
- Repository: https://github.com/mafintosh/sorted-array-functions
- Issues: https://github.com/mafintosh/sorted-array-functions/issues
- npm.io page: https://npm.io/package/sorted-array-functions

## Recent versions

- 1.3.0 (latest) — 2020-08-24
- 1.2.0 — 2018-06-02
- 1.1.0 — 2017-12-28
- 1.0.0 — 2016-10-23

## README

# sorted-array-functions

Maintain and search through a sorted array using some low level functions

```
npm install sorted-array-functions
```

[![build status](http://img.shields.io/travis/mafintosh/sorted-array-functions.svg?style=flat)](http://travis-ci.org/mafintosh/sorted-array-functions)

## Usage

``` js
var sorted = require('sorted-array-functions')
var list = []

sorted.add(list, 1)
sorted.add(list, 4)
sorted.add(list, 2)

console.log(list) // prints out [1, 2, 4]
console.log(sorted.has(list, 2)) // returns true
console.log(sorted.has(list, 3)) // returns false
console.log(sorted.eq(list, 2)) // returns 1 (the index)
console.log(sorted.gt(list, 2)) // returns 2
console.log(sorted.gt(list, 4)) // returns -1
```

## API

#### `sorted.add(list, value, [compare])`

Insert a new value into the list sorted.
Optionally you can use a custom compare function that returns, `compare(a, b)` that returns 1 if `a > b`, 0 if `a === b` and -1 if `a < b`.

#### `sorted.addFromFront(list, value, [compare])`

Inserts a new value (same result as `sorted.add()`) optimized for prepend.

#### `var bool = sorted.remove(list, value, [compare])`

Remove a value. Returns true if the value was in the list.

#### `var bool = sorted.has(list, value, [compare])`

Check if a value is in the list.

#### `var index = sorted.eq(list, value, [compare])`

Get the index of a value in the list (uses binary search).
If the value could not be found -1 is returned.

#### `var index = sorted.gte(list, value, [compare])`

Get the index of the first value that is `>=`.
If the value could not be found -1 is returned.

#### `var index = sorted.gt(list, value, [compare])`

Get the index of the first value that is `>`.
If the value could not be found -1 is returned.

#### `var index = sorted.lte(list, value, [compare])`

Get the index of the first value that is `<=`.
If the value could not be found -1 is returned.

#### `var index = sorted.lt(list, value, [compare])`

Get the index of the first value that is `<`.
If the value could not be found -1 is returned.

## License

MIT

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