Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 258bf8f42e1eb3a2bc32fa44a267c5bfe2a434aa @blakeembrey committed Apr 11, 2013
Showing with 248 additions and 0 deletions.
  1. +1 −0 .gitattributes
  2. +49 −0 .gitignore
  3. +3 −0 Readme.md
  4. +8 −0 component.json
  5. +109 −0 index.js
  6. +20 −0 package.json
  7. +58 −0 test.js
@@ -0,0 +1 @@
+* text=auto
@@ -0,0 +1,49 @@
+# Read about how to use .gitignore: http://h5bp.com/ae
+
+# Numerous always-ignore extensions
+*.diff
+*.err
+*.orig
+*.log
+*.rej
+*.swo
+*.swp
+*.vi
+*~
+*.sass-cache
+
+# OS or Editor folders
+.DS_Store
+._*
+Thumbs.db
+.cache
+.project
+.settings
+.tmproj
+nbproject
+*.sublime-project
+*.sublime-workspace
+
+# Dreamweaver added files
+_notes
+dwsync.xml
+
+# Komodo
+*.komodoproject
+.komodotools
+
+# Espresso
+*.esproj
+*.espressostorage
+
+# Rubinius
+*.rbc
+
+# Folders to ignore
+.hg
+.svn
+.CVS
+.idea
+
+# Project folders to ignore
+node_modules
@@ -0,0 +1,3 @@
+# Number Words
+
+Turn a number into a formatted word string.
@@ -0,0 +1,8 @@
+{
+ "name": "number-words",
+ "version": "0.0.1",
+ "main": [
+ "./index.js"
+ ],
+ "dependencies": {}
+}
109 index.js
@@ -0,0 +1,109 @@
+var numbers = {
+ '-': 'negative',
+ 0: 'zero',
+ 1: 'one',
+ 2: 'two',
+ 3: 'three',
+ 4: 'four',
+ 5: 'five',
+ 6: 'six',
+ 7: 'seven',
+ 8: 'eight',
+ 9: 'nine',
+ 10: 'ten',
+ 11: 'eleven',
+ 12: 'twelve',
+ 13: 'thirteen',
+ 14: 'fourteen',
+ 15: 'fifteen',
+ 16: 'sixteen',
+ 17: 'seventeen',
+ 18: 'eighteen',
+ 19: 'nineteen',
+ 20: 'twenty',
+ 30: 'thirty',
+ 40: 'fourty',
+ 50: 'fifty',
+ 60: 'sixty',
+ 70: 'seventy',
+ 80: 'eighty',
+ 90: 'ninety'
+};
+
+// http://en.wikipedia.org/wiki/English_numerals#Cardinal_numbers
+var helpers = {};
+// Store the helpers in the power of tens
+helpers[2] = 'hundred';
+helpers[3] = 'thousand';
+helpers[6] = 'million';
+helpers[9] = 'billion';
+helpers[12] = 'trillion';
+helpers[15] = 'quadrillion';
+helpers[18] = 'quintillion';
+// helpers[21] = 'sextillion';
+// helpers[24] = 'septillion';
+// helpers[27] = 'octillion';
+// helpers[30] = 'nonillion';
+// helpers[33] = 'decillion';
+// helpers[36] = 'undecillion';
+// helpers[39] = 'duodecillion';
+// helpers[42] = 'tredecillion';
+// helpers[45] = 'quattuordecillion';
+// helpers[48] = 'quindecillion';
+// helpers[51] = 'sexdecillion';
+// helpers[54] = 'septendecillion';
+// helpers[57] = 'octodecillion';
+// helpers[60] = 'novemdecillion';
+// helpers[63] = 'vigintillion';
+// helpers[100] = 'googol';
+// helpers[303] = 'centillion';
+
+var intervals = function (num) {
+ return Math.floor(Math.log(Math.abs(num + 1)) / Math.log(10));
+};
+
+var numberWords = module.exports = function (num) {
+ if (typeof +num === 'number') {
+ return numberWords.stringify(+num);
+ }
+ // Word to number conversion will be coming next..
+ // return numberWords.numberify(num);
+};
+
+numberWords.stringify = function (num) {
+ // Numbers are super buggy in JS over 10^20
+ if (typeof num !== 'number' || num > Math.pow(10, 20)) { return false; }
+ num = '' + num;
+
+ // If the number is already available, return it now
+ if (numbers[num]) { return numbers[num]; }
+
+ var interval = intervals(+num),
+ smallerNum = +num.slice(interval * -1);
+ // It's an interval of our huge numbers, try coersion
+ if (helpers[interval]) {
+ return numberWords.stringify(+num.slice(0, interval * -1)) + ' ' +
+ helpers[interval] + (smallerNum ? (smallerNum < 100 ? ' and ' : ', ') +
+ numberWords.stringify(smallerNum) : '');
+ }
+ // It's below one hundred, but greater than nine
+ if (interval === 1) {
+ return numberWords.stringify(Math.floor(+num / 10) * 10) + '-' + numberWords.stringify(+num.slice(1));
+ }
+
+ interval = intervals(+num / 10);
+ if (helpers[interval]) {
+ smallerNum = +num.slice(2);
+ return numberWords.stringify(+num.slice(0, 2)) + ' ' + helpers[interval] +
+ (smallerNum ? (smallerNum < 100 ? ' and ' : ', ') + numberWords.stringify(smallerNum) : '');
+ }
+
+ interval = intervals(+num / 100);
+ smallerNum = +num.slice(3);
+ return numberWords.stringify(+num.slice(0, 3)) + ' ' + helpers[interval] +
+ (smallerNum ? (smallerNum < 100 ? ' and ' : ', ') + numberWords.stringify(smallerNum) : '');
+
+ // var contNum = +num.slice(-3);
+ // return numberWords.stringify(+num / 1000) + ' ' +
+ // helpers[interval - 3] + (contNum ? ', ' + numberWords.stringify(contNum) : '');
+};
@@ -0,0 +1,20 @@
+{
+ "name": "number-words",
+ "version": "0.0.1",
+ "description": "Turn a number into a formatted string",
+ "main": "index.js",
+ "scripts": {
+ "test": "./node_modules/mocha/bin/mocha test.js"
+ },
+ "repository": "",
+ "keywords": [
+ "numbers",
+ "words"
+ ],
+ "author": "Blake Embrey",
+ "license": "MIT",
+ "readmeFilename": "Readme.md",
+ "devDependencies": {
+ "mocha": "~1.8.2"
+ }
+}
58 test.js
@@ -0,0 +1,58 @@
+/*global describe,it*/
+var assert = require('assert'),
+ numbers = require('./');
+
+describe('number words', function () {
+ it('should turn numbers into words', function () {
+ assert.equal(numbers(0), 'zero');
+ assert.equal(numbers(1), 'one');
+ assert.equal(numbers(2), 'two');
+ assert.equal(numbers(3), 'three');
+ assert.equal(numbers(4), 'four');
+ assert.equal(numbers(5), 'five');
+ assert.equal(numbers(6), 'six');
+ assert.equal(numbers(7), 'seven');
+ assert.equal(numbers(8), 'eight');
+ assert.equal(numbers(9), 'nine');
+ assert.equal(numbers(10), 'ten');
+ assert.equal(numbers(11), 'eleven');
+ assert.equal(numbers(12), 'twelve');
+ assert.equal(numbers(13), 'thirteen');
+ assert.equal(numbers(14), 'fourteen');
+ assert.equal(numbers(15), 'fifteen');
+ assert.equal(numbers(16), 'sixteen');
+ assert.equal(numbers(17), 'seventeen');
+ assert.equal(numbers(18), 'eighteen');
+ assert.equal(numbers(19), 'nineteen');
+ assert.equal(numbers(20), 'twenty');
+ });
+
+ it('should handle tens', function () {
+ assert.equal(numbers(29), 'twenty-nine');
+ assert.equal(numbers(36), 'thirty-six');
+ assert.equal(numbers(45), 'fourty-five');
+ assert.equal(numbers(51), 'fifty-one');
+ assert.equal(numbers(63), 'sixty-three');
+ assert.equal(numbers(78), 'seventy-eight');
+ assert.equal(numbers(84), 'eighty-four');
+ assert.equal(numbers(92), 'ninety-two');
+ });
+
+ it('should handle increasingly larger numbers', function () {
+ assert.equal(numbers(110), 'one hundred and ten');
+ assert.equal(numbers(156), 'one hundred and fifty-six');
+ assert.equal(numbers(1000), 'one thousand');
+ assert.equal(numbers(1033), 'one thousand and thirty-three');
+ assert.equal(numbers(1693), 'one thousand, six hundred and ninety-three');
+ assert.equal(numbers(10845), 'ten thousand, eight hundred and fourty-five');
+ assert.equal(numbers(763405), 'seven hundred and sixty-three thousand, four hundred and five');
+ assert.equal(numbers(2874595), 'two million, eight hundred and seventy-four thousand, five hundred and ninety-five');
+ assert.equal(numbers(Math.pow(10, 7)), 'ten million');
+ assert.equal(numbers(Math.pow(10, 9) + 162), 'one billion, one hundred and sixty-two');
+ assert.equal(numbers(Math.pow(10, 10)), 'ten billion');
+ assert.equal(numbers(Math.pow(10, 11)), 'one hundred billion');
+ assert.equal(numbers(Math.pow(10, 11) + 6), 'one hundred billion and six');
+ assert.equal(numbers(Math.pow(10, 12) + 3), 'one trillion and three');
+ assert.equal(numbers(Math.pow(10, 13)), 'ten trillion');
+ });
+});

0 comments on commit 258bf8f

Please sign in to comment.