updeep-remeda/lib/map.js

34 lines
624 B
JavaScript
Raw Normal View History

2015-08-05 07:25:34 +00:00
import update from './update';
import wrap from './wrap';
2015-08-10 06:51:09 +00:00
import forEach from 'lodash/collection/forEach';
2015-08-05 07:25:34 +00:00
function clone(object) {
2015-08-05 07:25:34 +00:00
if (Array.isArray(object)) {
return [...object];
2015-08-05 07:25:34 +00:00
}
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;
2015-08-05 07:25:34 +00:00
}
export default wrap(map);