0.0.5 • Published 2 years ago
@ajetski/seq v0.0.5
seq
A sequence/stream library for easily parsing and analyzing data
Installing
# install using npm:
npm i @ajetski/seq
# or install using yarn:
yarn add @ajetski/seq
Importing
import {
Sequence,
Range,
Filter,
Map,
Sequenceable /*, ...*/,
} from '@ajetski/seq';
Creating Sequenceables
Creating an empty Sequence
let seq = new Sequence();
Creating a Sequence from an Iterable
let seq = new Sequence([1, 2, 3]);
Creating a Range
let seq = new Range(1, 4); // Sequence(1, 2, 3)
let seq2 = new Range(1, 10, 2); // Sequence(1, 3, 5, 7, 9)
let seq3 = new Range(10, 1, -2); // Sequence(10, 8, 6, 4, 2)
Operations on Sequenceables
Iterating through a Sequence
let seq = new Range(1, 4); // Sequence(1, 2, 3)
seq.forEach((value, index) => {
///...
});
Convert to Array
let seq = new Range(1, 4); // Sequence(1, 2, 3)
let arr = seq.toArray();
Filtering a Sequence
let seq = new Sequence([1, 2, 3]).filter((elem, idx) => idx % 2 === 0); // Sequence(1, 3)
Fold/Reduce a Sequence
let seq = new Sequence([1, 2, 3]).fold(0, (acc, elem) => acc + elem); // 6
Map a Sequence
let seq = new Sequence([1, 2, 3]).map((elem, idx) => elem * 2); // Sequence(2, 4, 6)
Concat two Sequences
let seqA = new Sequence([1, 2, 3]);
let seqB = new Sequence([4, 5, 6]);
let seqC = seqA.concat(seqB); // Sequence(1, 2, 3, 4, 5, 6)
Append to a Sequence
let seqA = new Sequence([1, 2, 3]);
let seqB = seqA.append(4); // Sequence(1, 2, 3, 4)
Tests
Check out the tests folder to see more examples and usages