currency-in-words
Advanced tools
Comparing version 1.0.0 to 1.1.0
const { convert } = require('../src/index') | ||
describe('Convert', () => { | ||
it('returns "NaN" for undefined', () => { | ||
const result = convert(undefined) | ||
expect(result).toEqual('NaN') | ||
}) | ||
it('returns "NaN" for null', () => { | ||
const result = convert(null) | ||
expect(result).toEqual('NaN') | ||
}) | ||
it('returns "NaN" for an invalid number(a1.01)', () => { | ||
const result = convert('a1.01') | ||
expect(result).toEqual('NaN') | ||
}) | ||
it('returns "NaN" for an invalid number(1.1a)', () => { | ||
const result = convert('1.1a') | ||
expect(result).toEqual('NaN') | ||
}) | ||
it('returns "NaN" for an invalid number(random string)', () => { | ||
const result = convert('random string') | ||
expect(result).toEqual('NaN') | ||
}) | ||
it('returns "one" when given the input 1', () => { | ||
@@ -84,6 +104,204 @@ const result = convert('1') | ||
}) | ||
it('returns "one hundred nine hundred" when given the input 1900', () => { | ||
it('returns "one thousand nine hundred" when given the input 1900', () => { | ||
const result = convert('1900') | ||
expect(result).toEqual('one thousand nine hundred') | ||
}) | ||
}) | ||
it('returns "one thousand nine hundred ninety nine" when given the input 1999', () => { | ||
const result = convert('1999') | ||
expect(result).toEqual('one thousand nine hundred ninety nine') | ||
}) | ||
it('returns "ten thousand" when given the input 10000', () => { | ||
const result = convert('10000') | ||
expect(result).toEqual('ten thousand') | ||
}) | ||
it('returns "eleven thousand one hundred nine" when given the input 11109', () => { | ||
const result = convert('11109') | ||
expect(result).toEqual('eleven thousand one hundred nine') | ||
}) | ||
it('returns "eleven thousand one hundred ten" when given the input 11110', () => { | ||
const result = convert('11110') | ||
expect(result).toEqual('eleven thousand one hundred ten') | ||
}) | ||
it('returns "eleven thousand one hundred eleven" when given the input 11111', () => { | ||
const result = convert('11111') | ||
expect(result).toEqual('eleven thousand one hundred eleven') | ||
}) | ||
it('returns "eleven thousand one hundred nineteen" when given the input 11119', () => { | ||
const result = convert('11119') | ||
expect(result).toEqual('eleven thousand one hundred nineteen') | ||
}) | ||
it('returns "eleven thousand one hundred ninety" when given the input 11190', () => { | ||
const result = convert('11190') | ||
expect(result).toEqual('eleven thousand one hundred ninety') | ||
}) | ||
it('returns "eleven thousand one hundred ninety nine" when given the input 11199', () => { | ||
const result = convert('11199') | ||
expect(result).toEqual('eleven thousand one hundred ninety nine') | ||
}) | ||
it('returns "one lakh" when given the input 1,00,000', () => { | ||
const result = convert('100000') | ||
expect(result).toEqual('one lakh') | ||
}) | ||
it('returns "one lakh eleven thousand one hundred nine" when given the input 1,11,109', () => { | ||
const result = convert('111109') | ||
expect(result).toEqual('one lakh eleven thousand one hundred nine') | ||
}) | ||
it('returns "one lakh eleven thousand one hundred ten" when given the input 1,11,110', () => { | ||
const result = convert('111110') | ||
expect(result).toEqual('one lakh eleven thousand one hundred ten') | ||
}) | ||
it('returns "one lakh eleven thousand one hundred eleven" when given the input 1,11,111', () => { | ||
const result = convert('111111') | ||
expect(result).toEqual('one lakh eleven thousand one hundred eleven') | ||
}) | ||
it('returns "one lakh eleven thousand one hundred nineteen" when given the input 1,11,119', () => { | ||
const result = convert('111119') | ||
expect(result).toEqual('one lakh eleven thousand one hundred nineteen') | ||
}) | ||
it('returns "one lakh leven thousand one hundred ninety" when given the input 1,11,190', () => { | ||
const result = convert('111190') | ||
expect(result).toEqual('one lakh eleven thousand one hundred ninety') | ||
}) | ||
it('returns "one lakh eleven thousand one hundred ninety nine" when given the input 1,11,199', () => { | ||
const result = convert('111199') | ||
expect(result).toEqual('one lakh eleven thousand one hundred ninety nine') | ||
}) | ||
it('returns "ten lakh" when given the input 10,00,000', () => { | ||
const result = convert('1000000') | ||
expect(result).toEqual('ten lakh') | ||
}) | ||
it('returns "ten lakh eleven thousand one hundred nine" when given the input 10,11,109', () => { | ||
const result = convert('1011109') | ||
expect(result).toEqual('ten lakh eleven thousand one hundred nine') | ||
}) | ||
it('returns "ten lakh eleven thousand one hundred ten" when given the input 10,11,110', () => { | ||
const result = convert('1011110') | ||
expect(result).toEqual('ten lakh eleven thousand one hundred ten') | ||
}) | ||
it('returns "ten lakh eleven thousand one hundred eleven" when given the input 10,11,111', () => { | ||
const result = convert('1011111') | ||
expect(result).toEqual('ten lakh eleven thousand one hundred eleven') | ||
}) | ||
it('returns "ten lakh eleven thousand one hundred nineteen" when given the input 10,11,119', () => { | ||
const result = convert('1011119') | ||
expect(result).toEqual('ten lakh eleven thousand one hundred nineteen') | ||
}) | ||
it('returns "ten lakh leven thousand one hundred ninety" when given the input 10,11,190', () => { | ||
const result = convert('1011190') | ||
expect(result).toEqual('ten lakh eleven thousand one hundred ninety') | ||
}) | ||
it('returns "ten lakh eleven thousand one hundred ninety nine" when given the input 10,11,199', () => { | ||
const result = convert('1011199') | ||
expect(result).toEqual('ten lakh eleven thousand one hundred ninety nine') | ||
}) | ||
it('returns "one crore" when given the input 1,00,00,000', () => { | ||
const result = convert('10000000') | ||
expect(result).toEqual('one crore') | ||
}) | ||
it('returns "one crore one lakh eleven thousand one hundred nine" when given the input 1,01,11,109', () => { | ||
const result = convert('10111109') | ||
expect(result).toEqual('one crore one lakh eleven thousand one hundred nine') | ||
}) | ||
it('returns "one crore one lakh eleven thousand one hundred ten" when given the input 1,01,11,110', () => { | ||
const result = convert('10111110') | ||
expect(result).toEqual('one crore one lakh eleven thousand one hundred ten') | ||
}) | ||
it('returns "one crore one lakh eleven thousand one hundred eleven" when given the input 1,01,11,111', () => { | ||
const result = convert('10111111') | ||
expect(result).toEqual('one crore one lakh eleven thousand one hundred eleven') | ||
}) | ||
it('returns "oone crore ne lakh eleven thousand one hundred nineteen" when given the input 1,11,119', () => { | ||
const result = convert('10111119') | ||
expect(result).toEqual('one crore one lakh eleven thousand one hundred nineteen') | ||
}) | ||
it('returns "one crore one lakh leven thousand one hundred ninety" when given the input 1,11,190', () => { | ||
const result = convert('10111190') | ||
expect(result).toEqual('one crore one lakh eleven thousand one hundred ninety') | ||
}) | ||
it('returns "one crore one lakh eleven thousand one hundred ninety nine" when given the input 1,11,199', () => { | ||
const result = convert('10111199') | ||
expect(result).toEqual('one crore one lakh eleven thousand one hundred ninety nine') | ||
}) | ||
it('returns "ten crore ten lakh" when given the input 10,00,00,000', () => { | ||
const result = convert('101000000') | ||
expect(result).toEqual('ten crore ten lakh') | ||
}) | ||
it('returns "tten crore en lakh eleven thousand one hundred nine" when given the input 10,00,11,109', () => { | ||
const result = convert('101011109') | ||
expect(result).toEqual('ten crore ten lakh eleven thousand one hundred nine') | ||
}) | ||
it('returns "ten crore ten lakh eleven thousand one hundred ten" when given the input 10,00,11,110', () => { | ||
const result = convert('101011110') | ||
expect(result).toEqual('ten crore ten lakh eleven thousand one hundred ten') | ||
}) | ||
it('returns "ten crore ten lakh eleven thousand one hundred eleven" when given the input 10,00,11,111', () => { | ||
const result = convert('101011111') | ||
expect(result).toEqual('ten crore ten lakh eleven thousand one hundred eleven') | ||
}) | ||
it('returns "ten crore ten lakh eleven thousand one hundred nineteen" when given the input 10,00,11,119', () => { | ||
const result = convert('101011119') | ||
expect(result).toEqual('ten crore ten lakh eleven thousand one hundred nineteen') | ||
}) | ||
it('returns "ten crore ten lakh leven thousand one hundred ninety" when given the input 10,00,11,190', () => { | ||
const result = convert('101011190') | ||
expect(result).toEqual('ten crore ten lakh eleven thousand one hundred ninety') | ||
}) | ||
it('returns "ten crore ten lakh eleven thousand one hundred ninety nine" when given the input 10,00,11,199', () => { | ||
const result = convert('101011199') | ||
expect(result).toEqual('ten crore ten lakh eleven thousand one hundred ninety nine') | ||
}) | ||
/** | ||
* Logically everything should work fine with the tests above, but some random numbers, just for fool proofing.. | ||
*/ | ||
it('returns "five hundred fifty five" when given the input 555', () => { | ||
const result = convert('555') | ||
expect(result).toEqual('five hundred fifty five') | ||
}) | ||
it('returns "seventy seven thousand two hundred eighty nine" when given the input 77,289', () => { | ||
const result = convert('77289') | ||
expect(result).toEqual('seventy seven thousand two hundred eighty nine') | ||
}) | ||
it('returns "seventeen crore ninety nine lakh one hundred" when given the input 17,99,00,100', () => { | ||
const result = convert('179900100') | ||
expect(result).toEqual('seventeen crore ninety nine lakh one hundred') | ||
}) | ||
it('returns "ninety nine crore ninety nine lakh ninety nine thousand nine hundred ninety nine" when given the input 99,99,99,999', () => { | ||
const result = convert('999999999') | ||
expect(result).toEqual('ninety nine crore ninety nine lakh ninety nine thousand nine hundred ninety nine') | ||
}) | ||
/** | ||
* And few more.. | ||
*/ | ||
it('returns "one.one" when given the input 1.1', () => { | ||
const result = convert('1.1') | ||
expect(result).toEqual('one.one') | ||
}) | ||
it('returns "fifty seven.ninety nine" when given the input 57.99', () => { | ||
const result = convert('57.99') | ||
expect(result).toEqual('fifty seven.ninety nine') | ||
}) | ||
it('returns "ten" when given the input 10.01; neglect x.01 to x.09', () => { | ||
const result = convert('10.01') | ||
expect(result).toEqual('ten') | ||
}) | ||
it('returns "zero" when given the input 0.00', () => { | ||
const result = convert('0.00') | ||
expect(result).toEqual('zero') | ||
}) | ||
it('returns "zero" when given the input 000', () => { | ||
const result = convert('000') | ||
expect(result).toEqual('zero') | ||
}) | ||
it('returns "three hundred thirty" when given the input 00330; corrected leading zeros', () => { | ||
const result = convert('00330') | ||
expect(result).toEqual('three hundred thirty') | ||
}) | ||
it('returns "eighty eight.twenty" when given the input 088.200; corrected leading zeros and two decimal places', () => { | ||
const result = convert('088.200') | ||
expect(result).toEqual('eighty eight.twenty') | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
"use strict" | ||
"use strict" | ||
@@ -13,2 +13,9 @@ export const singleDigit: Array<string> = [ | ||
function isFirstCharZero(key: string): boolean { | ||
return +key[0] === 0 | ||
} | ||
function isSecondCharZero(key: string): boolean { | ||
return +key[1] === 0 | ||
} | ||
/** | ||
@@ -24,44 +31,63 @@ * Pass "key" as string for function argument, but cast to number (+key) when used as Array<string> index | ||
else | ||
return `${twoDigit[+key[0]]} ${singleDigit[+key.slice(-1)]}` | ||
return `${twoDigit[+key[0]]} ${singleDigit[+key.slice(-1)]}` | ||
} | ||
export function handleTens(key: string): string { | ||
if (+key[1] === 0) return `${twoDigit[+key[0]]}` | ||
else return handleTeens(key) | ||
if (isFirstCharZero(key)) | ||
return `${singleDigit[+key[1]]}` | ||
if (isSecondCharZero(key)) | ||
return `${twoDigit[+key[0]]}` | ||
return handleTeens(key) | ||
} | ||
export function handleHundreds(key: string): string { | ||
if (isFirstCharZero(key)) | ||
return `${handleTens(key.substring(1))}` | ||
if (key.substring(1) === '00') | ||
return `${singleDigit[+key[0]]} hundred` | ||
if (+key[0] === 0 && +key[1] === 0) | ||
return `${handleOnes(key[2])}` | ||
if (+key[1] === 0) | ||
return `${handleOnes(key[0])} hundred ${handleOnes(key.slice(-1))}` | ||
if (+key[0] === 0) | ||
return `${handleTens(key.substring(1))}` | ||
return `${handleOnes(key[0])} hundred ${handleTens(key.substring(1))}` | ||
return `${singleDigit[+key[0]]} hundred ${handleTens(key.substring(1))}` | ||
} | ||
export function handleThousands(key: string): string { | ||
if (key.substring(1) === '000') | ||
return `${singleDigit[+key[0]]} thousand` | ||
if (key.match(/^.[0][0]/)) | ||
return `${singleDigit[+key[0]]} thousand ${singleDigit[+key.slice(-1)]}` | ||
if (+key[1] === 0) | ||
return `${singleDigit[+key[0]]} thousand ${handleTens(key.substring(2))}` | ||
if (isFirstCharZero(key)) | ||
return `${handleHundreds(key.substring(1))}` | ||
if (isSecondCharZero(key)) | ||
return `${singleDigit[+key[0]]} thousand ${handleHundreds(key.substring(1))}` | ||
return `${singleDigit[+key[0]]} thousand ${handleHundreds(key.substring(1))}` | ||
} | ||
export function handleTenThousands(key: string): string { | ||
if (+key[1] === 0) | ||
if (isFirstCharZero(key)) | ||
return `${handleThousands(key.substring(1))}` | ||
if (isSecondCharZero(key)) | ||
return `${twoDigit[+key[0]]} thousand ${handleHundreds(key.substring(2))}` | ||
return `${handleTens(key.substring(0, 2))} thousand ${handleHundreds(key.slice(-3))}` | ||
} | ||
export function handleLakh(key: string): string { | ||
if (isFirstCharZero(key)) | ||
return `${handleTenThousands(key.substring(1))}` | ||
return `${singleDigit[+key[0]]} lakh ${handleTenThousands(key.substring(1))}` | ||
} | ||
export function handleTenLakh(key: string): string { | ||
if (isFirstCharZero(key)) | ||
return `${handleLakh(key.substring(1))}` | ||
if (isSecondCharZero(key)) | ||
return `${twoDigit[+key[0]]} lakh ${handleTenThousands(key.substring(2))}` | ||
return `${handleTens(key.substring(0, 2))} lakh ${handleTenThousands(key.substring(2))}` | ||
} | ||
export function handleCrore(key: string): string { | ||
if (isFirstCharZero(key)) | ||
return `${handleTenLakh(key.substring(1))}` | ||
return `${singleDigit[+key[0]]} crore ${handleTenLakh(key.substring(1))}` | ||
} | ||
export function handleTenCrore(key: string): string { | ||
return `${handleTens(key.substring(0, 2))} ${handleCrore(key.substring(2))}` | ||
if (isSecondCharZero(key)) | ||
return `${twoDigit[+key[0]]} crore ${handleTenLakh(key.substring(2))}` | ||
return `${handleTens(key.substring(0, 2))} crore ${handleTenLakh(key.substring(2))}` | ||
} | ||
// not handling more than Cr.10 for now, but promising there will be an updated version.. |
{ | ||
"name": "currency-in-words", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "convert currency or any numbers to corresponding words, eg: 1 -> \"one\", 10 -> \"ten\", 101 -> \"one hundred and one\" etc...", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
# currency-in-words | ||
**Convert currency or any number formats to respective words** | ||
___ | ||
### __For local installation and testing__ | ||
```javascript | ||
import convert from 'currency-in-words' | ||
```javascript | ||
cd package | ||
npm link | ||
cd ../ | ||
mkdir test-folder | ||
cd test-folder | ||
npm link currency-in-words | ||
touch script.js | ||
const result = convert('777') | ||
``` | ||
> script.js | ||
Adding test-case results just to make your life easier.. | ||
```javascript | ||
const convert = require('currency-in-words') | ||
<img src="./public/test1.png"> | ||
console.log('test 5', convert('5')) | ||
``` | ||
<img src="./public/test2.png"> |
@@ -7,11 +7,17 @@ const { | ||
interface objectMap { | ||
interface valueMap { | ||
[key: number]: string | Function | ||
} | ||
export function convert(key: string): string { | ||
if (!key) return '' | ||
if (key === '0') return 'zero' | ||
export function convert(value: string): string { | ||
const regex = /^\d*(\.\d+)?$/ | ||
// handle null and undefined.. | ||
if (!value || !value.match(regex)) return 'NaN' | ||
const indian: objectMap = { | ||
const [integer, fraction] = value.split('.') | ||
const zeroCorrected = '' + parseInt(integer, 10) | ||
if (zeroCorrected === '0') return 'zero' | ||
const indian: valueMap = { | ||
0: '', | ||
@@ -29,3 +35,9 @@ 1: handleOnes, | ||
return (indian[key.length] as Function)(key) | ||
const integerResult = (indian[zeroCorrected.length] as Function)(zeroCorrected) | ||
let result = integerResult | ||
if (fraction && +fraction[0] !== 0) // neglect .01 - .09 | ||
result = `${result}.${(indian[fraction.substring(0, 2).length] as Function)(fraction.substring(0, 2))}` // limit to two decimal places. | ||
return result.trim() | ||
} |
818573
13
533
17