updeep-remeda/lib/map.js
Aaron Jensen 167760fb24 Do not use update in map if iteratee is a function
Performance bump, especially while curry is slow.
2015-08-10 00:02:49 -07:00

41 lines
761 B
JavaScript

import update from './update';
import wrap from './wrap';
function forEach(object, callback) {
if (Array.isArray(object)) {
object.forEach(callback);
} else {
Object.keys(object).forEach((key) => callback(object[key], key));
}
}
function clone(object) {
if (Array.isArray(object)) {
return [...object];
}
return { ...object };
}
function map(iteratee, object) {
const updater = typeof iteratee === 'function' ?
iteratee :
update(iteratee);
let newObject;
forEach(object, (value, index) => {
const newValue = updater(value, index);
if (newValue === value) return;
newObject = newObject || clone(object);
newObject[index] = newValue;
});
return newObject || object;
}
export default wrap(map);