NativeScript Enumerable
A NativeScript module providing LINQ style extensions for handling arrays and lists.
NativeScript Toolbox
This module is part of nativescript-toolbox.
License
Platforms
- Android
- iOS
Installation
Run
tns plugin add nativescript-enumerable
inside your app project to install the module.
Usage
Create a sequence
; // from a list of values / objects with variable length; // from an array / list; // from object; // range of numbers: 2, 3, 4, 5, 6; // 50979 'TM' strings;
Work with them
; .skip1 // skip one element (5979) .take3 // take next remaining 3 elements (23979, 23979, 1781) .distinct // remove duplicates .select"" + x // convert to strings .order; // order by element ascending newSeq.each;
Most methods are chainable as in .NET context.
Functions and lambda expression
Many methods require one or more function.
Instead using a function like
.where
you can write
.where'x => x !== null'
instead.
Documentation
The full documentation with all functions and methods can be found on readme.io.
Demo app
The demo app contains examples to all functions and methods.
It also contains a code editor where you can enter own code and run it!



The wiki describes how it works.
Examples
Filters
// distinct()// 1, 2, 4, 3Enumerable.create1, 2, 4, 2, 3 .distinct; // except()// 2.0, 2.1, 2.3, 2.4, 2.5Enumerable.create20, 21, 22, 23, 24, 25 .except; // intersect()// 26, 30Enumerable.create44, 26, 92, 30, 71, 38 .intersect; // ofType()// '2', 'Tanja'Enumerable.create1, '2', 2, 'Tanja', 3 .ofType'string'; // typeof x == 'string' // union()// 5, 3, 9, 7, 8, 6, 4, 1, 0Enumerable.create5, 3, 9, 7, 5, 9, 3, 7 .union; // where()// 1, 2, 3Enumerable.create1, 2, 3, 4 .wherex < 4;
Sort elements
// orderBy(), thenBy()//// "apple", "grape", "mango", "banana",// "orange", "blueberry", "raspberry", "passionfruit"Enumerable.create"grape", "passionfruit", "banana", "mango", "orange", "raspberry", "apple", "blueberry" .orderByx.length // complement: orderByDescending() .thenByx; // complement: thenByDescending() // shorter: then() // reverse()// 4, 3, 2, 1Enumerable.create1, 2, 3, 4 .reverse;
Take / skip elements
// skip()// 3, 4Enumerable.create0, 1, 2, 3, 4 .skip3; // skipLast()// 0, 1, 2, 3Enumerable.create0, 1, 2, 3, 4 .skipLast; // skipWhile()// 55, 666, 77Enumerable.create22, 33, 44, 55, 666, 77 .skipWhilex < 50; // take()// 0, 1, 2Enumerable.create0, 1, 2, 3, 4 .take3; // takeWhile()// 22, 33, 44Enumerable.create22, 33, 44, 55 .takeWhilex < 50;
Get one element
// elementAt()// 33Enumerable.create11, 22, 33, 44 .elementAt2; // elementAtOrDefault()// 'TM'Enumerable.create11, 22, 33, 44 .elementAtOrDefault4, 'TM'; // out of range // first()// 11Enumerable.create11, 22, 33, 44 .first; // firstOrDefault()// 'MK'Enumerable.create .firstOrDefault'MK'; // last()// 44Enumerable.create11, 22, 33, 44 .last; // lastOrDefault()// 'PZ'Enumerable.create .lastOrDefault'PZ'; // single()// EXCEPTION, because we have more than one elementEnumerable.create11, 22, 33, 44 .single; // singleOrDefault()// 11Enumerable.create11 .singleOrDefault'YS';
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.create11, 22, 33, 44 .firstx >= 20;
Accumulators
// aggregate()// "Marcel Joachim Kloubert"Enumerable.create'Marcel', 'Joachim', 'Kloubert' .aggregate; // average()// 2.5Enumerable.create1, 2, 3, 4 .average; // sum()// 10Enumerable.create1, 2, 3, 4 .sum;
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.fromArraypersons .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.fromArraypersons .joinpets,person.name,pet.owner.name,;
Groupings
// groupBy()Enumerable.create"grape", "passionfruit", "banana", "apple", "blueberry" .groupByx .each;
Projection
// select()// "MARCEL", "KLOUBERT"Enumerable.create"Marcel", "Kloubert" .selectx.toUpperCase; // selectMany()// 1, 10, 100, 2, 20, 200, 3, 30, 300Enumerable.create1, 2, 3 .selectMany; // zip()// "Marcel Kloubert", "Bill Gates", "Albert Einstein"Enumerable.create'Marcel', 'Bill', 'Albert' .zip,;
Checks / conditions
// all()// (false)Enumerable.create1, 2, '3', 4 .alltypeof x !== "string"; // contains()// (true)Enumerable.create1, 2, '3' .contains3; // any()// (true)Enumerable.create1, 2, '3', 4 .anytypeof x === "string"; // sequenceEqual()// (false) Enumerable.create .sequenceEqual;
Conversions
// toArray() .toArray; // toObject() .toObject"item" + index; // toObservable() .toObservable"item" + index; // toObservableArray() .toObservableArray; // toLookup()// // lookup['A'][0] = 'Albert'// lookup['B'][0] = 'Bill'// lookup['B'][1] = 'Barney'// lookup['K'][0] = 'Konrad'// lookup['M'][0] = 'Marcel' .toLookupx;
Count
// 3Enumerable.create0, 1, 2 .count; // 2Enumerable.create0, 1, 2 .countx > 0;
More
concat
// 0, 1, 2, 'PZ', 'TM', 'MK'Enumerable.create0, 1, 2 .concat;
defaultIfEmpty
// 0, 1, 2Enumerable.create0, 1, 2 .defaultIfEmpty'PZ', 'TM', 'MK'; // 'PZ', 'TM', 'MK'Enumerable.create .defaultIfEmpty'PZ', 'TM', 'MK';
moveNext / current
;while seq.moveNext
reset
; seq.each; seq.reset .each;