forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ccdaservice): test and refactor populateTimezones function (o…
- Loading branch information
1 parent
c98fb92
commit 408ff7c
Showing
3 changed files
with
106 additions
and
22 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
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,40 @@ | ||
'use strict'; | ||
|
||
const DEPTH_LIMIT = 25; | ||
const TIMEZONE_PRECISION = 'tz'; | ||
|
||
function isObject(node) { | ||
return node && typeof node === 'object'; | ||
} | ||
|
||
function isTimezoneDateWithoutOffset(node) { | ||
return ( | ||
node.precision == TIMEZONE_PRECISION && | ||
!Object.prototype.hasOwnProperty.call(node, 'timezoneOffset') | ||
); | ||
} | ||
|
||
// do a recursive descent transformation of the node object, populating the timezone offset value | ||
// if we have a precision property (inside a date) with the value of timezone. | ||
function populateTimezones(node, tzOffset, currentDepth) { | ||
if (!isObject(node)) { | ||
return node; | ||
} | ||
|
||
if (currentDepth > DEPTH_LIMIT) { | ||
console.error( | ||
'Max depth traversal reached. Potential infinite loop. Breaking out of loop.' | ||
); | ||
} else if (isTimezoneDateWithoutOffset(node)) { | ||
node.timezoneOffset = tzOffset; | ||
} else { | ||
for (const [key, value] of Object.entries(node)) { | ||
node[key] = populateTimezones(value, tzOffset, currentDepth + 1); | ||
} | ||
} | ||
return node; | ||
} | ||
|
||
exports.populateTimezones = populateTimezones; | ||
exports.TIMEZONE_PRECISION = TIMEZONE_PRECISION; | ||
exports.DEPTH_LIMIT = DEPTH_LIMIT; |
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,65 @@ | ||
const { | ||
populateTimezones, | ||
TIMEZONE_PRECISION, | ||
DEPTH_LIMIT, | ||
} = require('./timezones'); | ||
|
||
describe('populateTimezones', () => { | ||
const timezoneOffset = 5; | ||
|
||
test.each([null, undefined, 10, false, 'OpenEMR'])( | ||
'should return the node input if the node input is not an object', | ||
(input) => { | ||
expect(populateTimezones(input)).toEqual(input); | ||
} | ||
); | ||
|
||
it(`should set timezoneOffset if node input has precision equals ${TIMEZONE_PRECISION}`, () => { | ||
expect( | ||
populateTimezones({ precision: TIMEZONE_PRECISION }, timezoneOffset) | ||
).toEqual({ | ||
precision: TIMEZONE_PRECISION, | ||
timezoneOffset, | ||
}); | ||
}); | ||
|
||
it('should set timezoneOffset for nested nodes', () => { | ||
const nodes = { | ||
a: { | ||
b: { | ||
c: { | ||
precision: TIMEZONE_PRECISION, | ||
}, | ||
}, | ||
}, | ||
}; | ||
expect(populateTimezones(nodes, timezoneOffset)).toEqual({ | ||
a: { | ||
b: { | ||
c: { | ||
precision: TIMEZONE_PRECISION, | ||
timezoneOffset, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it(`should abort recursion and return node if it hits depth ${DEPTH_LIMIT}`, () => { | ||
// keeping logs clean | ||
jest.spyOn(console, 'error').mockImplementationOnce(() => {}); | ||
const node = getDeepNode(); | ||
expect(populateTimezones(node, timezoneOffset, 0)).toEqual(node); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
|
||
function getDeepNode() { | ||
const node = {}; | ||
let latest = node; | ||
for (let i = 0; i < DEPTH_LIMIT + 1; i++) { | ||
latest[0] = {}; | ||
latest = latest[0]; | ||
} | ||
return node; | ||
} | ||
}); | ||
}); |