number-to-words
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "number-to-words", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Contains some util methods for converting numbers into words, ordinal words and ordinal numbers.", | ||
"main": "./src", | ||
"browser": "./numberToWords.min.js", | ||
"devDependencies": { | ||
"jasmine": "^2.2.1" | ||
"gulp": "^3.9.0", | ||
"gulp-concat": "^2.6.0", | ||
"gulp-rename": "^1.2.2", | ||
"gulp-replace": "^0.5.4", | ||
"gulp-uglify": "^1.4.0", | ||
"gulp-wrap": "^0.11.0", | ||
"jasmine": "^2.3.2" | ||
}, | ||
"scripts": { | ||
"build": "gulp build", | ||
"test": "jasmine" | ||
@@ -16,3 +24,3 @@ }, | ||
}, | ||
"author": "Martin Eneqvist <marlun78@hotmail.com> (https://github.com/marlun78)", | ||
"author": "Martin Eneqvist (https://github.com/marlun78)", | ||
"license": "MIT", | ||
@@ -19,0 +27,0 @@ "bugs": { |
@@ -51,11 +51,21 @@ | ||
### Comments / Bugs | ||
Comments and bug reports are much appreciated. Please enter them on the [issues page](https://github.com/marlun78/number-to-words/issues). Thanks! | ||
### Contributions, Comments and Bugs | ||
Contributions, comments and/or bug reports are much appreciated. Open a pull request or add comments on the | ||
[issues page](https://github.com/marlun78/number-to-words/issues). Thanks! | ||
### Upcoming v2.0 | ||
See [roadmap](ROADMAP.md) for details. | ||
### Change Log | ||
##### Version 1.2.0 (final 1.x release) | ||
- Bundles are now available for browsers (`./numberToWords.js` and `./numberToWords.min.js`). They export a global `numberToWords` variable. | ||
- Made unit tests runnable in browser. To run the unit tests in the browser, start a local server and visit localhost/spec with a browser. | ||
- `toOrdinal`, `toWords` and `toWordsOrdinal` now also allow string values as input (replaced `Math.floor` with `parseInt`). | ||
##### 1.1.0 | ||
- New methods `toOrdinal` and `toWordsOrdinal` | ||
- Deprecated `toWords` second optional `asOrdinal` parameter | ||
- Deprecated `toWords` second optional `asOrdinal` parameter, use `toWordsOrdinal()`-method instead | ||
- `toWords` now converts any decimal input to an integer (with `Math.floor`) | ||
@@ -62,0 +72,0 @@ - Bug fixed returning _“zeroth”_ instead of _“zero”_ when converting a number word into its ordinal |
'use strict'; | ||
var index = require('../src'); | ||
var index = typeof require !== 'undefined' ? require('../src') : window.numberToWords; | ||
@@ -15,2 +15,2 @@ describe('index', function () { | ||
}); | ||
}); | ||
}); |
'use strict'; | ||
var toOrdinal = require('../src/toOrdinal'); | ||
var toOrdinal = typeof require !== 'undefined' ? require('../src/toOrdinal') : window.numberToWords.toOrdinal; | ||
@@ -29,2 +29,2 @@ describe('toOrdinal', function () { | ||
tests.forEach(addTest); | ||
}); | ||
}); |
'use strict'; | ||
var toWordsOrdinal = require('../src/toWordsOrdinal'); | ||
var toWordsOrdinal = typeof require !== 'undefined' ? require('../src/toWordsOrdinal') : window.numberToWords.toWordsOrdinal; | ||
describe('toWordsOrdinal', function () { | ||
var input = 18, | ||
output = 'eighteenth'; | ||
it('should, if passed ' + input + ', return "' + output + '"', | ||
function () { | ||
expect(toWordsOrdinal(input)).toEqual(output); | ||
var tests = [ | ||
{ input: '0', expect: 'zeroth' }, | ||
{ input: '1', expect: 'first' }, | ||
{ input: '2', expect: 'second' }, | ||
{ input: '3', expect: 'third' }, | ||
{ input: '4', expect: 'fourth' }, | ||
{ input: '5', expect: 'fifth' }, | ||
{ input: '6', expect: 'sixth' }, | ||
{ input: '7', expect: 'seventh' }, | ||
{ input: '8', expect: 'eighth' }, | ||
{ input: '9', expect: 'ninth' }, | ||
{ input: '10', expect: 'tenth' }, | ||
{ input: '11', expect: 'eleventh' }, | ||
{ input: '12', expect: 'twelfth' }, | ||
{ input: '13', expect: 'thirteenth' }, | ||
{ input: '14', expect: 'fourteenth' }, | ||
{ input: '15', expect: 'fifteenth' }, | ||
{ input: '16', expect: 'sixteenth' }, | ||
{ input: '17', expect: 'seventeenth' }, | ||
{ input: '18', expect: 'eighteenth' }, | ||
{ input: '19', expect: 'nineteenth' }, | ||
{ input: '20', expect: 'twentieth' }, | ||
{ input: '21', expect: 'twenty-first' }, | ||
{ input: '22', expect: 'twenty-second' }, | ||
{ input: '23', expect: 'twenty-third' }, | ||
{ input: '24', expect: 'twenty-fourth' }, | ||
{ input: '25', expect: 'twenty-fifth' }, | ||
{ input: '26', expect: 'twenty-sixth' }, | ||
{ input: '27', expect: 'twenty-seventh' }, | ||
{ input: '28', expect: 'twenty-eighth' }, | ||
{ input: '29', expect: 'twenty-ninth' }, | ||
{ input: '30', expect: 'thirtieth' }, | ||
{ input: '40', expect: 'fortieth' }, | ||
{ input: '50', expect: 'fiftieth' }, | ||
{ input: '60', expect: 'sixtieth' }, | ||
{ input: '70', expect: 'seventieth' }, | ||
{ input: '80', expect: 'eightieth' }, | ||
{ input: '90', expect: 'ninetieth' }, | ||
{ input: '100', expect: 'one hundredth' }, | ||
{ input: '1000', expect: 'one thousandth' }, | ||
{ input: '1000000', expect: 'one millionth' }, | ||
{ input: '1000000000', expect: 'one billionth' }, | ||
{ input: '1000000000000', expect: 'one trillionth' }, | ||
{ input: '1000000000000000', expect: 'one quadrillionth' } | ||
]; | ||
function addTest(test) { | ||
it('should, if passed "' + test.input + '", return "' + test.expect + '"', function () { | ||
expect(toWordsOrdinal(test.input)).toEqual(test.expect); | ||
}); | ||
}); | ||
} | ||
tests.forEach(addTest); | ||
}); |
'use strict'; | ||
var toWords = require('../src/toWords'); | ||
var toWords = typeof require !== 'undefined' ? require('../src/toWords') : window.numberToWords.toWords; | ||
@@ -51,20 +51,47 @@ describe('toWords', function () { | ||
{ input: 100000000, expect: 'one hundred million' }, | ||
{ input: 777777777, expect: 'seven hundred seventy-seven million, seven hundred seventy-seven thousand, seven hundred seventy-seven' }, | ||
{ | ||
input: 777777777, | ||
expect: 'seven hundred seventy-seven million, seven hundred seventy-seven thousand, seven hundred seventy-seven' | ||
}, | ||
{ input: 1000000000, expect: 'one billion' }, | ||
{ input: 8888888888, expect: 'eight billion, eight hundred eighty-eight million, eight hundred eighty-eight thousand, eight hundred eighty-eight' }, | ||
{ | ||
input: 8888888888, | ||
expect: 'eight billion, eight hundred eighty-eight million, eight hundred eighty-eight thousand, eight hundred eighty-eight' | ||
}, | ||
{ input: 10000000000, expect: 'ten billion' }, | ||
{ input: 99999999999, expect: 'ninety-nine billion, nine hundred ninety-nine million, nine hundred ninety-nine thousand, nine hundred ninety-nine' }, | ||
{ | ||
input: 99999999999, | ||
expect: 'ninety-nine billion, nine hundred ninety-nine million, nine hundred ninety-nine thousand, nine hundred ninety-nine' | ||
}, | ||
{ input: 100000000000, expect: 'one hundred billion' }, | ||
{ input: 111111111111, expect: 'one hundred eleven billion, one hundred eleven million, one hundred eleven thousand, one hundred eleven' }, | ||
{ | ||
input: 111111111111, | ||
expect: 'one hundred eleven billion, one hundred eleven million, one hundred eleven thousand, one hundred eleven' | ||
}, | ||
{ input: 1000000000000, expect: 'one trillion' }, | ||
{ input: 2222222222222, expect: 'two trillion, two hundred twenty-two billion, two hundred twenty-two million, two hundred twenty-two thousand, two hundred twenty-two' }, | ||
{ | ||
input: 2222222222222, | ||
expect: 'two trillion, two hundred twenty-two billion, two hundred twenty-two million, two hundred twenty-two thousand, two hundred twenty-two' | ||
}, | ||
{ input: 10000000000000, expect: 'ten trillion' }, | ||
{ input: 33333333333333, expect: 'thirty-three trillion, three hundred thirty-three billion, three hundred thirty-three million, three hundred thirty-three thousand, three hundred thirty-three' }, | ||
{ | ||
input: 33333333333333, | ||
expect: 'thirty-three trillion, three hundred thirty-three billion, three hundred thirty-three million, three hundred thirty-three thousand, three hundred thirty-three' | ||
}, | ||
{ input: 100000000000000, expect: 'one hundred trillion' }, | ||
{ input: 444444444444444, expect: 'four hundred forty-four trillion, four hundred forty-four billion, four hundred forty-four million, four hundred forty-four thousand, four hundred forty-four' }, | ||
{ | ||
input: 444444444444444, | ||
expect: 'four hundred forty-four trillion, four hundred forty-four billion, four hundred forty-four million, four hundred forty-four thousand, four hundred forty-four' | ||
}, | ||
{ input: 1000000000000000, expect: 'one quadrillion' }, | ||
{ input: 5555555555555555, expect: 'five quadrillion, five hundred fifty-five trillion, five hundred fifty-five billion, five hundred fifty-five million, five hundred fifty-five thousand, five hundred fifty-five' }, | ||
{ input: 9007199254740992, expect: 'nine quadrillion, seven trillion, one hundred ninety-nine billion, two hundred fifty-four million, seven hundred forty thousand, nine hundred ninety-two' } | ||
{ | ||
input: 5555555555555555, | ||
expect: 'five quadrillion, five hundred fifty-five trillion, five hundred fifty-five billion, five hundred fifty-five million, five hundred fifty-five thousand, five hundred fifty-five' | ||
}, | ||
{ | ||
input: 9007199254740992, | ||
expect: 'nine quadrillion, seven trillion, one hundred ninety-nine billion, two hundred fifty-four million, seven hundred forty thousand, nine hundred ninety-two' | ||
} | ||
]; | ||
function addTest(test) { | ||
@@ -97,2 +124,2 @@ it('should, if passed ' + formatNumber(test.input) + ', return ' + test.expect, function () { | ||
return result.join('') + ' (' + length + ')'; | ||
} | ||
} |
@@ -7,2 +7,2 @@ 'use strict'; | ||
toWordsOrdinal: require('./toWordsOrdinal') | ||
}; | ||
}; |
'use strict'; | ||
module.exports = isFinite_; | ||
var globalIsFinite = isFinite; | ||
var isFinite = Number.isFinite || function (value) { | ||
return typeof value === 'number' && globalIsFinite(value); | ||
}; | ||
function isFinite_(value) { | ||
return typeof value === 'number' && isFinite(value); | ||
} | ||
module.exports = isFinite; |
'use strict'; | ||
module.exports = makeOrdinal; | ||
var endsWithDoubleZero = /(hundred|thousand|(m|b|tr|quadr)illion)$/; | ||
var endsWithTeen = /teen$/; | ||
var endsWithY = /y$/; | ||
var endsWithZeroThroughTwelve = /(zero|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve)$/; | ||
var ENDS_WITH_DOUBLE_ZERO_PATTERN = /(hundred|thousand|(m|b|tr|quadr)illion)$/; | ||
var ENDS_WITH_TEEN_PATTERN = /teen$/; | ||
var ENDS_WITH_Y_PATTERN = /y$/; | ||
var ENDS_WITH_ZERO_THROUGH_TWELVE_PATTERN = /(zero|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve)$/; | ||
var ordinalLessThanThirteen = { | ||
@@ -33,16 +31,20 @@ zero: 'zeroth', | ||
// Ends with *00 (100, 1000, etc.) or *teen (13, 14, 15, 16, 17, 18, 19) | ||
if (endsWithDoubleZero.test(words) || endsWithTeen.test(words)) { | ||
if (ENDS_WITH_DOUBLE_ZERO_PATTERN.test(words) || ENDS_WITH_TEEN_PATTERN.test(words)) { | ||
return words + 'th'; | ||
} | ||
// Ends with *y (20, 30, 40, 50, 60, 70, 80, 90) | ||
else if (endsWithY.test(words)) { | ||
return words.replace(endsWithY, 'ieth'); | ||
else if (ENDS_WITH_Y_PATTERN.test(words)) { | ||
return words.replace(ENDS_WITH_Y_PATTERN, 'ieth'); | ||
} | ||
// Ends with one through twelve | ||
else if (endsWithZeroThroughTwelve.test(words)) { | ||
return words.replace(endsWithZeroThroughTwelve, function (match, numberWord) { | ||
return ordinalLessThanThirteen[numberWord]; | ||
}); | ||
else if (ENDS_WITH_ZERO_THROUGH_TWELVE_PATTERN.test(words)) { | ||
return words.replace(ENDS_WITH_ZERO_THROUGH_TWELVE_PATTERN, replaceWithOrdinalVariant); | ||
} | ||
return words; | ||
} | ||
} | ||
function replaceWithOrdinalVariant(match, numberWord) { | ||
return ordinalLessThanThirteen[numberWord]; | ||
} | ||
module.exports = makeOrdinal; |
@@ -5,4 +5,2 @@ 'use strict'; | ||
module.exports = toOrdinal; | ||
/** | ||
@@ -12,15 +10,16 @@ * Converts an integer into a string with an ordinal postfix. | ||
* @example toOrdinal(12) => '12th' | ||
* @param {number} number | ||
* @param {number|string} number | ||
* @returns {string} | ||
*/ | ||
function toOrdinal(number) { | ||
if (!isFinite(number)) { | ||
throw new TypeError('Not a finite number'); | ||
} | ||
var str = String(Math.floor(number)); | ||
var num = parseInt(number, 10); | ||
if (!isFinite(num)) throw new TypeError('Not a finite number: ' + number + ' (' + typeof number + ')'); | ||
var str = String(num); | ||
var lastChar = str.charAt(str.length - 1); | ||
return str + (lastChar === '1' ? 'st' | ||
: lastChar === '2' ? 'nd' | ||
: lastChar === '3' ? 'rd' | ||
: 'th'); | ||
} | ||
: lastChar === '2' ? 'nd' | ||
: lastChar === '3' ? 'rd' | ||
: 'th'); | ||
} | ||
module.exports = toOrdinal; |
@@ -6,4 +6,2 @@ 'use strict'; | ||
module.exports = toWords; | ||
var TEN = 10; | ||
@@ -19,11 +17,8 @@ var ONE_HUNDRED = 100; | ||
var LESS_THAN_TWENTY = [ | ||
'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', | ||
'nine', | ||
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', | ||
'seventeen', 'eighteen', 'nineteen' | ||
'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', | ||
'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' | ||
]; | ||
var TENTHS_LESS_THAN_HUNDRED = [ | ||
'zero', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', | ||
'eighty', 'ninety' | ||
'zero', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' | ||
]; | ||
@@ -35,4 +30,4 @@ | ||
* @example toWords(12) => 'twelve' | ||
* @param {number} number | ||
* @param {boolean} asOrdinal - Deprecated, use toWordsOrdinal() instead! | ||
* @param {number|string} number | ||
* @param {boolean} [asOrdinal] - Deprecated, use toWordsOrdinal() instead! | ||
* @returns {string} | ||
@@ -42,6 +37,5 @@ */ | ||
var words; | ||
if (!isFinite(number)) { | ||
throw new TypeError('Not a finite number'); | ||
} | ||
words = generateWords(Math.floor(number)); | ||
var num = parseInt(number, 10); | ||
if (!isFinite(num)) throw new TypeError('Not a finite number: ' + number + ' (' + typeof number + ')'); | ||
words = generateWords(num); | ||
return asOrdinal ? makeOrdinal(words) : words; | ||
@@ -109,2 +103,4 @@ } | ||
return generateWords(remainder, words); | ||
} | ||
} | ||
module.exports = toWords; |
@@ -6,8 +6,6 @@ 'use strict'; | ||
module.exports = toWordsOrdinal; | ||
/** | ||
* Converts a number into ordinal words. | ||
* @example toWordsOrdinal(12) => 'twelfth' | ||
* @param {number} number | ||
* @param {number|string} number | ||
* @returns {string} | ||
@@ -18,2 +16,4 @@ */ | ||
return makeOrdinal(words); | ||
} | ||
} | ||
module.exports = toWordsOrdinal; |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
32695
22
615
78
0
7
1