DEVBOX-LINQ
Lambda operations for js inspired by C# :)
Installation
npm install devbox-linq
Usage (Node.js)
;
only works on v6 and above
Documentation
- add
- addRange
- all
- any
- count
- distinct
- distinctRecursive
- first
- firstOrDefault
- groupBy
- last
- lastOrDefault
- max
- min
- average
- order
- orderBy
- orderByDesc
- orderDesc
- remove
- removeAt
- select
- selectMany
- skip
- sum
- take
- thenBy
- thenByDesc
- where
add(item)
Add a item into a existing array.
let array = id: 1 name: 'Goku' ; array; // Same as .push(), it's only semanthic console;/*OUTPUT:[ { id: 1, name: 'Goku' }, { id: 2, name: 'Vegeta' }]*/
addRange(...array)
Add a list of items into a existing array.
//Way 1let array1 = id: 1 name: 'Goku' id: 2 name: 'Vegeta' ;let array2 = id: 3 name: 'Bulma' id: 4 name: 'Gohan' ; array1; console;/*OUTPUT:[ { id: 1, name: 'Goku' }, { id: 2, name: 'Vegeta' }, { id: 3, name: 'Bulma' }, { id: 4, name: 'Gohan' }]*/
//Way 2let array = id: 1 name: 'Goku' id: 2 name: 'Vegeta' ; array; console;/*OUTPUT:[ { id: 1, name: 'Goku' }, { id: 2, name: 'Vegeta' }, { id: 3, name: 'Bulma' }, { id: 4, name: 'Gohan' }]*/
all(condition)
Checks that all items in the list match the condition.
let array = id: 1 name: 'Goku' id: 2 name: 'Vegeta' ; arrayall xid == 1; // falsearrayall xid >= 1; // true
any(condition?)
Checks that one item in the list match the condition. If no condition, check if has something in the list.
let array = id: 1 name: 'Goku' id: 2 name: 'Vegeta' ; array; // falsearray; // truearray; // true
count(condition?)
Count all items that match the condition. If no condition, returns the list length.
let array = id: 1 name: 'Goku' id: 2 name: 'Vegeta' ;array; //1array; //2
distinct()
Distinguishes equal items from the list. If it's complex type, like object and array, it'll be by reference.
let array = text: 'Object' text: 'Object' ;array; // Nothing happens let obj = text: 'Object2' ;array; // Adding one timearray; // Adding two timesarray;console;/*OUTPUT:[ { text: 'Object' }, { text: 'Object' }, { text: 'Object2' }]*/ 1 2 3 4 1 2; // [1, 2, 3, 4]
distinctRecursive()
Distinguishes equal items from the list. If it's complex type, like object, it'll be done verifying the attributes.
let array = text: 'Object' text: 'Object' ;array; // [{ text: 'Object' }] let obj = text: 'Object2' ;array; // Adding one timearray; // Adding two timesarray;console;/*OUTPUT:[ { text: 'Object' }, { text: 'Object2' }]*/
first(condition?)
Returns the first item that match the condition.
let array = id: 1 name: 'Goku' id: 2 name: 'Vegeta' id: 2 name: 'Vegeta2' ; array; // { id: 2, name: 'Vegeta' }array; // { id: 1, name: 'Goku' } array; // throws exception; // throws exception
firstOrDefault(condition?)
Returns the first item that match the condition, if anyone match, returns undefined instead exception.
let array = id: 1 name: 'Goku' id: 2 name: 'Vegeta' id: 2 name: 'Vegeta2' ; array; // { id: 2, name: 'Vegeta' }array; // { id: 1, name: 'Goku' } array; // undefined; // undefined
groupBy(expression)
Group the list by a expression.
let array = id: 1 name: 'Goku' age: 20 id: 2 name: 'Vegeta' age: 20 id: 3 name: 'Bulma' age: 19 ; let group = array;console;/*OUTPUT:[ [ { id: 1, name: 'Goku', age: 20 }, { id: 2, name: 'Vegeta', age: 20 } ], [ { id: 3, name: 'Bulma', age: 19 } ]]*/ console; // 20console; // 19
last(condition?)
Returns the last item that match the condition.
let array = id: 1 name: 'Goku' id: 1 name: 'Goku2' id: 2 name: 'Vegeta' ; array; // { id: 1, name: 'Goku2' }array; // { id: 2, name: 'Vegeta' } array; // throws exception; // throws exception
lastOrDefault(condition?)
Returns the last item that match the condition, if anyone match, returns undefined instead exception.
let array = id: 1 name: 'Goku' id: 1 name: 'Goku2' id: 2 name: 'Vegeta' ; array; // { id: 1, name: 'Goku2' }array; // { id: 2, name: 'Vegeta' } array; // undefined; // undefined
max(expression?)
Returns the largest item that match the expression.
let array = id: 1 name: 'Goku' age: 22 id: 2 name: 'Vegeta' age: 20 ; array; // 221 5 3; // 5
min(expression?)
Returns the smallest item that match the expression.
let array = id: 1 name: 'Goku' age: 22 id: 2 name: 'Vegeta' age: 20 ; array; // 201 5 3; // 1
average(expression?)
Returns the average of list that match the expression.
10 20 30; // 20
order()
Order list ascending.
2 3 5 1 4; // [1, 2, 3, 4, 5]'B' 'C' 'E' 'A' 'D'; // ['A', 'B', 'C', 'D', 'E']
orderBy(expression)
Order list ascending by expression.
let array = id: 1 name: 'Goku' age: 22 id: 2 name: 'Vegeta' age: 20 id: 3 name: 'Bulma' age: 15 ; array;console;/*OUTPUT:[ { id: 3, name: 'Bulma', age: 15 }, { id: 2, name: 'Vegeta', age: 20 }, { id: 1, name: 'Goku', age: 22 }]*/ array;console;/*OUTPUT:[ { id: 3, name: 'Bulma', age: 15 }, { id: 1, name: 'Goku', age: 22 }, { id: 2, name: 'Vegeta', age: 20 }]*/ 5 4 3 2 1; // [1, 2, 3, 4, 5] - Same as .order()
orderByDesc(expression)
Order list descending by expression.
let array = id: 1 name: 'Goku' age: 15 id: 2 name: 'Vegeta' age: 20 id: 3 name: 'Bulma' age: 22 ; array;console;/*OUTPUT:[ { id: 3, name: 'Bulma', age: 22 }, { id: 2, name: 'Vegeta', age: 20 }, { id: 1, name: 'Goku', age: 15 }]*/ array;console;/*OUTPUT:[ { id: 2, name: 'Vegeta', age: 20 }, { id: 1, name: 'Goku', age: 15 }, { id: 3, name: 'Bulma', age: 22 }]*/ 1 2 3 4 5; // [5, 4, 3, 2, 1] - Same as .orderDesc()
orderDesc()
Order list descending.
2 3 5 1 4; // [5, 4, 3, 2, 1]'B' 'C' 'E' 'A' 'D'; // ['E', 'D', 'C', 'B', 'A']
remove(condition?)
Remove items from the list.
let array = id: 1 name: 'Goku' id: 2 name: 'Vegeta' ; //By Conditionarray;console; //OUTPUT: [{ id: 1, name: 'Goku' }] //By Reflet goku = array;array;console; //OUTPUT: []
removeAt(index)
Remove a item from the list by index.
let array = id: 1 name: 'Goku' id: 2 name: 'Vegeta' ; array;console; //OUTPUT: [{ id: 1, name: 'Goku' }]
select(expression)
Transform your list.
let array = id: 1 name: 'Goku' id: 2 name: 'Vegeta' ; array; // [1, 2]array; // [2, 3]array; // ['Goku is very strong', 'Vegeta is very strong'] array; /*OUTPUT:[ { user: '1 - Goku' }, { user: '2 - Vegeta' }]*/
selectMany(expression)
Joins multiple lists inside a object in one.
let array = id: 1 name: 'Goku' friends: 'Chi-Chi' 'Kuririn' 'Trunks' 'Gohan' id: 2 name: 'Vegeta' friends: 'Bulma' 'Trunks' ; let allFriends = array;console; // ['Chi-Chi', 'Kuririn', 'Trunks', 'Gohan', 'Bulma', 'Trunks']console; // ['Chi-Chi', 'Kuririn', 'Gohan', 'Bulma', 'Trunks']
skip(length)
Skip the length informed.
let array = id: 1 name: 'Goku' id: 2 name: 'Vegeta' id: 3 name: 'Bulma' ; let arraySkipped = array;console; // [{ id: 3, name: 'Bulma' }] 1 2 3 4 5 6 7 8 9 10; // [6, 7, 8, 9, 10]
sum(expression?)
Sum the items by expression.
let array = id: 1 name: 'Goku' power: 8001 id: 2 name: 'Vegeta' power: 7000 ; array; // 150011 2 3 4 5; // 15
take(length)
Take the length informed.
let array = id: 1 name: 'Goku' id: 2 name: 'Vegeta' id: 3 name: 'Bulma' ; let arrayTook = array;console; // [{ id: 1, name: 'Goku' }] arrayTook = array;console; // [{ id: 2, name: 'Vegeta' }] 1 2 3 4 5 6 7 8 9 10; // [1, 2, 3, 4, 5]1 2 3 4 5 6 7 8 9 10; // [4, 5, 6]
thenBy(expression)
Order the list ascending by a second expression after some "order" method (order, orderBy, orderByDesc or orderDesc)
let array = id: 1 name: 'Goku' age: 20 id: 2 name: 'Vegeta' age: 22 id: 3 name: 'Bulma' age: 20 ; array;console;/*OUTPUT[ { id: 3, name: 'Bulma', age: 20 }, { id: 1, name: 'Goku', age: 20 }, { id: 2, name: 'Vegeta', age: 22 }]*/ //Order the even first, then put them in the ascending order10 9 8 7 6 5 4 3 2 1;//OUTPUT: [2, 4, 6, 8, 10, 1, 3, 5, 7, 9]
thenByDesc(expression)
Order the list descending by a second expression after some "order" method (order, orderBy, orderByDesc or orderDesc)
let array = id: 1 name: 'Goku' age: 20 id: 2 name: 'Vegeta' age: 22 id: 3 name: 'Bulma' age: 20 ; array;console;/*OUTPUT[ { id: 1, name: 'Goku', age: 20 } { id: 3, name: 'Bulma', age: 20 } { id: 2, name: 'Vegeta', age: 22 }]*/ //Order the even first, then put them in the descending order10 9 8 7 6 5 4 3 2 1;//OUTPUT: [10, 8, 6, 4, 2, 9, 7, 5, 3, 1]
where(expression)
Filter items that matches the condition
let array = id: 1 name: 'Goku' power: 8001 id: 2 name: 'Vegeta' power: 7000 id: 3 name: 'Bulma' power: 12 ; let strongers = array;console;/*OUTPUT:[ { id: 1, name: 'Goku', power: 8001 }, { id: 2, name: 'Vegeta', power: 7000 }]*/ 1 2 3 4 5 6; // [2, 4, 6]