updeep-remeda/lib/map.js

39 lines
799 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-12 07:23:51 +00:00
import mapArray from 'lodash/collection/map';
import mapObject from 'lodash/object/mapValues';
function shallowEqual(object, otherObject) {
let equal = true;
forEach(otherObject, (value, key) => {
if (value !== object[key]) {
equal = false;
// exit early
return false;
}
});
2015-08-05 07:25:34 +00:00
2015-08-12 07:23:51 +00:00
return equal;
}
function map(iteratee, object) {
const updater = typeof iteratee === 'function' ?
iteratee :
update(iteratee);
2015-08-12 07:23:51 +00:00
const mapper = Array.isArray(object) ?
mapArray :
mapObject;
2015-08-12 07:23:51 +00:00
const newObject = mapper(object, updater);
const equal = shallowEqual(object, newObject);
2015-08-12 07:23:51 +00:00
return equal ?
object :
newObject;
2015-08-05 07:25:34 +00:00
}
export default wrap(map);