Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11264 from Uzlopak/perf-setDottetPath
perf: improve setDottedPath
- Loading branch information
Showing
2 changed files
with
56 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
'use strict'; | ||
|
||
module.exports = function setDottedPath(obj, path, val) { | ||
const parts = path.indexOf('.') === -1 ? [path] : path.split('.'); | ||
if (path.indexOf('.') === -1) { | ||
obj[path] = val; | ||
return; | ||
} | ||
const parts = path.split('.'); | ||
const last = parts.pop(); | ||
let cur = obj; | ||
for (const part of parts.slice(0, -1)) { | ||
for (const part of parts) { | ||
if (cur[part] == null) { | ||
cur[part] = {}; | ||
} | ||
|
||
cur = cur[part]; | ||
} | ||
|
||
const last = parts[parts.length - 1]; | ||
cur[last] = val; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const setDottedPath = require('../../lib/helpers/path/setDottedPath'); | ||
|
||
describe('setDottedPath', function() { | ||
it('setDottedPath root element', function() { | ||
const obj = { | ||
clearingInstituteName: 'Our local bank', | ||
'transaction.receipt': 'I am a transaction receipt', | ||
'transaction.authorizationCode': 'ABCDEF', | ||
'transaction.acquirer.settlementDate': 'February 2021', | ||
'sourceOfFunds.provided.card.issuer': 'Big bank corporation', | ||
nonExistentField: 'I should not be present' | ||
}; | ||
setDottedPath(obj, 'authorizationResponse', '123456'); | ||
assert.deepEqual(obj, { | ||
'sourceOfFunds.provided.card.issuer': 'Big bank corporation', | ||
'transaction.acquirer.settlementDate': 'February 2021', | ||
'transaction.authorizationCode': 'ABCDEF', | ||
'transaction.receipt': 'I am a transaction receipt', | ||
authorizationResponse: '123456', | ||
clearingInstituteName: 'Our local bank', | ||
nonExistentField: 'I should not be present' | ||
}); | ||
}); | ||
it('setDottedPath sub element', function() { | ||
const obj = { | ||
clearingInstituteName: 'Our local bank', | ||
'transaction.receipt': 'I am a transaction receipt', | ||
'transaction.authorizationCode': 'ABCDEF', | ||
'transaction.acquirer.settlementDate': 'February 2021', | ||
'sourceOfFunds.provided.card.issuer': 'Big bank corporation', | ||
nonExistentField: 'I should not be present' | ||
}; | ||
setDottedPath(obj, 'authorizationResponse.stan', '123456'); | ||
assert.deepEqual(obj, { | ||
'sourceOfFunds.provided.card.issuer': 'Big bank corporation', | ||
'transaction.acquirer.settlementDate': 'February 2021', | ||
'transaction.authorizationCode': 'ABCDEF', | ||
'transaction.receipt': 'I am a transaction receipt', | ||
authorizationResponse: { | ||
stan: '123456' | ||
}, | ||
clearingInstituteName: 'Our local bank', | ||
nonExistentField: 'I should not be present' | ||
}); | ||
}); | ||
}); |