Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update readme.

  • Loading branch information...
commit d0c3ef4cbe6a95e1aee1d54e6d577008fbc2787e 1 parent 4670db5
@blakeembrey authored
Showing with 49 additions and 36 deletions.
  1. +30 −22 Readme.md
  2. +1 −1  component.json
  3. +14 −10 index.js
  4. +1 −1  package.json
  5. +3 −2 test.js
View
52 Readme.md
@@ -1,6 +1,6 @@
# 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
@@ -14,28 +14,36 @@ npm install number-words --save
var numbers = require('number-words');
```
-* `numbers(number|word)`
-* `numbers.stringify(number)`
-* `numbers.numberify(word)`
+* [numbers( number|string )](#function)
+* [numbers.parse( string )](#parse)
+* [numbers.stringify( number )](#stringify)
-```javascript
-numbers(20) // twenty
-numbers(150) // one hundred and fifty
-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
-
-numbers(0.5) // zero point five
-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('fifty six') // 56
-numbers('nine hundred and twenty two') // 922
-numbers('three hundred and fifty thousand') // 350000
-numbers('six billion and ninety million and three') // 6090000003
-
-numbers('point six six') // 0.66
-numbers('zero point two one nine') // 0.219
-numbers('five point five') // 5.5
+### Function
+
+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.
+
+### Parse
+
+Parses a string into a number as best as possible.
+
+```
+numbers.parse('ninety nine');
+=> 99
+
+numbers.parse('point two five nine');
+=> 0.259
+```
+
+### Stringify
+
+Stringifies a number to the word equivalent.
+
+```
+numbers.stringify(99);
+=> "ninety nine"
+
+numbers.stringify(0.259);
+=> "zero point two five nine"
```
## License
View
2  component.json
@@ -1,6 +1,6 @@
{
"name": "number-words",
- "version": "0.0.2",
+ "version": "0.0.3",
"main": [
"./index.js"
],
View
24 index.js
@@ -62,7 +62,9 @@ helpers[303] = 'centillion';
// Make a hash of the numbers and helper numbers reversed
// E.g. The key as the word and value as the number
var numbersMap = {};
+numbersMap.nil = 0;
numbersMap.naught = 0;
+numbersMap.period = '.';
numbersMap.decimal = '.';
Object.keys(numbers).forEach(function (num) {
@@ -93,12 +95,12 @@ var intervals = function (num) {
* @return {string|number}
*/
var numberWords = module.exports = function (num) {
+ if (typeof num === 'string') {
+ return numberWords.parse(num);
+ }
if (typeof num === 'number') {
return numberWords.stringify(num);
}
- if (typeof num === 'string') {
- return numberWords.numberify(num);
- }
throw new Error('Number words can handle handle numbers and/or strings');
};
@@ -159,22 +161,24 @@ numberWords.stringify = function (num) {
* @param {string} num
* @return {number}
*/
-numberWords.numberify = function (num) {
+numberWords.parse = function (num) {
if (typeof num !== 'string') { return false; }
- var modifier = 1,
- largest = 0,
+ var modifier = 1,
+ largest = 0,
largestInterval = 0,
- zeros = 0, // Keep track of the number of leading zeros in the decimal
- stack = [];
+ zeros = 0, // Keep track of the number of leading zeros in the decimal
+ stack = [];
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]) {
return memo * num;
}
return memo + num;
- }, 0) * largest;
+ }, 0);
+
+ return total * largest;
};
var total = num.split(/\W+/g).map(function (num) {
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "number-words",
- "version": "0.0.2",
+ "version": "0.0.3",
"description": "Turn a number into a formatted string",
"main": "index.js",
"scripts": {
View
5 test.js
@@ -139,7 +139,8 @@ describe('number words', function () {
assert.equal(numbers('eight zero three zero five'), 80305);
assert.equal(numbers('twenty thirteen'), 2013);
assert.equal(numbers('two decimal fifty six'), 2.56);
- // assert.equal(numbers('twenty one three'), 2013);
- // assert.equal(numbers('one twenty'), 120);
+ assert.equal(numbers('nineteen thirty-five'), 1935);
+ assert.equal(numbers('one two five six'), 1256);
+ assert.equal(numbers('thirty hundred'), 3000);
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.