Skip to content

Commit

Permalink
Add convertMonthCodeToName and monthCodeToNumber functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bryaningl3 committed Mar 4, 2021
1 parent 8871e35 commit 1004050
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/content/releases/5.9.0.md
Original file line number Diff line number Diff line change
@@ -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.
31 changes: 31 additions & 0 deletions lib/utilities/convert/monthCodeToName.js
Original file line number Diff line number Diff line change
@@ -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;
})();



31 changes: 31 additions & 0 deletions lib/utilities/convert/monthCodeToNumber.js
Original file line number Diff line number Diff line change
@@ -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;
})();



4 changes: 2 additions & 2 deletions lib/utilities/data/monthCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ module.exports = (() => {

return {
getCodeToNameMap: () => {
return monthMap;
return Object.assign({ }, monthMap);
},

getCodeToNumberMap: () => {
return numberMap;
return Object.assign({ }, numberMap);
}
};
})();
1 change: 1 addition & 0 deletions lib/utilities/data/timezones.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test/specs/utilities/convert/monthCodeToNameSpec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
15 changes: 15 additions & 0 deletions test/specs/utilities/convert/monthCodeToNumberSpec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 1004050

Please sign in to comment.