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
- Host: GitHub
- URL: https://github.com/royalicing/itsybitsy
- Owner: RoyalIcing
- License: mit
- Created: 2021-09-11T13:24:24.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-01-11T03:51:59.000Z (over 4 years ago)
- Last Synced: 2025-01-18T23:44:08.469Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 320 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
## 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"]
```