From 96ca6cd77f67954dc7809c61e47a38951e338bf0 Mon Sep 17 00:00:00 2001 From: Isaac Grynsztein Date: Fri, 18 Oct 2019 18:29:53 -0400 Subject: [PATCH] Added ability to convert numbers to numeral adverbs (e.g. 'once', 'twice', 'thrice', 'four times', etc. --- src/index.js | 3 ++- src/toWordsNumeralAdverb.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/toWordsNumeralAdverb.js diff --git a/src/index.js b/src/index.js index 54198b8..e03e725 100644 --- a/src/index.js +++ b/src/index.js @@ -3,5 +3,6 @@ module.exports = { toOrdinal: require('./toOrdinal'), toWords: require('./toWords'), - toWordsOrdinal: require('./toWordsOrdinal') + toWordsOrdinal: require('./toWordsOrdinal'), + toWordsNumeralAdverb: require('./toWordsNumeralAdverb') }; diff --git a/src/toWordsNumeralAdverb.js b/src/toWordsNumeralAdverb.js new file mode 100644 index 0000000..c38f4e6 --- /dev/null +++ b/src/toWordsNumeralAdverb.js @@ -0,0 +1,29 @@ +'use strict'; + +var toWords = require('./toWords'); + +numeralAdverbsLessThanFour = { + one: 'once', + two: 'twice', + three: 'thrice' +} + +/** + * Converts a number into a numeral adverb. + * @example toWordsNumeralAdverb(12) => 'twelve times' + * @example toWordsNumeralAdverb(3, false) => 'thrice' + * @example toWordsNumeralAdverb(3, true) => 'three times' + * @param {number|string} number + * @param {boolean} ignoreThrice if thrice should be ignored for those who don't like it + * @returns {string} + */ +function toWordsNumeralAdverb(number, ignoreThrice = false) { + var words = toWords(number); + if (numeralAdverbsLessThanFour.hasOwnProperty(words) && (number !== 3 || !ignoreThrice)) { + return numeralAdverbsLessThanFour[words]; + } else { + return words + ' times'; + } +} + +module.exports = toWordsNumeralAdverb; \ No newline at end of file