Last active
December 20, 2015 23:09
-
-
Save GianlucaGuarini/6210149 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Here we will could extend lodash with some custom useful methods | |
* | |
*/ | |
(function (_) { | |
'use strict'; | |
/** | |
* Initialize a new array having a custom length and a custom default value | |
* @param { Number | Object | String } defaultValue: any valid javascript element | |
* @param { Int } length: the length of the output | |
* @return { Array } having a custom value and custom length | |
*/ | |
var CustomArray = function (defaultValue, length) { | |
var _arr = []; | |
while (length--) _arr.push(defaultValue); | |
return _arr; | |
}; | |
var _arrOperation = function (arr1, arr2, operation) { | |
var arr2 = arr2 || arr1; | |
if (_.isNumber(arr2)) arr2 = new CustomArray(arr2, arr1.length); | |
if (_.isNumber(arr1)) arr1 = new CustomArray(arr1, arr2.length); | |
if (arr1.length !== arr2.length) throw ('Please the two arrays must have the same length'); | |
return _.map(arr1, function (val, i) { | |
switch (operation) { | |
case 'max': | |
return val > arr2[i] ? val : arr2[i]; | |
break; | |
case 'min': | |
return val < arr2[i] ? val : arr2[i]; | |
break; | |
case '+': | |
return val + arr2[i]; | |
break; | |
case '*': | |
return val * arr2[i]; | |
break; | |
case '/': | |
return val / arr2[i]; | |
break; | |
default: | |
} | |
}); | |
}; | |
_.mixin({ | |
/** | |
* | |
* Similar to the _.pick method but it could be used with more than one property | |
* @return { Object } new object containing only the properties filtered | |
* | |
*/ | |
'multiplePick': function (obj, array) { | |
var ret = {}; | |
_.each(array, function (key) { | |
ret[key] = obj[key]; | |
}); | |
return ret; | |
}, | |
/** | |
* | |
* @param { Array } arr | |
* @param { Array | Object | String | Number } elm: element contained inside this array | |
* @return { Boolean } return whether the element is the last one of the array | |
*/ | |
'isLast': function (arr, elm) { | |
var length = arr.length - 1, | |
elmIndex = _.indexOf(arr, elm); | |
// if this element is not contained iside this array | |
if (!~elmIndex) return false; | |
return elmIndex === length; | |
}, | |
/** | |
* Merge two arrays picking the max numeric value per each index | |
* @param { Array } arr1 | |
* @param { Array } arr2 | |
* @return { Array } | |
*/ | |
'arrMax': function (arr1, arr2) { | |
return _arrOperation(arr1, arr2, 'max'); | |
}, | |
/** | |
* Return the sum of all the numeric items in the first array or merge two numeric arrays summing the value of each index of these last | |
* @param { Array } arr1 | |
* @param { Array } arr2 | |
* @return { Float } | |
*/ | |
'arrSum': function (arr1, arr2) { | |
return _arrOperation(arr1, arr2, '+'); | |
}, | |
/** | |
* Multiply two array | |
* @param { Array } arr1 | |
* @param { Array } arr2 | |
* @return { Float } | |
*/ | |
'arrMultiplication': function (arr1, arr2) { | |
return _arrOperation(arr1, arr2, '*'); | |
}, | |
/** | |
* Divide two array | |
* @param { Array } arr1 | |
* @param { Array } arr2 | |
* @return { Float } | |
*/ | |
'arrDivision': function (arr1, arr2) { | |
return _arrOperation(arr1, arr2, '/'); | |
} | |
}); | |
}(window._)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment