-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add convertMonthCodeToName and monthCodeToNumber functions
- Loading branch information
1 parent
8871e35
commit 1004050
Showing
7 changed files
with
102 additions
and
2 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 |
---|---|---|
@@ -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. |
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,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; | ||
})(); | ||
|
||
|
||
|
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,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; | ||
})(); | ||
|
||
|
||
|
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
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,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); | ||
}); | ||
}); |
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,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); | ||
}); | ||
}); |