updeep-remeda/lib/map.js
Aaron Jensen efb669f017 Use lodash's forEach
It's a lot faster
2015-08-10 00:02:49 -07:00

34 lines
624 B
JavaScript

import update from './update';
import wrap from './wrap';
import forEach from 'lodash/collection/forEach';
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);