updeep-remeda/lib/map.js

34 lines
778 B
JavaScript
Raw Normal View History

2017-04-19 00:55:46 +00:00
import forEach from 'lodash/forEach'
import mapArray from 'lodash/map'
import mapObject from 'lodash/mapValues'
import update from './update'
import wrap from './wrap'
2015-08-12 07:23:51 +00:00
function shallowEqual(object, otherObject) {
2017-04-19 00:55:46 +00:00
let equal = true
2017-04-19 00:47:53 +00:00
// eslint-disable-next-line consistent-return
2015-08-12 07:23:51 +00:00
forEach(otherObject, (value, key) => {
if (value !== object[key]) {
2017-04-19 00:55:46 +00:00
equal = false
2015-08-12 07:23:51 +00:00
// exit early
2017-04-19 00:55:46 +00:00
return false
2015-08-12 07:23:51 +00:00
}
2017-04-19 00:55:46 +00:00
})
2015-08-05 07:25:34 +00:00
2017-04-19 00:55:46 +00:00
return equal
}
function map(iteratee, object) {
2017-04-19 00:55:46 +00:00
const updater = typeof iteratee === 'function' ? iteratee : update(iteratee)
2017-04-19 00:55:46 +00:00
const mapper = Array.isArray(object) ? mapArray : mapObject
2017-04-19 00:55:46 +00:00
const newObject = mapper(object, updater)
const equal = shallowEqual(object, newObject)
2017-04-19 00:55:46 +00:00
return equal ? object : newObject
2015-08-05 07:25:34 +00:00
}
2017-04-19 00:55:46 +00:00
export default wrap(map)