A simple JS library that mimics some of the C# LINQ Extension. Most of the extensions are just wrappers to already existing Array prototype method.
Using this library, you are able to filter, sort, and map as you would in C#:
var data = users.where( u => u.isActive )
.orderBy( "-lastLogin" )
.skip( page * pageSize )
.take( pageSize )
.select( u => {
return {
"User Name": u.userName,
"Last Login": new Date( u.lastLogin ).toLocaleString(),
"Created On": new Date( u.createdOn ).toLocaleString()
}
});
var arrToSort = [
{ first: 1, second: "a", third: 0, fourth: [ 1, 2, 3 ] },
{ first: 1, second: "b", third: 0 },
{ first: 2, second: "a", third: 0, fourth: [ 1, 2, 3 ] },
{ first: 3, second: "b", third: 1 },
{ first: 3, second: "b", third: 0 },
];
Returns a boolean based on whether the predicate
returns a truthy value for any element.
arrToSort.any( a => a.first === 2 );
// expected outcome: true
arrToSort.any( a => a.first === 12 );
// expected outcome: false
Returns a boolean based on whether the predicate
returns a truthy value for every element.
arrToSort.all( a => a.first );
// expected outcome: true
arrToSort.all( a => a.first === 1 );
// expected outcome: false
Filters the array based on the predicate
function provided.
arrToSort.where( a => a.first === 2 );
// expected outcome: [{ first: 2, second: "a", third: 0 }]
Skips count
elements in the array.
arrToSort.skip( 4 );
// expected outcome: [{ first: 3, second: "b", third: 0 }]
Takes the first count
elements int he array
arrToSort.take( 1 );
// expected outcome: [{ first: 1, second: "a", third: 0 }]
Orders array based on element's properties in ascending order unless denoted by a minus/subtract symbol.
arrToSort.orderBy( "first", "-second", "third" );
/* expected outcome: [
{ first: 1, second: "b", third: 0, fourth: [ 1, 2, 3 ] },
{ first: 1, second: "a", third: 0 },
{ first: 2, second: "a", third: 0, fourth: [ 1, 2, 3 ] },
{ first: 3, second: "b", third: 0 },
{ first: 3, second: "b", third: 1 },
]*/
Returns array with elements based on the output of func
arrToSort.select( a => a.first );
// expected outcome: [ 1, 1, 2, 3, 3 ]
Returns a flattened array with elements based on the output of func
arrToSort.selectMany( a => a.fourth );
// expected outcome: [ 1, 2, 3, undefined, 1, 2, 3, undefined, undefined, undefined ]
arrToSort.where( a => a.fourth ).selectMany( a => a.fourth );
// expected outcome: [ 1, 2, 3, 1, 2, 3 ]
Will return the first element in the array. Default will return null if 0 elements exist, other wise exceptions are thrown if there are no elements in the array.
If the optional predicate parameter is provided, will execute a .where() on the array first.
arrToSort.first();
// expected outcome: { first: 1, second: "a", third: 0 }
[].first();
// expected outcome: Sequence contains no elements
[].firstOrDefault();
// expected outcome: null
Will return the ONLY element in the array. Default will return null if 0 elements exist, other wise exceptions are thrown if not exactly 1 element exists in the array.
If the optional predicate parameter is provided, will execute a .where() on the array first.
arrToSort.take( 1 ).single();
// expected outcome: { first: 1, second: "a", third: 0 }
arrToSort.single();
// expected outcome: More than one element exists in sequence
[].single();
// expected outcome: Sequence contains no elements.
[].singleOrDefault();
// expected outcome: null