2.0.0 • Published 9 years ago
auxiliary-js
Licence
ISC
Version
2.0.0
Deps
0
Vulns
0
Weekly
0
Auxiliary JS : a utility library
Installation:
npm install auxiliary-js
Usage:
var util=require('util');
- Each(): to iterate through an array or object
var list=[1,2,3]; // var list={"one":1,"two":2,"three":3};
util.each(list,function(val){
console.log(val);
});
- Map(): to modify values of an object or array and return it.
var list=[1,2,3];
util.map(list,functio(val){
return val*2;
});
- First() and Last() to get the first and last element from the list respectively.
var first=util.map([1,2,3]);
var last=util.map([1,2,3]);
- Find(): returns the first matching elements from the array
var list=[1,2,3,4,5,6];
var result=util.find(list,function(val){
return val%2==0;
}); //returns 2
- Filter(): returns all matching elements from the array
var list=[1,2,3,4,5,6];
var result=util.filter(list,function(val){
return val%2==0;
}); //returns [2,4,6]
- Except(): returns all the non matching elements from the array
var list=[1,2,3,4,5,6];
var result=util.excpt(list,function(val){
return val%2==0;
}); //returns [1,3,5]
- All(): returns true if all the elements match the given criteria
var list=[2,4,6];
var result = util.all(list, function (val) {
return val % 2 == 0;
}); //returns true;
- Any(): returns true if any of the elements match the given criteria
var list=[2,4,6];
var result = util.any(list, function (val) {
return val % 2 == 0;
}); //returns true;
- Min() and Max(): returns min and max value from array respectively
var list=[2,4,6,-2];
var min = util.min(list); //returns -2;
var max = util.max(list); //returns 6;
- Sort(): to sort collections
var list = [2, 5, 1, 10, 8, 7];
var result = util.sortBy(list);
var list = [
{ id: 2, name: "abc", price: 100 },
{ id: 5, name: "ghi", price: 200 },
{ id: 1, name: "def", price: 300 }
];
var result = util.sort(list, "id"); //sorting by id field
- SortByDesc(): to sort collections in descending order
var list = [2, 5, 1, 10, 8, 7];
var result = util.sortByDesc(list);
var list = [
{ id: 2, name: "abc", price: 100 },
{ id: 5, name: "ghi", price: 200 },
{ id: 1, name: "def", price: 300 }
];
var result = util.sortByDesc(list, "id"); //sorting by id field
- Count(): to get the count of the elements satisfying the criteria
var list = [2, 5, 1, 10, 8, 7];
var result = util.count(list,function(val){
return val%2==0;
}); //returns 3
- CountBy(): to get the count of the elements by their types
var list = [2, 3, 4, 5, 6, 7, 0, 0, 8, 0];
var result = util.countBy(list, function (val) {
if (val == 0) return 'zero';
if (val % 2 == 0) return 'even';
return 'odd';
});
/*
returns
{
zero: 3,
even: 4,
odd: 3
}
*/