Last active
December 28, 2018 11:18
-
-
Save jasdeepkhalsa/f70854fa1557d223932f494d74c1e4fa to your computer and use it in GitHub Desktop.
Number to Words Convertor
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
import { toWords } from 'number-to-words'; | |
export function numberToWords(number) { | |
if (!number || number === true) { | |
return number; | |
} | |
const numWithNoNonDigits = number.replace ? number.replace(/\D/g,'') : number; | |
const numToArray = numWithNoNonDigits.toString().split(''); | |
if (Number.isNaN(parseInt(numWithNoNonDigits))) { | |
return number; | |
} | |
let result = ''; | |
numToArray.forEach((item, index, array) => { | |
if (index !== array.length - 1) { | |
result += `${toWords(item)} `; | |
} | |
else { | |
result += `${toWords(item)}`; | |
} | |
}); | |
return result; | |
} |
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
import { numberToWords } from './numberToWords'; | |
describe('numberToWords', () => { | |
it('should return words for an international number string', () => { | |
const INPUT = '+44 000 123 456'; | |
const EXPECTED = 'four four zero zero zero one two three four five six'; | |
expect(numberToWords(INPUT)).toEqual(EXPECTED); | |
}); | |
it('should return words for a local number string', () => { | |
const INPUT = '0800 000 1234'; | |
const EXPECTED = 'zero eight zero zero zero zero zero one two three four'; | |
expect(numberToWords(INPUT)).toEqual(EXPECTED); | |
}); | |
it('should return words for a number', () => { | |
const INPUT = 123; | |
const EXPECTED = 'one two three'; | |
expect(numberToWords(INPUT)).toEqual(EXPECTED); | |
}); | |
it('should return words for a mixture of letters and numbers', () => { | |
const INPUT = 'nice123nice'; | |
const EXPECTED = 'one two three'; | |
expect(numberToWords(INPUT)).toEqual(EXPECTED); | |
}); | |
it('should fallback gracefully for no input', () => { | |
const INPUT = ''; | |
const EXPECTED = ''; | |
expect(numberToWords(INPUT)).toEqual(EXPECTED); | |
}); | |
it('should fallback gracefully for just a plus symbol', () => { | |
const INPUT = '+'; | |
const EXPECTED = '+'; | |
expect(numberToWords(INPUT)).toEqual(EXPECTED); | |
}); | |
it('should fallback gracefully for a non-number', () => { | |
const INPUT = 'nice'; | |
const EXPECTED = 'nice'; | |
expect(numberToWords(INPUT)).toEqual(EXPECTED); | |
}); | |
it('should fallback gracefully for a boolean: false value', () => { | |
const INPUT = false; | |
const EXPECTED = false; | |
expect(numberToWords(INPUT)).toEqual(EXPECTED); | |
}); | |
it('should fallback gracefully for a boolean: true value', () => { | |
const INPUT = true; | |
const EXPECTED = true; | |
expect(numberToWords(INPUT)).toEqual(EXPECTED); | |
}); | |
it('should fallback gracefully for a NaN', () => { | |
const INPUT = NaN; | |
const EXPECTED = NaN; | |
expect(numberToWords(INPUT)).toEqual(EXPECTED); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment