Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,20 @@ DotObject.prototype.set = function (path, val, obj, merge) {
return obj
}

/**
*
* Use given function to update a property on an object using dot notation.
*
* @param {String} path
* @param {Function} fn
* @param {Object} obj
*/
DotObject.prototype.updateIn = function (path, fn, obj) {
var originalValue = this.pick(path, obj)
this.set(path, fn(originalValue), obj)
return obj
}

/**
*
* Transform an object
Expand Down Expand Up @@ -479,6 +493,7 @@ DotObject.copy = wrap('copy')
DotObject.object = wrap('object')
DotObject.str = wrap('str')
DotObject.set = wrap('set')
DotObject.updateIn = wrap('updateIn')
DotObject.del = DotObject.remove = wrap('remove')
DotObject.dot = wrap('dot')

Expand Down
18 changes: 18 additions & 0 deletions test/update-in.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

require('should')
var Dot = require('../index')

describe('updateIn:', function () {
it('Should be able to update value at given path using given function', function () {
var obj = {
counter: 1
}

Dot.updateIn('counter', function (currentValue) {
return currentValue + 1;
}, obj)

obj.should.eql({counter: 2})
})
})