node-enumerable
ES6 ready LINQ library written in TypeScript.
Table of contents
- Requirements
- Installation
- Usage
- Playground / demos
- Examples
- Documentation
- License
- Tests
↑]
Requirements [- an ES6 compatible environment like modern browsers or NodeJS
- TypeScript 2.3 or later (ONLY when using defintion files)
↑]
Installation [↑]
NodeJS [Run
npm install node-enumerable --save
inside project folder to install the module.
↑]
Browser [Download the latest version from here.
<!-- node-enumerable -->
↑]
Usage [↑]
Create a sequence [const Enumerable = ; { 111; 222; 333;} // from a list of values / objects with variable lengthlet seq1 = Enumerable; // from an arraylet seq2 = Enumerable;// from a generatorlet seq3 = Enumerable;// from a string// // 'A', 'j', 'n', 'a', 't'let seq4 = Enumerable; // alt: Enumerable.fromString('Ajnat'); // range of numbers: 2, 3, 4, 5, 6let seq5 = Enumerable; // 5979 'TM' stringslet seq6 = Enumerable; // build, using factory function// // 'item_1', 'item_2', 'item_3'let seq7 = Enumerable; // build, using factory function// by building a flatten list// // 1, 10, 100, 2, 20, 200, 3, 30, 300let seq8 = Enumerable; // create 3 elements // // the 'build()' function has // a same argument // create empty sequencelet seq9 = Enumerable;
↑]
Work with them [let seq = Enumerable; let newSeq = seq // remove all elements that are (null) // skip one element (5979) // take next remaining 3 elements (23979, 23979, 1781) // remove duplicates // convert to strings ; // order by element ascending // you also can use the// 'each' and 'forEach' methods// of the sequence to do the// following jobfor let item of newSeq // [0] 1781 // [1] 23979 console;
Most methods are chainable as in .NET context.
↑]
Async operations [const FS = ;const Path = ; let seq = Enumerable; seq;
The context
argument of the async()
method uses the AsyncActionContext interface.
↑]
Playground / demos [You can test all features in your browser.
↑]
Examples [↑]
Filters [// distinct()// 1, 2, 4, 3Enumerable ;// distinctBy()// "grape", "passionfruit", "banana", "raspberry"Enumerable ; // except()// 2.0, 2.1, 2.3, 2.4, 2.5Enumerable ; // intersect()// 26, 30Enumerable ; // ofType()// '5979', 'Tanja'Enumerable ; // typeof x === 'string' // union()// 5, 3, 9, 7, 8, 6, 4, 1, 0Enumerable ; // where()// 1, 2, 3Enumerable ;
↑]
Sort elements [// orderBy(), thenBy()//// "apple", "grape", "mango", "banana",// "orange", "blueberry", "raspberry", "passionfruit"Enumerable // complement: orderByDescending() ; // complement: thenByDescending() // shorter: then() // reverse()// 4, 3, 2, 1Enumerable ; // rand()// e.g.: 2, 5, 7, 8, 0, 4, 6, 9, 3, 1Enumerable ; // alt: shuffle()
↑]
Take / skip elements [// skip()// 3, 4Enumerable ; // skipLast()// 0, 1, 2, 3Enumerable ; // skipWhile()// 55, 666, 77Enumerable ; // take()// 0, 1, 2Enumerable ; // takeWhile()// 22, 33, 44Enumerable ;
↑]
Get one element [// elementAt()// 33Enumerable ; // elementAtOrDefault()// 'TM'Enumerable ; // out of range // first()// 11Enumerable ; // firstOrDefault()// 'MK'Enumerable ; // last()// 44Enumerable ; // lastOrDefault()// 'PZ'Enumerable ; // single()// EXCEPTION, because we have more than one elementEnumerable ; // singleOrDefault()// 11Enumerable ;
All methods with NO OrDefault
suffix will throw exceptions if no element was found.
You also can use a function as first argument for all of these methods that works as filter / condition:
// first()// 22Enumerable ;
↑]
Accumulators [// aggregate()// " Marcel Joachim Kloubert"Enumerable.create'Marcel', 'Joachim', 'Kloubert' .aggregate, ''; // average()// 2.5Enumerable.create1, 2, 3, 4 .average; // "M., Tanja"Enumerable.create'M.', 'Tanja' .joinToString', ';
↑]
Minimum / maximum values [// max()// 3Enumerable.create1, 3, 2 .max; // min()// 1Enumerable.create2, 3, 1, 2 .min;
↑]
Joins [ ; ; // groupJoin()// // [0] 'Owner: Tanja; Pets: WauWau, Sparky'// [1] 'Owner: Marcel; Pets: Gina, Schnuffi, Asta'// [2] 'Owner: Yvonne; Pets: Schnuffel'// [3] 'Owner: Josefine; Pets: Lulu'Enumerable.frompersons .groupJoinpets,person.name,pet.owner.name,; // join()// // [0] 'Owner: Tanja; Pet: WauWau'// [1] 'Owner: Marcel; Pet: Gina'// [2] 'Owner: Marcel; Pet: Schnuffi'// [3] 'Owner: Marcel; Pet: Asta'// [4] 'Owner: Yvonne; Pet: Schnuffel'// [5] 'Owner: Josefine; Pet: Lulu'Enumerable.frompersons .joinpets,person.name,pet.owner.name,;
↑]
Groupings [// groupBy()Enumerable ;
↑]
Projection [// flatten()// 1, (false), 3, 44, '555', 66.6, (true)Enumerable ; // select()// "MARCEL", "KLOUBERT"Enumerable ; // selectMany()// 1, 10, 100, 2, 20, 200, 3, 30, 300Enumerable ; // zip()// "Marcel Kloubert", "Bill Gates", "Albert Einstein"Enumerable ;
↑]
Checks / conditions [// all()// (false)Enumerable all typeof x !== "string"; // any()// (true)Enumerable ; // contains()// (true)Enumerable ; // not()// 1, 2, 4Enumerable ; // sequenceEqual()// (false) Enumerable ;
↑]
Conversions [// toArray()let jsArray = Enumerable ; // toObject()let obj = Enumerable ; // toLookup()// // lookup['A'][0] = 'Albert'// lookup['B'][0] = 'Bill'// lookup['B'][1] = 'barney'// lookup['K'][0] = 'Konrad'// lookup['M'][0] = 'Marcel'let lookup = Enumerable ;
↑]
Count [// 3Enumerable ; // a second call will return 0 // if reset() method is not called // 2Enumerable ; // 4Enumerable length; // a second call will return // the same value, because we have an array // based sequence here // // a generator based sequence will behave as count() // (false)Enumerable ; // all are (false)Enumerable;Enumerable;Enumerable;
↑]
Math [// abs()// 1, 22.57, 444, NaN, -333.85, NaNEnumerable ; // ceil()// -1, 23, 444, NaN, -333, NaNEnumerable ; // cos()// 0.004, -0.99996, -0.01Enumerable ; // complement: arcCos() // cosH()// 29937.07, 1792456423.07, 107321789892958.03Enumerable ; // complement: arcCosH() // exp()// 2.72, 7.39, 20.09Enumerable ; // floor()// -1, 23, 444, NaN, -334, NaNEnumerable ; // log()// 0, 1, 2, 3, 4Enumerable ; // pow()// 1, 4, 9, 16Enumerable ; // product()// 24Enumerable product; // root()// 1, 2, 3, 4Enumerable ; // round()// -1, 23, 444, NaN, -334, 2, NaNEnumerable ; // sin()// 0.84, 0.91, 0.14Enumerable ; // complement: arcSin() // sinH()// 1.18, 3.63, 10.02Enumerable ; // complement: arcSinH() // sqrt()// 1, 2, 3, 4Enumerable ; // sum()// 10Enumerable ; // tan()// 1.72, -1.76, -0.01Enumerable ; // complement: arcTan() // tanH()// 0, 0.46, -0.76Enumerable ; // complement: arcTanH()
↑]
More [↑]
assert [let seq1 = Enumerable;seq1; // will throw an exception // at second element (1) let seq2 = Enumerable;seq2; // will throw an aggregated exception // at the end // for all odd values
↑]
chunk [let seq = Enumerable;for let chunk of seq // [0] => [0, 1, 2] // [1] => [3, 4, 5] // [2] => [6, 7, 8] // [3] => [9]
↑]
clone [let father = Enumerable; // create 3 clones of 'father'for let child of father //TODO // alt: father.clone().take(3)
↑]
concat / concatArray [// 0, 1, 2, 'PZ', 'TM', 'MK'Enumerable ; // alt: append() // 0, 111, 222, 'pz', 'tm', 'mk'Enumerable ; // alt: appendArray()
↑]
consume [ { let storage = ; return iterator: storage: storage ;} { for let i = 0; i < size; i++ i; storage; } const OBJ = ; const SEQ = Enumerable;SEQ; // enumerates the 'iterator' in OBJ // and fills the 'storage' in OBJ
↑]
defaultIfEmpty / defaultArrayIfEmpty [// 0, 1, 2Enumerable ; // 'PZ', 'TM', 'MK'Enumerable ; // 0, 11, 22Enumerable ;// alt: defaultSequenceIfEmpty() // 'pz', 'tm', 'mk'Enumerable ;
↑]
forAll [let arr = ; try // alt: eachAll() Enumerable;catch e // access the list of errors by // 'e.errors' // e.errors[0] = 'Error in value 0'; // e.errors[1] = 'Error in value 2'; // e.errors[2] = 'Error in value 3'; // arr[0] === 1// arr[1] === 3// arr[2] === 5
↑]
intersperse / intersperseArray [// 0, '-', 1, '-', 2Enumerable ; // -- or --Enumerable ;
↑]
pipe [let arr1 = ;let arr2 = ; let seq = Enumerable;for let item of seq arr2; // arr1 = [10, 20, 30]// arr2 = [1, 2, 3]
↑]
popFrom / shiftFrom [let arr1 = 11 22 33 ;for let item of Enumerable // [0] 33 // [1] 22 // [2] 11// arr1 is empty now let arr2 = 111 222 333 ;for let item of Enumerable // [0] 111 // [1] 222 // [2] 333// arr2 is empty now
↑]
prepend / prependArray [// 'PZ', 'TM', 'MK', 0, 1, 2Enumerable ; // 'pz', 'tm', 'mk', 0, 111, 222Enumerable ;
↑]
pushTo [let arr = ;Enumerable ; // arr: [0, 1, 2]
↑]
random [for let value of Enumerable // 10 random numbers // between 0 and 1 for let value of Enumerable // 23979 random numbers // between 0 and 5979
↑]
reset [let seq = Enumerable; seq; seq ;
↑]
trace [// write items via 'console.trace()'Enumerable ; // with formatterEnumerable ;
↑]
Documentation [The API documentation can be found here.
↑]
License [↑]
Tests [Go to the module folder and run
tscnpm test
to start unit tests from test/
subfolder.