2015-08-05 07:25:34 +00:00
|
|
|
import update from './update';
|
2015-08-05 11:29:30 +00:00
|
|
|
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;
|
2015-08-09 07:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function map(iteratee, object) {
|
2015-08-10 06:36:34 +00:00
|
|
|
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-09 07:21:24 +00:00
|
|
|
|
2015-08-12 07:23:51 +00:00
|
|
|
const newObject = mapper(object, updater);
|
|
|
|
const equal = shallowEqual(object, newObject);
|
2015-08-09 07:21:24 +00:00
|
|
|
|
2015-08-12 07:23:51 +00:00
|
|
|
return equal ?
|
|
|
|
object :
|
|
|
|
newObject;
|
2015-08-05 07:25:34 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 11:29:30 +00:00
|
|
|
export default wrap(map);
|