updeep-remeda/lib/updateIn.js
Aaron Jensen eb4868775a
Build on node 8, 10 and latest and install latest yarn (#79)
* Update packages to fix build

* Remove nsp, it is deprecated
2018-11-27 09:32:22 -08:00

29 lines
704 B
JavaScript

import curry from './util/curry'
import update from './update'
import map from './map'
import splitPath from './util/splitPath'
const wildcard = '*'
function reducePath(acc, key) {
if (key === wildcard) {
return value =>
Object.prototype.hasOwnProperty.call(value, wildcard)
? // If we actually have wildcard as a property, update that
update({ [wildcard]: acc }, value)
: // Otherwise map over all properties
map(acc, value)
}
return { [key]: acc }
}
function updateIn(path, value, object) {
const parts = splitPath(path)
const updates = parts.reduceRight(reducePath, value)
return update(updates, object)
}
export default curry(updateIn)