# @vlr/array-tools

> Set of DRY tools to work with array. Typescript style.

Latest version **2.2.1** (published 2019-08-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @vlr/array-tools
pnpm add @vlr/array-tools
yarn add @vlr/array-tools
bun add @vlr/array-tools
```

## 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.2.1 |
| Published | 2019-08-18 |
| First published | 2017-12-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 89.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Vladimir Repin |
| Maintainers | vlr |

## Links

- npm: https://www.npmjs.com/package/@vlr/array-tools
- Repository: https://gitlab.com/vlr/array-tools
- Homepage: https://gitlab.com/vlr/array-tools#readme
- Issues: https://gitlab.com/vlr/array-tools/issues
- npm.io page: https://npm.io/package/@vlr/array-tools

## Recent versions

- 2.2.1 (latest) — 2019-08-18
- 2.2.0 — 2019-04-28
- 2.1.0 — 2019-02-03
- 2.0.0 — 2019-01-29
- 1.0.15 — 2019-01-11
- 1.0.14 — 2018-10-14
- 1.0.13 — 2018-09-09
- 1.0.12 — 2018-08-20
- 1.0.11 — 2018-08-10
- 1.0.10 — 2018-02-05
- 1.0.9 — 2018-02-02
- 1.0.8 — 2018-01-31
- 1.0.7 — 2018-01-07
- 1.0.6 — 2018-01-06
- 1.0.5 — 2017-12-29
- … 1 more at https://npm.io/package/@vlr/array-tools/versions

## README

# @vlr/array-tools
Set of DRY tools to work with array. Typescript style.
To use it with ES5, to support Internet explorer for instance, import it as "@vlr/array-tools/es5"

# all
Method returns true if all items in the array fullfil the condition. Empty array also yields true.

```
import { all } from "@vlr/array-tools"

const a = [1, 2, 3];

const allHigher = all(a, item => item > 2); // false
const allNumbers = all(a, isNumber); // true
```

# areEqual
Method returns true if two arrays are equal by criteria, and go in the same order.

```
const a = [ 
  { id: 1, name: 'a' }, 
  { id: 2, name: 'b' }
];

const b = [ 
  { id: 1, name: 'c' }, 
  { id: 2, name: 'd' }
];

const c = [ 
  { id: 2, name: 'b' }, 
  { id: 1, name: 'a' }
];

const anb = areEqual(a, b, item => item.id); // true
const anc = areEqual(a, c, item => item.id); // false
```

# areEquivalent
Method returns true if two arrays are equal by criteria, order does not matter.

# contains
Returns true if item is present in the array. Useful if array.prototype.includes is not available

# intersect and except
intersect returns items present in both arrays, except return items from first array not present in second array

```
const a = [1, 2];
const b = [2, 3];

const result1 = intersect(a, b); // [2]
const result2 = except(a, b); // [1]
```

# flatMap
Combination of map and then flatten, aka concatenation of array of arrays.

const a = [1, 2];

const b = flatMap(a, item => [item, 3]); // [1, 3, 2, 3];

# hasElements
Returns true if array is an instance of Array and contains at least one element

# first and last
Returns first and last elements of array satisfying the criteria, or undefined if no such elements present.

```
const a = [1, 2, 3];

const result1 = first(a, item => item > 1); // 2
const result2 = last(a, item => item > 1); // 3
```

# min and max
Returns element minimal or maximal by given criteria

```
const a = [1, 2, 3];

const result1 = min(a, item => -item); // 3
const result2 = max(a, item => -item); // 1
```

# regexToArray
Current interface of working with /g regex is a bit inconvenient. This method transforms it to array of matches. So you can execute .map on it and etc.

```
const matches = regexToArray(/\d/g, 'a1b2c'); 
// two matches, matches[0][0] = '1', matches[1][0] = '2'
```

# splitInGroups
Splits array into small arrays of given size

```
const a = [1, 2, 3, 4];
const result = splitInGroups(a, 2); // [[1, 2], [3, 4]];
```

# unique
Returns items from array unique by given criteria

```
const a = [ 
  { id: 1, name: 'a' }, 
  { id: 1, name: 'b' },
  { id: 2, name: 'c' }, 
  { id: 2, name: 'd' }
];

const result = unique(a, item => item.id); //  [a[0], a[2]]
```

# range
Returns array of numbers from start(inclusive) to end(non-inclusive)

# trim
Removes amount of items from end of the array, trimOne removes one item.

```
const items = [1, 2, 3, 4];

const result = trim(items, 2); // [1, 2]
```

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