Skip to content

Instantly share code, notes, and snippets.

@Rwin90
Last active September 9, 2020 06:11
Show Gist options
  • Save Rwin90/7f1185247747f2cb45d1302f17b1270c to your computer and use it in GitHub Desktop.
Save Rwin90/7f1185247747f2cb45d1302f17b1270c to your computer and use it in GitHub Desktop.
/**
* Get an item from an array collection by id
*/
const getByIdFromArray = module.exports.getByIdFromArray = function getByIdFromArray(collection = []) {
return function(id = '') {
return collection.find((item = {}) => item.id === id);
}
}
/**
* Get an item from an object collection by id
*/
const getByIdFromHash = module.exports.getByIdFromHash = function getByIdFromHash(collection = {}) {
return function(id = '') {
return collection[id];
}
}
/**
* Get an item from a collection by id
*/
const getByIdFromCollection = module.exports.getByIdFromCollection = function getByIdFromCollection(collection = []) {
return (
Array.isArray(collection) ?
getByIdFromArray(collection) :
getByIdFromHash(collection)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment