Skip to content

Instantly share code, notes, and snippets.

@eternal44
Created December 9, 2015 02:16
Show Gist options
  • Save eternal44/d0ec4e64beaa42a41752 to your computer and use it in GitHub Desktop.
Save eternal44/d0ec4e64beaa42a41752 to your computer and use it in GitHub Desktop.
transform (map) function
var transform = function(collection, predicate){
if (Array.isArray(collection)){
return collection.map(predicate);
} else if (typeof collection === 'string'){
collection = collection.split('');
return collection.map(predicate).join('');
} else if (typeof collection === 'object'){
return Object.keys(collection).reduce(function(results, property){
results[property] = predicate(collection[property]);
return results;
}, {});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment