2017-04-19 00:55:46 +00:00
|
|
|
import curry from './util/curry'
|
|
|
|
import update from './update'
|
|
|
|
import map from './map'
|
|
|
|
import splitPath from './util/splitPath'
|
2015-08-05 05:28:31 +00:00
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
const wildcard = '*'
|
2015-09-12 05:48:02 +00:00
|
|
|
|
|
|
|
function reducePath(acc, key) {
|
|
|
|
if (key === wildcard) {
|
|
|
|
return value =>
|
2018-11-27 17:32:22 +00:00
|
|
|
Object.prototype.hasOwnProperty.call(value, wildcard)
|
2017-04-19 00:55:46 +00:00
|
|
|
? // If we actually have wildcard as a property, update that
|
|
|
|
update({ [wildcard]: acc }, value)
|
|
|
|
: // Otherwise map over all properties
|
2018-11-27 17:32:22 +00:00
|
|
|
map(acc, value)
|
2015-09-12 05:48:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
return { [key]: acc }
|
2015-09-12 05:48:02 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 07:27:56 +00:00
|
|
|
function updateIn(path, value, object) {
|
2017-04-19 00:55:46 +00:00
|
|
|
const parts = splitPath(path)
|
|
|
|
const updates = parts.reduceRight(reducePath, value)
|
2015-08-05 05:28:31 +00:00
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
return update(updates, object)
|
2015-08-05 05:28:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
export default curry(updateIn)
|