Skip to content

Instantly share code, notes, and snippets.

@sammoore
Created February 9, 2017 21:23
Show Gist options
  • Save sammoore/ab99bb3a66a9c78e01b7c447334817be to your computer and use it in GitHub Desktop.
Save sammoore/ab99bb3a66a9c78e01b7c447334817be to your computer and use it in GitHub Desktop.
Some basic utils. YMMV / not tested.
var utils = {
lists: {
firstOf: function firstOf(collection) {
var keys = Object.keys(collection);
return keys.length > 0 ? collection[keys[0]] : undefined;
},
valueForKey: function valueForKey(collection, key) {
return collection[key];
},
valueForPath: function valueForPath(collection, pathOrKey) {
var path = Array.isArray(pathOrKey) ? pathOrKey : [pathOrKey];
var keys = path.slice();
var value = collection;
do {
value = value[keys.shift()];
} while (keys.length > 0 && value !== undefined);
return value;
},
},
funcs: {
// returns a new function with any additional arguments partially applied to `func`.
// example: var add2 = funcs.partial(function add(x, y) { return x + y }, 2);
// signature: ((Prefix..., Suffix...) => Any, Prefix...) => ((Suffix...) => Any)
partial: function partial(func) {
var prefix = Array.prototype.slice.call(arguments, 1);
return function () {
var suffix = Array.prototype.slice.call(arguments);
return func.apply(null, [].concat(prefix, suffix));
};
},
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment