Comparing version 1.1.0 to 1.2.0
26
index.js
@@ -8,18 +8,26 @@ /* eslint-disable eqeqeq */ | ||
module.exports = function numWords (num) { | ||
if ((num = num.toString()).length > 9) { | ||
const getLT20 = (n) => a[Number(n)] | ||
const get20Plus = (n) => b[n[0]] + ' ' + a[n[1]] | ||
module.exports = function numWords (input) { | ||
const num = Number(input) | ||
if (isNaN(num)) return '' | ||
if (num === 0) return 'zero' | ||
const numStr = num.toString() | ||
if (numStr.length > 9) { | ||
throw new Error('overflow') // Does not support converting more than 9 digits yet | ||
} | ||
const n = ('000000000' + num).substr(-9).match(regex) | ||
if (!n) return | ||
const [, n1, n2, n3, n4, n5] = ('000000000' + numStr).substr(-9).match(regex) // left pad zeros | ||
let str = '' | ||
str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '' | ||
str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '' | ||
str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '' | ||
str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '' | ||
str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) : '' | ||
str += n1 != 0 ? (getLT20(n1) || get20Plus(n1)) + 'crore ' : '' | ||
str += n2 != 0 ? (getLT20(n2) || get20Plus(n2)) + 'lakh ' : '' | ||
str += n3 != 0 ? (getLT20(n3) || get20Plus(n3)) + 'thousand ' : '' | ||
str += n4 != 0 ? getLT20(n4) + 'hundred ' : '' | ||
str += n5 != 0 && str != '' ? 'and ' : '' | ||
str += n5 != 0 ? (getLT20(n5) || get20Plus(n5)) : '' | ||
return str.trim() | ||
} |
{ | ||
"name": "num-words", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Convert digits to words", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
89
test.js
@@ -1,5 +0,9 @@ | ||
const { test } = require('tap') | ||
const { test, only } = require('tap') | ||
const numWords = require('.') | ||
only('test playground', (t) => { | ||
t.end() | ||
}) | ||
test('Should convert numbers to words correctly', (t) => { | ||
@@ -31,6 +35,87 @@ const assertNum = (num, words) => { | ||
assertNum(3, 'three') | ||
assertNum(34, 'thirty four') | ||
assertNum(345, 'three hundred and forty five') | ||
assertNum(3456, 'three thousand four hundred and fifty six') | ||
assertNum(34567, 'thirty four thousand five hundred and sixty seven') | ||
assertNum(345678, 'three lakh forty five thousand six hundred and seventy eight') | ||
assertNum(3456789, 'thirty four lakh fifty six thousand seven hundred and eighty nine') | ||
assertNum(34567890, 'three crore forty five lakh sixty seven thousand eight hundred and ninety') | ||
assertNum(345678901, 'thirty four crore fifty six lakh seventy eight thousand nine hundred and one') | ||
// add more | ||
assertNum(4, 'four') | ||
assertNum(45, 'forty five') | ||
assertNum(456, 'four hundred and fifty six') | ||
assertNum(4567, 'four thousand five hundred and sixty seven') | ||
assertNum(45678, 'forty five thousand six hundred and seventy eight') | ||
assertNum(456789, 'four lakh fifty six thousand seven hundred and eighty nine') | ||
assertNum(4567890, 'forty five lakh sixty seven thousand eight hundred and ninety') | ||
assertNum(45678901, 'four crore fifty six lakh seventy eight thousand nine hundred and one') | ||
assertNum(456789012, 'forty five crore sixty seven lakh eighty nine thousand and twelve') | ||
assertNum(5, 'five') | ||
assertNum(56, 'fifty six') | ||
assertNum(567, 'five hundred and sixty seven') | ||
assertNum(5678, 'five thousand six hundred and seventy eight') | ||
assertNum(56789, 'fifty six thousand seven hundred and eighty nine') | ||
assertNum(567890, 'five lakh sixty seven thousand eight hundred and ninety') | ||
assertNum(5678901, 'fifty six lakh seventy eight thousand nine hundred and one') | ||
assertNum(56789012, 'five crore sixty seven lakh eighty nine thousand and twelve') | ||
assertNum(567890123, 'fifty six crore seventy eight lakh ninety thousand one hundred and twenty three') | ||
assertNum(6, 'six') | ||
assertNum(67, 'sixty seven') | ||
assertNum(678, 'six hundred and seventy eight') | ||
assertNum(6789, 'six thousand seven hundred and eighty nine') | ||
assertNum(67890, 'sixty seven thousand eight hundred and ninety') | ||
assertNum(678901, 'six lakh seventy eight thousand nine hundred and one') | ||
assertNum(6789012, 'sixty seven lakh eighty nine thousand and twelve') | ||
assertNum(67890123, 'six crore seventy eight lakh ninety thousand one hundred and twenty three') | ||
assertNum(678901234, 'sixty seven crore eighty nine lakh one thousand two hundred and thirty four') | ||
assertNum(7, 'seven') | ||
assertNum(78, 'seventy eight') | ||
assertNum(789, 'seven hundred and eighty nine') | ||
assertNum(7890, 'seven thousand eight hundred and ninety') | ||
assertNum(78901, 'seventy eight thousand nine hundred and one') | ||
assertNum(789012, 'seven lakh eighty nine thousand and twelve') | ||
assertNum(7890123, 'seventy eight lakh ninety thousand one hundred and twenty three') | ||
assertNum(78901234, 'seven crore eighty nine lakh one thousand two hundred and thirty four') | ||
assertNum(789012345, 'seventy eight crore ninety lakh twelve thousand three hundred and forty five') | ||
assertNum(8, 'eight') | ||
assertNum(89, 'eighty nine') | ||
assertNum(890, 'eight hundred and ninety') | ||
assertNum(8901, 'eight thousand nine hundred and one') | ||
assertNum(89012, 'eighty nine thousand and twelve') | ||
assertNum(890123, 'eight lakh ninety thousand one hundred and twenty three') | ||
assertNum(8901234, 'eighty nine lakh one thousand two hundred and thirty four') | ||
assertNum(89012345, 'eight crore ninety lakh twelve thousand three hundred and forty five') | ||
assertNum(890123456, 'eighty nine crore one lakh twenty three thousand four hundred and fifty six') | ||
assertNum(9, 'nine') | ||
assertNum(90, 'ninety') | ||
assertNum(901, 'nine hundred and one') | ||
assertNum(9012, 'nine thousand and twelve') | ||
assertNum(90123, 'ninety thousand one hundred and twenty three') | ||
assertNum(901234, 'nine lakh one thousand two hundred and thirty four') | ||
assertNum(9012345, 'ninety lakh twelve thousand three hundred and forty five') | ||
assertNum(90123456, 'nine crore one lakh twenty three thousand four hundred and fifty six') | ||
assertNum(901234567, 'ninety crore twelve lakh thirty four thousand five hundred and sixty seven') | ||
assertNum(0, 'zero') | ||
assertNum('0', 'zero') | ||
assertNum('012', 'twelve') | ||
assertNum('0123', 'one hundred and twenty three') | ||
assertNum('01234', 'one thousand two hundred and thirty four') | ||
assertNum('012345', 'twelve thousand three hundred and forty five') | ||
assertNum('0123456', 'one lakh twenty three thousand four hundred and fifty six') | ||
assertNum('012345678', 'one crore twenty three lakh forty five thousand six hundred and seventy eight') | ||
assertNum('bad', '') | ||
assertNum('12x', '') | ||
assertNum(1e3, 'one thousand') | ||
t.throws(() => numWords(1e9)) | ||
t.end() | ||
}) |
10828
128