Skip to content

Commit

Permalink
Update readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jul 4, 2013
1 parent 4670db5 commit d0c3ef4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 36 deletions.
52 changes: 30 additions & 22 deletions Readme.md
@@ -1,6 +1,6 @@
# Number Words # Number Words


Turn any number into a formatted word string, and turn it back again! Turn any number into a formatted word string, and turn it back again.


## Installation ## Installation


Expand All @@ -14,28 +14,36 @@ npm install number-words --save
var numbers = require('number-words'); var numbers = require('number-words');
``` ```


* `numbers(number|word)` * [numbers( number|string )](#function)
* `numbers.stringify(number)` * [numbers.parse( string )](#parse)
* `numbers.numberify(word)` * [numbers.stringify( number )](#stringify)


```javascript ### Function
numbers(20) // twenty
numbers(150) // one hundred and fifty Number Words exposes a single function that accepts either a string or a number. The string will delegate to the `parse` method and a number will delegate to the `stringify` method.
numbers(1620) // one thousand, six hundred and twenty
numbers(3726473223) // three billion, seven hundred and twenty-six million, four hundred and seventy-three thousand, two hundred and twenty-three ### Parse


numbers(0.5) // zero point five Parses a string into a number as best as possible.
numbers(0.7345) // zero point seven three four five
numbers(8364.3243) // eight thousand, three hundred and sixty-four point three two four three ```

numbers.parse('ninety nine');
numbers('fifty six') // 56 => 99
numbers('nine hundred and twenty two') // 922
numbers('three hundred and fifty thousand') // 350000 numbers.parse('point two five nine');
numbers('six billion and ninety million and three') // 6090000003 => 0.259

```
numbers('point six six') // 0.66
numbers('zero point two one nine') // 0.219 ### Stringify
numbers('five point five') // 5.5
Stringifies a number to the word equivalent.

```
numbers.stringify(99);
=> "ninety nine"
numbers.stringify(0.259);
=> "zero point two five nine"
``` ```


## License ## License
Expand Down
2 changes: 1 addition & 1 deletion component.json
@@ -1,6 +1,6 @@
{ {
"name": "number-words", "name": "number-words",
"version": "0.0.2", "version": "0.0.3",
"main": [ "main": [
"./index.js" "./index.js"
], ],
Expand Down
24 changes: 14 additions & 10 deletions index.js
Expand Up @@ -62,7 +62,9 @@ helpers[303] = 'centillion';
// Make a hash of the numbers and helper numbers reversed // Make a hash of the numbers and helper numbers reversed
// E.g. The key as the word and value as the number // E.g. The key as the word and value as the number
var numbersMap = {}; var numbersMap = {};
numbersMap.nil = 0;
numbersMap.naught = 0; numbersMap.naught = 0;
numbersMap.period = '.';
numbersMap.decimal = '.'; numbersMap.decimal = '.';


Object.keys(numbers).forEach(function (num) { Object.keys(numbers).forEach(function (num) {
Expand Down Expand Up @@ -93,12 +95,12 @@ var intervals = function (num) {
* @return {string|number} * @return {string|number}
*/ */
var numberWords = module.exports = function (num) { var numberWords = module.exports = function (num) {
if (typeof num === 'string') {
return numberWords.parse(num);
}
if (typeof num === 'number') { if (typeof num === 'number') {
return numberWords.stringify(num); return numberWords.stringify(num);
} }
if (typeof num === 'string') {
return numberWords.numberify(num);
}
throw new Error('Number words can handle handle numbers and/or strings'); throw new Error('Number words can handle handle numbers and/or strings');
}; };


Expand Down Expand Up @@ -159,22 +161,24 @@ numberWords.stringify = function (num) {
* @param {string} num * @param {string} num
* @return {number} * @return {number}
*/ */
numberWords.numberify = function (num) { numberWords.parse = function (num) {
if (typeof num !== 'string') { return false; } if (typeof num !== 'string') { return false; }


var modifier = 1, var modifier = 1,
largest = 0, largest = 0,
largestInterval = 0, largestInterval = 0,
zeros = 0, // Keep track of the number of leading zeros in the decimal zeros = 0, // Keep track of the number of leading zeros in the decimal
stack = []; stack = [];


var totalStack = function () { var totalStack = function () {
return stack.reduceRight(function (memo, num, index, array) { var total = stack.reduceRight(function (memo, num, index, array) {
if (num > array[index + 1]) { if (num > array[index + 1]) {
return memo * num; return memo * num;
} }
return memo + num; return memo + num;
}, 0) * largest; }, 0);

return total * largest;
}; };


var total = num.split(/\W+/g).map(function (num) { var total = num.split(/\W+/g).map(function (num) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{ {
"name": "number-words", "name": "number-words",
"version": "0.0.2", "version": "0.0.3",
"description": "Turn a number into a formatted string", "description": "Turn a number into a formatted string",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
Expand Down
5 changes: 3 additions & 2 deletions test.js
Expand Up @@ -139,7 +139,8 @@ describe('number words', function () {
assert.equal(numbers('eight zero three zero five'), 80305); assert.equal(numbers('eight zero three zero five'), 80305);
assert.equal(numbers('twenty thirteen'), 2013); assert.equal(numbers('twenty thirteen'), 2013);
assert.equal(numbers('two decimal fifty six'), 2.56); assert.equal(numbers('two decimal fifty six'), 2.56);
// assert.equal(numbers('twenty one three'), 2013); assert.equal(numbers('nineteen thirty-five'), 1935);
// assert.equal(numbers('one twenty'), 120); assert.equal(numbers('one two five six'), 1256);
assert.equal(numbers('thirty hundred'), 3000);
}); });
}); });

0 comments on commit d0c3ef4

Please sign in to comment.