YAEnumerable
Yet Another Enumerable Java Script Framework
Purpose
To create and maintain a linq type JS framework. See Credits/Other Frameworks for alternatives.
Install
npm install yaenumerable
Example
Each selector will at a minimum return an item and the index of the item. The index is always the last item returned in the callback. See tests for additional examples.
where Condition
var enumerable = ;var anArray = a:1a:2a:3; //aValues will now be [{a:2},{a:3}]var aValues = enumerable ;
select
var enumerable = ;var anArray = a:1a:2a:3; //aValues will now be [1,2,3]var aValues = enumerable ;
selectMany
Allows you to flatten an array
var enumerable = ;var anArray = a:123 a:456 a:789 ; //Will return: [1,2,3,4,5,6,7,8,9]var flattenedArray = enumerable ;
first
var enumerable = ;var anArray = a:1a:2a:3; //if no function specified will return first item:{a:1}var firstObject = enumerable; //Since function is specfied to return a, will return 1var firstA = enumerable ;
sum
var enumerable = ; //sum without a selector//In this case it will return 6var sum = enumerable; //When specified with a selector will return the sum of that item://In this case it will return 6var sumOfA = enumerable ;
count
count items in enumerable. Example use is with an enumerable
var enumerable = ;var anArray = 123; //Will return 2var count = enumerable ;
any
Determines if any items meet a condtion
var enumerable = ;var anArray = 123; //Will return truevar hasItemGreaterThan1 = enumerable ; //Can use with a selector as wellvar hasItemAGreaterThan1 = enumerable ;
forEach
var enumerable = ;var anArray = a:1a:2a:3; //aValues will now be [1,2,3]var aValues = enumerable ;
asyncForEach
Allows you to make async calls on each item in an array and get the results of all calls when each call is complete.
var enumerable = ;var anArray = a:1a:2a:3; var { //Some long process var result = item; ; }; enumerable ;
For additional examples see the tests. To run them:
npm test