npm add itsybitsy
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"]
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]
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
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]
import { bitsy } from "itsybitsy";
function* compact<T>(item: T): Generator<Exclude<T, null | undefined | false>> {
if (item == null || (typeof item === 'boolean' && item === false)) return;
yield item as Exclude<T, null | undefined | false>;
}
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]
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] ]
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?
const strings = [...forte(addOne).then(toString).of(numbers)];
const totalCount = forte(count, 0).reduce(numbers);
Alternative API
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"]