JSPM

  • Created
  • Published
  • Downloads 294
  • Score
    100M100P100Q89421F
  • License MIT

Provides a huge set of functions for working with iterable collections

Package Exports

  • itertools-ts

Readme

IterTools implementation for TypeScript

Scrutinizer Code Quality Coverage Status Build and test License: MIT

Inspired by Python — designed for TypeScript.

Warning: This library is an active work in progress, and is not yet ready for production use.

Setup

npm i itertools-ts

Quick Reference

Loop Iteration Tools

Single Iteration

Iterator Description Code Snippet
flatMap Map function onto items and flatten result flatMap(data, mapper)
map Map function onto each item map(data, mapper)
repeat Repeat an item a number of times repeat(item, repetitions)

Usage

Flat Map

Map a function only the elements of the iterable and then flatten the results.

function *flatMap<TInput, TOutput>(
  data: Iterable<TInput>|Iterator<TInput>,
  mapper: FlatMapper<TInput, TOutput>,
): Iterable<TOutput>
import { flatMap } from './single';

const data = [1, 2, 3, 4, 5];
const mapper = ($item) => [$item, -$item];

for (number of flatMap(data, mapper)) {
    console.log(`${number} `);
}
// 1 -1 2 -2 3 -3 4 -4 5 -5

Map

Map a function onto each element.

function* map<TInput, TOutput>(
  data: Iterable<TInput>|Iterator<TInput>,
  mapper: (datum: TInput) => TOutput,
): Iterable<TOutput>
import { map } from './single';

const grades = [100, 99, 95, 98, 100];
const strictParentsOpinion = (g) => (g === 100) ? 'A' : 'F';

for(const actualGrade of map(grades, strictParentsOpinion)) {
  console.log(actualGrade);
}
// A, F, F, F, A

Repeat

Repeat an item.

function *repeat<T>(item: T, repetitions: number): Iterable<T>
import { repeat } from './single';

data = 'Beetlejuice';
repetitions = 3;

for (const repeated of repeat(data, repetitions)) {
    console.log(repeated);
}
// 'Beetlejuice', 'Beetlejuice', 'Beetlejuice'

Unit testing

npm i
npm run test

License

IterTools TS is licensed under the MIT License.