Last active
September 9, 2020 06:11
-
-
Save Rwin90/7f1185247747f2cb45d1302f17b1270c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 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