Created
May 21, 2016 18:18
-
-
Save PseudoSky/66f1aa0cbb5c5718c15c89226765aad2 to your computer and use it in GitHub Desktop.
My precious lodash secrets.
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
// ---------------------------------------------------------------------------// | |
// -------------------------------- Helpers --------------------------------- // | |
// ---------------------------------------------------------------------------// | |
function ignoreEqual(obj_v,src_v){ | |
if(!obj_v || src_v == obj_v){ | |
return null; | |
} | |
if(obj_v && src_v!= obj_v){ | |
return obj_v; | |
}; | |
return null; | |
} | |
// Execute an array of functions | |
var fly = _.spread(_.flow); | |
// Passing an array of filters to nilty will return a function that will | |
// Apply them all to a given array, omitting the elements that return true | |
// for any of the filters. | |
// Ex, filter Non Object elements, | |
// Arrays, and | |
// Empty/null objects | |
// | |
// _.nilty([_.isArray,_.isEmpty])([9,[],[{a:8}],{},{a:8},null]) | |
// | |
// -> [ { a: 8 } ] | |
function nilty(arr_bools){ | |
// Defaults to omit Null, Undefined, Empty Arrays, Empty Objects | |
arr_bools=arr_bools || [_.isNil, _.isEmpty]; | |
arr_bools=_.map(arr_bools,function(filter){ | |
return _.partial(_.omitBy,_,filter); | |
}); | |
arr_bools.push(_.values); | |
return fly(arr_bools); | |
} | |
// ---------------------------------------------------------------------------// | |
// ----------------------------- Sir Mix A lot ------------------------------ // | |
// ---------------------------------------------------------------------------// | |
_.mixin({ | |
fly: fly, | |
then:function(o,f){ | |
o.$promise.then(f); | |
}, | |
timestamps:function(arr,path){ | |
return _.chain(arr) | |
.update(path, function(n) { return new Date(n) }).value(); | |
}, | |
nilty: nilty, | |
// Function with nilty defaults, see nilty | |
clip: nilty(), | |
// Crip iterates over an array of objects and returns them with | |
// Only the k/v pairs that have keys in the "keys" array | |
crip: function(keys,arr){ | |
if(arr){ | |
return _.map(arr,_.crip(keys)); | |
}else{ | |
return _.partial(_.pick,_,keys) | |
} | |
}, | |
// Diff the objects and ignore the k/v pairs with the same value as src obj | |
crimp: function(src,ob){ | |
if(ob){ | |
return _.clip(_.mergeWith(ob,src,ignoreEqual)); | |
}else{ | |
return function(obj){ | |
return _.clip(_.mergeWith(obj,src,ignoreEqual)); | |
} | |
} | |
}, | |
// Diff the objects and ignore the k/v pairs with the same value as src obj | |
crimpAll: function(src,arr){ | |
var crimp=_.crimp(src); | |
return _.map(arr, crimp); | |
}, | |
toLookup:function(data,key_id,value_id){ | |
return _.zipObject(_.spread(_.zip)([key_id,value_id].map(function(d){return _.map(data,d)}))); | |
}, | |
join: function () { | |
var cmp = arguments[arguments.length - 1]; | |
var join = []; | |
_.each(arguments, function (array) { | |
if (_.isFunction(array)) return; | |
_.each(array, function (newObj) { | |
var isMerged = false; | |
_.each(join, function (joinObj, joinIndex) { | |
if (cmp(newObj, joinObj)) { | |
_.each(newObj, function (value, key) { | |
joinObj[key] = value; | |
}); | |
join[joinIndex] = joinObj; | |
isMerged = true; | |
} | |
}); | |
if (!isMerged) { | |
join.push(newObj); | |
} | |
}); | |
}); | |
return join; | |
}, | |
alpha:function(s){return s.replace(/[^a-zA-Z& ,]+/g,'');}, | |
tram:function(s){return _.trim(s,' ')}, | |
sanitize:function(s){return (_.isString(s)) ? _.map(this.alpha(s).split(','),_.tram).map(_.lower) : s;}, | |
lowers:function(s){return s.toLowerCase();}, | |
lower:function(s){return (_.isArray(s) ? _.map(s,_.lowers) : s.toLowerCase());} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment