diff --git a/docs/content/releases/5.9.0.md b/docs/content/releases/5.9.0.md new file mode 100644 index 00000000..5f7196d3 --- /dev/null +++ b/docs/content/releases/5.9.0.md @@ -0,0 +1,7 @@ +**New Features** + +* Added `convert/monthCodeToName.js` and `convert/monthCodeToName.js` functions. + +**Bug Fixes** + +* Updated `monthCodes.getCodeToNameMap` and `monthCodes.getCodeToNumberMap` to return copied objects, thereby protecting internal state from consumers. \ No newline at end of file diff --git a/lib/utilities/convert/monthCodeToName.js b/lib/utilities/convert/monthCodeToName.js new file mode 100644 index 00000000..5811ead3 --- /dev/null +++ b/lib/utilities/convert/monthCodeToName.js @@ -0,0 +1,31 @@ +const is = require('@barchart/common-js/lang/is'); + +const monthCodes = require('./../data/monthCodes'); + +module.exports = (() => { + 'use strict'; + + const map = monthCodes.getCodeToNameMap(); + + /** + * Converts a futures month code to the month number (e.g. "F" to "January", or "N" to "July"). + * + * @function + * @memberOf Functions + * @ignore + * @param {String} monthCode + * @returns {String|null} + */ + function convertMonthCodeToNumber(monthCode) { + if (!is.string(monthCode)) { + return null; + } + + return map[monthCode] || null; + } + + return convertMonthCodeToNumber; +})(); + + + diff --git a/lib/utilities/convert/monthCodeToNumber.js b/lib/utilities/convert/monthCodeToNumber.js new file mode 100644 index 00000000..8941adbf --- /dev/null +++ b/lib/utilities/convert/monthCodeToNumber.js @@ -0,0 +1,31 @@ +const is = require('@barchart/common-js/lang/is'); + +const monthCodes = require('./../data/monthCodes'); + +module.exports = (() => { + 'use strict'; + + const map = monthCodes.getCodeToNumberMap(); + + /** + * Converts a futures month code to the month number (e.g. "F" to 1, or "N" to 7). + * + * @function + * @memberOf Functions + * @ignore + * @param {String} monthCode + * @returns {Number|null} + */ + function convertMonthCodeToNumber(monthCode) { + if (!is.string(monthCode)) { + return null; + } + + return map[monthCode] || null; + } + + return convertMonthCodeToNumber; +})(); + + + diff --git a/lib/utilities/data/monthCodes.js b/lib/utilities/data/monthCodes.js index 1809cdaa..995db5fa 100644 --- a/lib/utilities/data/monthCodes.js +++ b/lib/utilities/data/monthCodes.js @@ -25,11 +25,11 @@ module.exports = (() => { return { getCodeToNameMap: () => { - return monthMap; + return Object.assign({ }, monthMap); }, getCodeToNumberMap: () => { - return numberMap; + return Object.assign({ }, numberMap); } }; })(); \ No newline at end of file diff --git a/lib/utilities/data/timezones.js b/lib/utilities/data/timezones.js index 3e52e325..2e16ae75 100644 --- a/lib/utilities/data/timezones.js +++ b/lib/utilities/data/timezones.js @@ -3,6 +3,7 @@ const timezone = require('@barchart/common-js/lang/timezone'); module.exports = (() => { 'use strict'; + return { /** * Gets a list of names in the tz database (see https://en.wikipedia.org/wiki/Tz_database diff --git a/test/specs/utilities/convert/monthCodeToNameSpec.js b/test/specs/utilities/convert/monthCodeToNameSpec.js new file mode 100644 index 00000000..400abb21 --- /dev/null +++ b/test/specs/utilities/convert/monthCodeToNameSpec.js @@ -0,0 +1,15 @@ +const convertMonthCodeToName = require('./../../../../lib/utilities/convert/monthCodeToName'); + +describe('When converting a futures month code to a month name', () => { + it('The character "F" should translate to "January"', () => { + expect(convertMonthCodeToName('F')).toEqual('January'); + }); + + it('The character "N" should translate to "July"', () => { + expect(convertMonthCodeToName('N')).toEqual('July'); + }); + + it('The character "A" should translate to null value', () => { + expect(convertMonthCodeToName('A')).toBe(null); + }); +}); \ No newline at end of file diff --git a/test/specs/utilities/convert/monthCodeToNumberSpec.js b/test/specs/utilities/convert/monthCodeToNumberSpec.js new file mode 100644 index 00000000..88bafef3 --- /dev/null +++ b/test/specs/utilities/convert/monthCodeToNumberSpec.js @@ -0,0 +1,15 @@ +const convertMonthCodeToNumber = require('./../../../../lib/utilities/convert/monthCodeToNumber'); + +describe('When converting a futures month code to a month name', () => { + it('The character "F" should translate to the number 1', () => { + expect(convertMonthCodeToNumber('F')).toEqual(1); + }); + + it('The character "N" should translate to the number 7', () => { + expect(convertMonthCodeToNumber('N')).toEqual(7); + }); + + it('The character "A" should translate to null value', () => { + expect(convertMonthCodeToNumber('A')).toBe(null); + }); +}); \ No newline at end of file