# @jswork/filter2tuple

> A function to split an array into two tuples based on a filtering condition.

Latest version **1.0.4** (published 2023-09-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install @jswork/filter2tuple
pnpm add @jswork/filter2tuple
yarn add @jswork/filter2tuple
bun add @jswork/filter2tuple
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2023-09-07 |
| First published | 2023-09-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | afeiship |
| Keywords | array, filter, split, tuple |

## Links

- npm: https://www.npmjs.com/package/@jswork/filter2tuple
- Homepage: https://js.work
- npm.io page: https://npm.io/package/@jswork/filter2tuple

## Recent versions

- 1.0.4 (latest) — 2023-09-07
- 1.0.3 — 2023-09-07
- 1.0.2 — 2023-09-07
- 1.0.1 — 2023-09-07

## README

# filter2tuple
> A function to split an array into two tuples based on a filtering condition.

[![version][version-image]][version-url]
[![license][license-image]][license-url]
[![size][size-image]][size-url]
[![download][download-image]][download-url]

## installation
```shell
npm install @jswork/filter2tuple
```

## usage
```js
import filter2tuple from '@jswork/filter2tuple';

const numbers = [1, 2, 3, 4, 5, 6];
const [even, odd] = filter2tuple(numbers, (item) => item % 2 === 0);

console.log(even, odd);
// [2, 4, 6] [1, 3, 5]
```

## other
> Use reduce implement:

```js
// impl1:
function filter2tuple(arr, filterFn) {
  return arr.reduce((result, item) => {
    filterFn(item) ? result[0].push(item) : result[1].push(item);
    return result;
  }, [[], []]);
}

// impl2: 
function filter2tuple(arr, filterFn) {
  return arr.reduce(
    (result, item) => {
      const idx = Number(!filterFn(item));
      result[idx].push(item);
      return result;
    },
    [[], []]
  );
}
```

## license
Code released under [the MIT license](https://github.com/afeiship/filter2tuple/blob/master/LICENSE.txt).

[version-image]: https://img.shields.io/npm/v/@jswork/filter2tuple
[version-url]: https://npmjs.org/package/@jswork/filter2tuple

[license-image]: https://img.shields.io/npm/l/@jswork/filter2tuple
[license-url]: https://github.com/afeiship/filter2tuple/blob/master/LICENSE.txt

[size-image]: https://img.shields.io/bundlephobia/minzip/@jswork/filter2tuple
[size-url]: https://github.com/afeiship/filter2tuple/blob/master/dist/index.min.js

[download-image]: https://img.shields.io/npm/dm/@jswork/filter2tuple
[download-url]: https://www.npmjs.com/package/@jswork/filter2tuple

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