updeep-remeda/lib/map.js

39 lines
770 B
JavaScript
Raw Normal View History

2015-08-05 07:25:34 +00:00
import update from './update';
import wrap from './wrap';
2016-01-14 16:02:59 +00:00
import forEach from 'lodash/forEach';
import mapArray from 'lodash/map';
import mapObject from 'lodash/mapValues';
2015-08-12 07:23:51 +00:00
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);