updeep-remeda/lib/updateIn.js

29 lines
711 B
JavaScript
Raw Normal View History

import curry from './util/curry';
2015-08-05 05:28:31 +00:00
import update from './update';
import map from './map';
2015-08-07 03:48:04 +00:00
import splitPath from './util/splitPath';
2015-08-05 05:28:31 +00:00
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 };
}
2015-08-05 07:27:56 +00:00
function updateIn(path, value, object) {
2015-08-07 03:48:04 +00:00
const parts = splitPath(path);
const updates = parts.reduceRight(reducePath, value);
2015-08-05 05:28:31 +00:00
2015-08-05 07:27:56 +00:00
return update(updates, object);
2015-08-05 05:28:31 +00:00
}
export default curry(updateIn);