Last active
October 27, 2015 15:44
-
-
Save TheBox193/95bab69d40e9e033cca9 to your computer and use it in GitHub Desktop.
loDash.js and Underscore.js Mixins
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
/** | |
* Removes all falsy and undefined values from an object | |
* @param {object} o Object to be compacted | |
* @return {object} Compact Object | |
* @link http://stackoverflow.com/a/14058408/417822 | |
*/ | |
_.mixin({'compactObject': function(o) { | |
var clone = _.clone(o); | |
_.each(clone, function(v, k) { | |
if(!v) delete clone[k]; | |
}); | |
return clone; | |
}}); |
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
/** | |
* Safely retrieves the value of a deeply nested object value | |
* @param {object} O The Object to be searched | |
* @param {string} str Dot notation string of the address of the attribute to be retrieved | |
* @return {mixed} Value of nested item or undefined | |
* @link http://stackoverflow.com/a/15400575/417822 | |
*/ | |
_.mixin({'getDeep': function(O, str) { | |
if ( !_.isObject(O) ) return; | |
var seg= str.split('.'); | |
while(O && seg.length) { | |
O = O[seg.shift()]; | |
} | |
return O; | |
}}); |
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
/** | |
* Tests if a deep value has a value/exists. Uses _.getDeep to retrieve value. | |
* @param {object} O The Object to be tested | |
* @param {string} str Dot notation string of the address of the attributed to be tested | |
* @return {boolean} | |
* @todo validate that falesy values 'false' '0' etc come back as true. | |
*/ | |
_.mixin({'hasDeep': function(O, str) { | |
return !_.isUndefined( _.getDeep(O, str) ); | |
}}); |
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
/** | |
* Shorthand for !_.isUndefined | |
* @param {mixed} e Item to be tested | |
* @return {boolean} | |
*/ | |
_.mixin({'isDefined': function(e) { | |
return !_.isUndefined(e); | |
}}); |
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
/** | |
* Toggles an array element | |
* @param {Array} arr Source Array | |
* @param {Mixed} val Value to toggle | |
* @param {Boolean} Optional: Boolean to determine whether to add or remove | |
* @return {Array} Result | |
*/ | |
_.mixin({'arrayToggle': function(arr, val, state) { | |
if ( _.indexOf(arr,val)==-1 || state) | |
return _.union(arr,[val]); | |
else | |
return _.without(arr,val); | |
}}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blog Post: http://blog.jstassen.com/2015/09/lodash-js-underscore-js-mixins/