File Explorer

/var/runtime/node_modules/@aws-sdk/node_modules/obliterator

This explorer reads the filesystem of the server it runs on, so /workspace/user isn't present here. Browsing and the terminal still work against this server's own disk from /.

0 dirs
33 files
README.md6.3 KB · 322 lines
[![Build Status](https://travis-ci.org/Yomguithereal/obliterator.svg)](https://travis-ci.org/Yomguithereal/obliterator) # Obliterator Obliterator is a dead simple JavaScript/TypeScript library providing miscellaneous higher-order iterator functions such as combining two or more iterators into a single one. # Installation ```npm install --save obliterator``` Note `obliterator` comes along with its TypeScript declarations. # Usage ## Summary *Classes* * [Iterator](#iterator) *Functions* * [chain](#chain)* [combinations](#combinations)* [consume](#consume)* [filter](#filter)* [forEach](#foreach)* [map](#map)* [match](#match)* [permutations](#permutations)* [powerSet](#powerSet)* [split](#split)* [take](#take) ## Iterator A handy Iterator class with safeguards and usable with ES2015's `for ... of` loop constructs & spread operator. ```jsimport Iterator from 'obliterator/iterator';// Orimport {Iterator} from 'obliterator'; const iterator = new Iterator(function() {  // Define what the `next` function does}); // Checking that the given value is an iterator (native or else)Iterator.is(value); // Creating an empty iteratorconst emptyIterator = Iterator.empty(); // Creating a simple iterator from a single valueconst simpleIterator = Iterator.of(34); // Creating a simple iterator from multiple valuesconst multipleIterator = Iterator.of(1, 2, 3);``` ## chain Variadic function chaining all the given iterators. ```jsimport chain from 'obliterator/chain';// Orimport {chain} from 'obliterator'; const set1 = new Set('a');const set2 = new Set('bc'); const chained = chain(set1.values(), set2.values()); chained.next();>>> {done: false, value: 'a'}chained.next();>>> {done: false, value: 'b'}``` ## combinations Returns an iterator of combinations of the given array and of the given size. Note that for performance reasons, the yielded combination is always the same object. ```jsimport combinations from 'obliterator/combinations';// Orimport {combinations} from 'obliterator'; const iterator = combinations(['A', 'B', 'C', 'D'], 2); iterator.next().value;>>> ['A', 'B']iterator.next().value;>>> ['A', 'C']``` ## consume Function consuming the given iterator fully or for n steps. ```jsimport consume from 'obliterator/consume';// Orimport {consume} from 'obliterator'; const set = new Set([1, 2, 3]); // Consuming the whole iteratorlet iterator = set.values();consume(iterator);iterator.next().done>>> true // Consuming n stepslet iterator = set.values();consume(iterator, 2);iterator.next().value>>> 3``` ## filter Function returning an iterator filtering another one's values using the given predicate. ```jsimport filter from 'obliterator/filter';// Orimport {filter} from 'obliterator'; const set = new Set([1, 2, 3, 4, 5]); const even = x => x % 2 === 0; const iterator = filter(even, set.values()); iterator.next().value>>> 2iterator.next().value>>> 4``` ## forEach Function able to iterate over almost any JavaScript iterable value using a callback. Supported values range from arrays, typed arrays, sets, maps, objects, strings, arguments, iterators, arbitrary iterables etc. ```jsimport forEach from 'obliterator/foreach';// Orimport {forEach} from 'obliterator'; const set = new Set(['apple', 'banana']); forEach(set.values(), (value, i) => {  console.log(i, value);}); // Iterating over a stringforEach('abc', (char, i) => ...); // Iterating over a mapforEach(map, (value, key) => ...);``` Optionally, one can use the `forEachWithNullKeys` function to iterate over mixed values but with the twist that iterables without proper keys (lists, sets etc.), will yield `null` instead of an index key. ```jsimport {forEachWithNullKeys} from 'obliterator/foreach'; const set = new Set(['apple', 'banana']); forEach(set, (value, key) => {  console.log(key, value);});>>> null, 'apple'>>> null, 'banana'``` ## map Function returning an iterator mapping another one's values using the given function. ```jsimport map from 'obliterator/map';// Orimport {map} from 'obliterator'; const set = new Set([1, 2, 3, 4, 5]); const triple = x => x * 3; const iterator = map(triple, set.values()); iterator.next().value>>> 3iterator.next().value>>> 6``` ## match Function returning an iterator over the matches of a given regex applied to the target string. ```jsimport match from 'obliterator/match';// Orimport {match} from 'obliterator'; const iterator = match(/t/, 'test'); iterator.next().value.index>>> 0iterator.next().value.index>>> 3``` ## permutations Returns an iterator of permutations of the given array and of the given size. Note that for performance reasons, the yielded permutation is always the same object. ```jsimport permutations from 'obliterator/permutations';// Orimport {permutations} from 'obliterator'; let iterator = permutations([1, 2, 3]); iterator.next().value>>> [1, 2, 3]iterator.next().value>>> [1, 3, 2] iterator = permutations(['A', 'B', 'C', 'D'], 2); iterator.next().value;>>> ['A', 'B']iterator.next().value;>>> ['A', 'C']``` ## powerSet Returns an iterator of sets composing the power set of the given array. ```jsimport powerSet from 'obliterator/power-set';// Orimport {powerSet} from 'obliterator'; const iterator = powerSet(['A', 'B', 'C']); iterator.next().value;>>> []iterator.next().value;>>> ['A']``` ## split Returns an iterator over the splits of the target string, according to the given RegExp pattern. ```jsimport split from 'obliterator/split';// Orimport {split} from 'obliterator'; const iterator = split(/;/g, 'hello;world;super'); iterator.next().value;>>> 'hello'iterator.next().value;>>> 'world'``` ## take Function taking values from given iterator and returning them in an array. ```jsimport take from 'obliterator/take';// Orimport {take} from 'obliterator'; const set = new Set([1, 2, 3]); // To take n values from the iteratortake(set.values(), 2);>>> [1, 2] // To convert the full iterator into an arraytake(set.values());>>> [1, 2, 3]``` # Contribution Contributions are obviously welcome. Please be sure to lint the code & add the relevant unit tests before submitting any PR. ```git clone git@github.com:Yomguithereal/obliterator.gitcd obliteratornpm install # To lint the codenpm run lint # To run the unit testsnpm test``` # License [MIT](LICENSE.txt)