An open API service indexing awesome lists of open source software.

https://github.com/royalicing/itsybitsy

Map, filter, flat map, reduce β€” all in one place
https://github.com/royalicing/itsybitsy

Last synced: 2 months ago
JSON representation

Map, filter, flat map, reduce β€” all in one place

Awesome Lists containing this project

README

          


πŸ•·πŸ•Έ itsybitsy


Map, filter, flat map, reduce β€”Β all with the one function



minified and gzipped size
minified size
zero dependencies

## Install

```console
npm add itsybitsy
```

## Examples

### Map

```ts
import { bitsy } from "itsybitsy";

function* toString(n: number) {
yield n.toString();
}

const numbers = [1, 2, 3];
const strings = [...bitsy(toString).from(numbers)];
// ["1", "2", "3"]
```

### Filter

```ts
import { bitsy } from "itsybitsy";

function* whereEven(item: number) {
if (item % 2 === 0) yield item;
}

const numbers = [1, 2, 3, 4, 5, 6];
const evens = [...bitsy(whereEven).from(numbers)];
// [2, 4, 6]
```

### Reduce

```ts
import { bitsy } from "itsybitsy";

function* count(_item: unknown, accum: number) {
return accum + 1;
}

function* sum(item: number, accum: number) {
return accum + item;
}

const numbers = [1, 2, 3, 4, 5, 6];
const totalCount = bitsy(count, 0).result(numbers); // 6
const totalSum = bitsy(sum, 0).result(numbers); // 21
```

### Flat map

```ts
import { bitsy } from "itsybitsy";

function* repeatEvens(item: number) {
if (item % 2 === 0) {
yield item;
yield item;
} else {
yield item;
}
}

const numbers = [1, 2, 3, 4, 5, 6];
const withEvensRepeated = [...bitsy(repeatEvens).from(numbers)];
// [1, 2, 2, 3, 4, 4, 5, 6, 6]
```

### Compact

```ts
import { bitsy } from "itsybitsy";

function* compact(item: T): Generator> {
if (item == null || (typeof item === 'boolean' && item === false)) return;
yield item as Exclude;
}

const itemsWithFalsey = [0, 1, false, 2, true, 3, null, 4, undefined, 5];
const itemsWithoutFalsey = [...bitsy(compact).from(itemsWithFalsey)];
// [0, 1, 2, true, 3, 4, 5]
```

### Chunk into pairs

```ts
import { bitsy } from "itsybitsy";

function* chunk2(item: string | number, accum: undefined | unknown) {
if (accum === undefined) {
return item;
} else {
yield [accum, item];
return;
}
}

const pairsFlat = [
"/",
8,
"/about",
3,
"/docs/api",
2,
"/pricing",
1,
];
const pairs = [...bitsy(chunk2, undefined).from(pairsFlat)];
// [ ["/", 8], ["/about", 3], ["/docs/api", 2], ["/pricing", 1] ]
```

### Chaining with `.then`

```ts
import { bitsy } from "itsybitsy";

function* add1(item: number) {
yield item + 1;
}

function* toString(item: number) {
yield item.toString();
}

function* repeatEvens(item: number) {
if (item % 2 === 0) {
yield item;
yield item;
} else {
yield item;
}
}

const numbers = [1, 2, 3, 4, 5];
const strings = [...bitsy(add1).then(repeatEvens).then(toString).from(numbers)];
// ["2", "2", "3", "4", "4", "5", "6", "6"]
```

----

Alternative name: forte?

```ts
const strings = [...forte(addOne).then(toString).of(numbers)];

const totalCount = forte(count, 0).reduce(numbers);
```

----

Alternative API

```ts
bitsy({
*0(n: number) {
if (n % 2 === 0) yield n;
},
*1(n: number) {
yield n.toString();
},
*2(s: string) {
yield `#${s}`;
}
}).from([1, 2, 3, 4, 5, 6]);
// ["#2", "#4", "#6"]
```