├── .eslintignore ├── .npmrc ├── .vexor.yml ├── .travis.yml ├── circle.yml ├── wallaby.js ├── .probo.yaml ├── .codeclimate.yml ├── .gitlab-ci.yml ├── example.js ├── shippable.yml ├── .gitignore ├── LICENSE ├── package.json ├── wercker.yml ├── lib └── index.js ├── README.md ├── .eslintrc └── test └── english2NumberSpec.js /.eslintignore: -------------------------------------------------------------------------------- 1 | **/*{.,-}min.js 2 | test/** 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | //registry.npmjs.org/:_authToken=${NPM_TOKEN} 2 | -------------------------------------------------------------------------------- /.vexor.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "6" 4 | - "7" 5 | script: 6 | - npm install 7 | - npm test 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | - "0.11" 5 | - "0.12" 6 | - "4" 7 | - "5" 8 | - "6" 9 | - "7" 10 | script: 11 | - npm install 12 | - npm test 13 | -------------------------------------------------------------------------------- /circle.yml: -------------------------------------------------------------------------------- 1 | machine: 2 | node: 3 | version: 6.1.0 4 | environment: 5 | MOCHA_FILE: $CIRCLE_TEST_REPORTS/test-results.xml 6 | 7 | test: 8 | override: 9 | - mocha test --reporter mocha-circleci-reporter -------------------------------------------------------------------------------- /wallaby.js: -------------------------------------------------------------------------------- 1 | module.exports = function () { 2 | return { 3 | files: [ 4 | "*.js" 5 | ], 6 | 7 | tests: [ 8 | "test/**/*Spec.js" 9 | ], 10 | env: { 11 | type: "node" 12 | } 13 | }; 14 | }; -------------------------------------------------------------------------------- /.probo.yaml: -------------------------------------------------------------------------------- 1 | steps: 2 | - plugin: Script 3 | name: Install NVM, run tests 4 | script: | 5 | apt-get update 6 | curl -sL https://deb.nodesource.com/setup_7.x -o setup_7.x 7 | . setup_7.x 8 | apt-get install nodejs 9 | npm install 10 | npm test 11 | -------------------------------------------------------------------------------- /.codeclimate.yml: -------------------------------------------------------------------------------- 1 | --- 2 | engines: 3 | duplication: 4 | enabled: false 5 | config: 6 | languages: 7 | - javascript 8 | eslint: 9 | enabled: true 10 | checks: 11 | no-extend-native: 12 | enabled: false 13 | fixme: 14 | enabled: true 15 | ratings: 16 | paths: 17 | - "**.js" -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | image: node:7.1 2 | 3 | all_tests: 4 | script: 5 | - npm install 6 | - npm test 7 | - npm run-script codacy 8 | - npm run-script codeclimate 9 | 10 | deploy: 11 | script: 12 | - npm install 13 | - npm run semantic-release 14 | - git remote set-url --push origin https://$GITLAB_SERVER_FQDN.git 15 | - git push --tags 16 | only: 17 | - master 18 | 19 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var toNumber = require("english2number"); 2 | 3 | console.log(toNumber("one hundred and twenty-three septillion, four hundred and fifty-six sextillion, seven hundred and eighty-nine quintillion, one hundred and twenty-three quadrillion, four hundred and fifty-six trillion, seven hundred and eighty-nine billion, one hundred and twenty-three million, four hundred and fifty-six thousand and seven hundred and eighty-nine")); 4 | -------------------------------------------------------------------------------- /shippable.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "6.9.2" 4 | - 6 5 | - "7.2.1" 6 | - 7 7 | 8 | env: 9 | - XUNIT_FILE=shippable/testresults/result.xml 10 | build: 11 | ci: 12 | - mkdir -p shippable/testresults 13 | - mkdir -p shippable/codecoverage 14 | 15 | - npm install 16 | 17 | #Run tests 18 | - npm test 19 | 20 | #Generate coverage report with istanbul 21 | - ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- test --ui=bdd --reporter=xunit-file 22 | - ./node_modules/.bin/istanbul report cobertura --dir shippable/codecoverage/ -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | #JetBrains IDEs 40 | .idea 41 | .idea/modules.xml 42 | .idea/proportionate-js.iml 43 | .idea/workspace.xml 44 | 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Travis Savo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "name": "english2number", 4 | "version": "0.0.0-semantic-versioned", 5 | "description": "Converts English words to numbers.", 6 | "main": "lib/index.js", 7 | "scripts": { 8 | "test": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- test -R spec", 9 | "codacy": "cat ./coverage/lcov.info | ./node_modules/.bin/codacy-coverage -p .", 10 | "codeclimate": "codeclimate-test-reporter < coverage/lcov.info", 11 | "semantic-release": "semantic-release-gitlab" 12 | }, 13 | "directories": { 14 | "lib": "lib", 15 | "test": "test" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/TSavo/english2number-js.git" 20 | }, 21 | "keywords": [ 22 | "Javascript", 23 | "numbers", 24 | "english", 25 | "english2numbers", 26 | "natural language", 27 | "numeral", 28 | "convert", 29 | "convert English to numbers" 30 | ], 31 | "author": "Travis Savo ", 32 | "license": "MIT", 33 | "bugs": { 34 | "url": "https://github.com/TSavo/english2number-js/issues" 35 | }, 36 | "homepage": "https://github.com/TSavo/english2number-js#readme", 37 | "tonicExampleFilename": "example.js", 38 | "devDependencies": { 39 | "chai": "^3.5.0", 40 | "codacy-coverage": "^2.0.0", 41 | "codeclimate-test-reporter": "^0.4.0", 42 | "cz-conventional-changelog": "^1.2.0", 43 | "expect": "^1.20.2", 44 | "istanbul": "^0.4.5", 45 | "mocha": "^3.2.0", 46 | "mocha-circleci-reporter": "0.0.2", 47 | "mocha-junit-reporter": "^1.12.1", 48 | "semantic-release-gitlab": "^2.4.20", 49 | "should": "^11.1.2", 50 | "xunit-file": "^1.0.0" 51 | }, 52 | "config": { 53 | "commitizen": { 54 | "path": "./node_modules/cz-conventional-changelog" 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /wercker.yml: -------------------------------------------------------------------------------- 1 | # This references the default nodejs container from 2 | # the Docker Hub: https://registry.hub.docker.com/_/node/ 3 | # If you want Nodesource's container you would reference nodesource/node 4 | # Read more about containers on our dev center 5 | # http://devcenter.wercker.com/docs/containers/index.html 6 | box: node 7 | # This is the build pipeline. Pipelines are the core of wercker 8 | # Read more about pipelines on our dev center 9 | # http://devcenter.wercker.com/docs/pipelines/index.html 10 | 11 | env: 12 | # Set environment variable for test results output 13 | - XUNIT_FILE=shippable/testresults/result.xml 14 | 15 | # You can also use services such as databases. Read more on our dev center: 16 | # http://devcenter.wercker.com/docs/services/index.html 17 | # services: 18 | # - postgres 19 | # http://devcenter.wercker.com/docs/services/postgresql.html 20 | 21 | # - mongo 22 | # http://devcenter.wercker.com/docs/services/mongodb.html 23 | build: 24 | # The steps that will be executed on build 25 | # Steps make up the actions in your pipeline 26 | # Read more about steps on our dev center: 27 | # http://devcenter.wercker.com/docs/steps/index.html 28 | steps: 29 | # A step that executes `npm install` command 30 | - npm-install 31 | # A step that executes `npm test` command 32 | - npm-test 33 | 34 | # A custom script step, name value is used in the UI 35 | # and the code value contains the command that get executed 36 | - script: 37 | name: echo nodejs information 38 | code: | 39 | echo "node version $(node -v) running" 40 | echo "npm version $(npm -v) running" 41 | ci: 42 | - mkdir -p shippable 43 | - mkdir -p shippable/codecoverage 44 | 45 | #Run tests 46 | - npm test 47 | 48 | #Generate coverage report with istanbul 49 | - ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- test --ui=bdd --reporter=xunit-file --compilers coffee:coffee-script/register --require coffee-coverage/register-istanbul 50 | - ./node_modules/.bin/istanbul report cobertura --dir shippable/codecoverage/ -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | var english2Number, large, small; 3 | 4 | small = { 5 | 'zero': 0, 6 | 'one': 1, 7 | 'two': 2, 8 | 'three': 3, 9 | 'four': 4, 10 | 'five': 5, 11 | 'six': 6, 12 | 'seven': 7, 13 | 'eight': 8, 14 | 'nine': 9, 15 | 'ten': 10, 16 | 'eleven': 11, 17 | 'twelve': 12, 18 | 'thirteen': 13, 19 | 'fourteen': 14, 20 | 'fifteen': 15, 21 | 'sixteen': 16, 22 | 'seventeen': 17, 23 | 'eighteen': 18, 24 | 'nineteen': 19, 25 | 'twenty': 20, 26 | 'thirty': 30, 27 | 'forty': 40, 28 | 'fifty': 50, 29 | 'sixty': 60, 30 | 'seventy': 70, 31 | 'eighty': 80, 32 | 'ninety': 90 33 | }; 34 | 35 | large = { 36 | 'thousand': 1000, 37 | 'million': 1000000, 38 | 'billion': 1000000000, 39 | 'trillion': 1000000000000, 40 | 'quadrillion': 1000000000000000, 41 | 'quintillion': 1000000000000000000, 42 | 'sextillion': 1000000000000000000000, 43 | 'septillion': 1000000000000000000000000, 44 | 'octillion': 1000000000000000000000000000, 45 | 'nonillion': 1000000000000000000000000000000, 46 | 'decillion': 1000000000000000000000000000000000 47 | }; 48 | 49 | english2Number = function(english) { 50 | var current, exponent, i, int, len, product, total, word, words, negative; 51 | if (!isNaN(int = parseInt(english, 10))) { 52 | return int; 53 | } 54 | negative = english.indexOf("negative") === 0 || english.indexOf("-") === 0; 55 | words = english.replace(/\sand\s/g, " ").replace(/^negative\s/,"").replace(/^-\s/,"").replace(/^a\s/,"one ").replace(/,\s/g, " ").replace(/first/g, "one").replace(/second/g, "two").replace(/third/g, "three").replace(/fourth/g, "four").replace(/fifth/g, "five").replace(/eighth/g, "eight").replace(/ninth/g, "nine").replace(/twelfth/g, "twelve").replace(/twentieth/g, "twenty").replace(/fiftieth/g, "fifty").replace(/seventieth/g, "seventy").replace(/ninetieth/g, "ninety").replace(/(i|ie)?th(\b|-|$)/g, "").split(/[\s-]+/); 56 | total = 0; 57 | current = 0; 58 | for (i = 0, len = words.length; i < len; i++) { 59 | word = words[i]; 60 | product = small[word]; 61 | if (product !== undefined) { 62 | current += product; 63 | } else if (word === "hundred" && current !== 0) { 64 | current *= 100; 65 | } else { 66 | exponent = large[word]; 67 | if (exponent) { 68 | total += current * exponent; 69 | current = 0; 70 | } else { 71 | throw new Error("Unknown number: " + word); 72 | } 73 | } 74 | } 75 | var output = total + current; 76 | if(negative){ 77 | return output * -1; 78 | } 79 | return output; 80 | }; 81 | module.exports = english2Number; 82 | }).call(this); 83 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The English2Number Javascript Library 2 | 3 | 4 | [![build status](https://gitlab.com/TSavo/english2number-js/badges/master/build.svg)](https://gitlab.com/TSavo/english2number-js/commits/master) [![coverage report](https://gitlab.com/TSavo/english2number-js/badges/master/coverage.svg)](https://gitlab.com/TSavo/english2number-js/commits/master) [![dependencies Status](https://david-dm.org/tsavo/english2number-js/status.svg)](https://david-dm.org/tsavo/english2number-js) [![npm version](https://badge.fury.io/js/english2number.svg)](https://badge.fury.io/js/english2number) 5 | 6 | [ ![Codeship Status for TSavo/english2number-js](https://app.codeship.com/projects/10c6d730-a2d8-0134-83f1-76e92e43cf6d/status?branch=master)](https://app.codeship.com/projects/190100) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/89e3660a6b4042938b84e6e2f565fcc8)](https://www.codacy.com/app/evilgenius/english2number-js?utm_source=github.com&utm_medium=referral&utm_content=TSavo/english2number-js&utm_campaign=Badge_Grade) [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/89e3660a6b4042938b84e6e2f565fcc8)](https://www.codacy.com/app/evilgenius/english2number-js?utm_source=github.com&utm_medium=referral&utm_content=TSavo/english2number-js&utm_campaign=Badge_Coverage) [![Code Climate](https://codeclimate.com/github/TSavo/english2number-js/badges/gpa.svg)](https://codeclimate.com/github/TSavo/english2number-js) [![Test Coverage](https://codeclimate.com/github/TSavo/english2number-js/badges/coverage.svg)](https://codeclimate.com/github/TSavo/english2number-js/coverage) [![Issue Count](https://codeclimate.com/github/TSavo/english2number-js/badges/issue_count.svg)](https://codeclimate.com/github/TSavo/english2number-js) [![CircleCI](https://circleci.com/gh/TSavo/english2number-js.svg?style=svg)](https://circleci.com/gh/TSavo/english2number-js) 7 | 8 | 9 | ## What it does 10 | 11 | Converts English words to numbers. 12 | 13 | The following inputs will return this number: 123,456,789,001 14 | 15 | - "one hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand and one" (cardinal) 16 | - "a hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand and one" (cardinal) 17 | - "one hundred twenty-three billion, four hundred fifty-six million, seven hundred eighty-nine thousand, one" (cardinal) 18 | - "one hundred twenty-three billion four hundred fifty-six million seven hundred eighty-nine thousand one" (cardinal) 19 | - "a hundred twenty-three billion, four hundred fifty-six million, seven hundred eighty-nine thousand, and one" (cardinal) 20 | - "a hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand and first" (ordinal) 21 | - "one hundred twenty-three billion, four hundred fifty-six million, seven hundred eighty-nine thousand, first" (ordinal) 22 | 23 | And in fact really any reasonable combination of cardinal and ordinal English speech patterns for describing numbers. 24 | 25 | ## What's the limit? 26 | 27 | It doesn't parse anything bigger than decillion, which is 33 zeros after the 1, or 1000000000000000000000000000000000. 28 | 29 | If you think it should, it would be trivial to add larger number support, but Javascript can't realistically represent that precision anyway. Perhaps a BigNumber version of this library would be in order. 30 | 31 | # Installation 32 | 33 | npm install --save english2number 34 | 35 | # Example Usage 36 | 37 | #### As a module: 38 | ```javascript 39 | var toNumber = require("english2number"); 40 | 41 | toNumber("one hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand and one") // 123,456,789,001 42 | ``` 43 | 44 | # Why not [numeral.js](http://numeraljs.com/)? 45 | 46 | [numeral.js](http://numeraljs.com/) is an amazing library for dealing with numerals, but it lacks support for English parsing. 47 | 48 | # Contributing 49 | Pull requests are welcome, please file any bugs on https://github.com/tsavo/english2number-js -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | ecmaFeatures: 2 | modules: true 3 | jsx: true 4 | 5 | env: 6 | amd: true 7 | browser: true 8 | es6: true 9 | jquery: true 10 | node: true 11 | 12 | # http://eslint.org/docs/rules/ 13 | rules: 14 | # Possible Errors 15 | comma-dangle: [2, never] 16 | no-cond-assign: 2 17 | no-console: 0 18 | no-constant-condition: 2 19 | no-control-regex: 2 20 | no-debugger: 2 21 | no-dupe-args: 2 22 | no-dupe-keys: 2 23 | no-duplicate-case: 2 24 | no-empty: 2 25 | no-empty-character-class: 2 26 | no-ex-assign: 2 27 | no-extra-boolean-cast: 2 28 | no-extra-parens: 0 29 | no-extra-semi: 2 30 | no-func-assign: 2 31 | no-inner-declarations: [2, functions] 32 | no-invalid-regexp: 2 33 | no-irregular-whitespace: 2 34 | no-negated-in-lhs: 2 35 | no-obj-calls: 2 36 | no-regex-spaces: 2 37 | no-sparse-arrays: 2 38 | no-unexpected-multiline: 2 39 | no-unreachable: 2 40 | use-isnan: 2 41 | valid-jsdoc: 0 42 | valid-typeof: 2 43 | 44 | # Best Practices 45 | accessor-pairs: 2 46 | block-scoped-var: 0 47 | complexity: [2, 6] 48 | consistent-return: 0 49 | curly: 0 50 | default-case: 0 51 | dot-location: 0 52 | dot-notation: 0 53 | eqeqeq: 2 54 | guard-for-in: 2 55 | no-alert: 2 56 | no-caller: 2 57 | no-case-declarations: 2 58 | no-div-regex: 2 59 | no-else-return: 0 60 | no-empty-label: 2 61 | no-empty-pattern: 2 62 | no-eq-null: 2 63 | no-eval: 2 64 | no-extend-native: 2 65 | no-extra-bind: 2 66 | no-fallthrough: 2 67 | no-floating-decimal: 0 68 | no-implicit-coercion: 0 69 | no-implied-eval: 2 70 | no-invalid-this: 0 71 | no-iterator: 2 72 | no-labels: 0 73 | no-lone-blocks: 2 74 | no-loop-func: 2 75 | no-magic-number: 0 76 | no-multi-spaces: 0 77 | no-multi-str: 0 78 | no-native-reassign: 2 79 | no-new-func: 2 80 | no-new-wrappers: 2 81 | no-new: 2 82 | no-octal-escape: 2 83 | no-octal: 2 84 | no-proto: 2 85 | no-redeclare: 2 86 | no-return-assign: 2 87 | no-script-url: 2 88 | no-self-compare: 2 89 | no-sequences: 0 90 | no-throw-literal: 0 91 | no-unused-expressions: 2 92 | no-useless-call: 2 93 | no-useless-concat: 2 94 | no-void: 2 95 | no-warning-comments: 0 96 | no-with: 2 97 | radix: 2 98 | vars-on-top: 0 99 | wrap-iife: 2 100 | yoda: 0 101 | 102 | # Strict 103 | strict: 0 104 | 105 | # Variables 106 | init-declarations: 0 107 | no-catch-shadow: 2 108 | no-delete-var: 2 109 | no-label-var: 2 110 | no-shadow-restricted-names: 2 111 | no-shadow: 0 112 | no-undef-init: 2 113 | no-undef: 0 114 | no-undefined: 0 115 | no-unused-vars: 0 116 | no-use-before-define: 0 117 | 118 | # Node.js and CommonJS 119 | callback-return: 2 120 | global-require: 2 121 | handle-callback-err: 2 122 | no-mixed-requires: 0 123 | no-new-require: 0 124 | no-path-concat: 2 125 | no-process-exit: 2 126 | no-restricted-modules: 0 127 | no-sync: 0 128 | 129 | # Stylistic Issues 130 | array-bracket-spacing: 0 131 | block-spacing: 0 132 | brace-style: 0 133 | camelcase: 0 134 | comma-spacing: 0 135 | comma-style: 0 136 | computed-property-spacing: 0 137 | consistent-this: 0 138 | eol-last: 0 139 | func-names: 0 140 | func-style: 0 141 | id-length: 0 142 | id-match: 0 143 | indent: 0 144 | jsx-quotes: 0 145 | key-spacing: 0 146 | linebreak-style: 0 147 | lines-around-comment: 0 148 | max-depth: 0 149 | max-len: 0 150 | max-nested-callbacks: 0 151 | max-params: 0 152 | max-statements: [2, 30] 153 | new-cap: 0 154 | new-parens: 0 155 | newline-after-var: 0 156 | no-array-constructor: 0 157 | no-bitwise: 0 158 | no-continue: 0 159 | no-inline-comments: 0 160 | no-lonely-if: 0 161 | no-mixed-spaces-and-tabs: 0 162 | no-multiple-empty-lines: 0 163 | no-negated-condition: 0 164 | no-nested-ternary: 0 165 | no-new-object: 0 166 | no-plusplus: 0 167 | no-restricted-syntax: 0 168 | no-spaced-func: 0 169 | no-ternary: 0 170 | no-trailing-spaces: 0 171 | no-underscore-dangle: 0 172 | no-unneeded-ternary: 0 173 | object-curly-spacing: 0 174 | one-var: 0 175 | operator-assignment: 0 176 | operator-linebreak: 0 177 | padded-blocks: 0 178 | quote-props: 0 179 | quotes: 0 180 | require-jsdoc: 0 181 | semi-spacing: 0 182 | semi: 0 183 | sort-vars: 0 184 | space-after-keywords: 0 185 | space-before-blocks: 0 186 | space-before-function-paren: 0 187 | space-before-keywords: 0 188 | space-in-parens: 0 189 | space-infix-ops: 0 190 | space-return-throw-case: 0 191 | space-unary-ops: 0 192 | spaced-comment: 0 193 | wrap-regex: 0 194 | 195 | # ECMAScript 6 196 | arrow-body-style: 0 197 | arrow-parens: 0 198 | arrow-spacing: 0 199 | constructor-super: 0 200 | generator-star-spacing: 0 201 | no-arrow-condition: 0 202 | no-class-assign: 0 203 | no-const-assign: 0 204 | no-dupe-class-members: 0 205 | no-this-before-super: 0 206 | no-var: 0 207 | object-shorthand: 0 208 | prefer-arrow-callback: 0 209 | prefer-const: 0 210 | prefer-reflect: 0 211 | prefer-spread: 0 212 | prefer-template: 0 213 | require-yield: 0 214 | -------------------------------------------------------------------------------- /test/english2NumberSpec.js: -------------------------------------------------------------------------------- 1 | var chai; 2 | chai = require("chai"); 3 | var should = chai.should(); 4 | 5 | var toNumber = require("../lib"); 6 | 7 | describe("english2number", function() { 8 | it("should throw when the value is not a valid number", function(){ 9 | +function(){ 10 | toNumber("This isn't a valid number 100."); 11 | }.should.throw(); 12 | }); 13 | it("should give right answers for numbers", function(){ 14 | toNumber("0").should.equal(0); 15 | toNumber("1").should.equal(1); 16 | toNumber("2").should.equal(2); 17 | toNumber("3").should.equal(3); 18 | toNumber("4").should.equal(4); 19 | toNumber("5").should.equal(5); 20 | toNumber("6").should.equal(6); 21 | toNumber("7").should.equal(7); 22 | toNumber("8").should.equal(8); 23 | toNumber("9").should.equal(9); 24 | toNumber("10").should.equal(10); 25 | toNumber("20").should.equal(20); 26 | toNumber("30").should.equal(30); 27 | toNumber("50").should.equal(50); 28 | toNumber("90").should.equal(90); 29 | toNumber("100").should.equal(100); 30 | }); 31 | it("should give right answers for numbered ordinals", function(){ 32 | toNumber("0th").should.equal(0); 33 | toNumber("1st").should.equal(1); 34 | toNumber("2nd").should.equal(2); 35 | toNumber("3rd").should.equal(3); 36 | toNumber("4th").should.equal(4); 37 | toNumber("5th").should.equal(5); 38 | toNumber("6th").should.equal(6); 39 | toNumber("7th").should.equal(7); 40 | toNumber("8th").should.equal(8); 41 | toNumber("9th").should.equal(9); 42 | toNumber("10th").should.equal(10); 43 | toNumber("20th").should.equal(20); 44 | toNumber("30th").should.equal(30); 45 | toNumber("50th").should.equal(50); 46 | toNumber("77th").should.equal(77); 47 | toNumber("90th").should.equal(90); 48 | toNumber("100th").should.equal(100); 49 | }); 50 | it("should give right answers for english names of numbers", function(){ 51 | toNumber("zero").should.equal(0); 52 | toNumber("one").should.equal(1); 53 | toNumber("two").should.equal(2); 54 | toNumber("three").should.equal(3); 55 | toNumber("four").should.equal(4); 56 | toNumber("five").should.equal(5); 57 | toNumber("six").should.equal(6); 58 | toNumber("seven").should.equal(7); 59 | toNumber("eight").should.equal(8); 60 | toNumber("nine").should.equal(9); 61 | toNumber("ten").should.equal(10); 62 | toNumber("eleven").should.equal(11); 63 | toNumber("twelve").should.equal(12); 64 | toNumber("thirteen").should.equal(13); 65 | toNumber("fourteen").should.equal(14); 66 | toNumber("fifteen").should.equal(15); 67 | toNumber("sixteen").should.equal(16); 68 | toNumber("seventeen").should.equal(17); 69 | toNumber("eighteen").should.equal(18); 70 | toNumber("nineteen").should.equal(19); 71 | toNumber("twenty").should.equal(20); 72 | toNumber("twenty-five").should.equal(25); 73 | toNumber("twenty five").should.equal(25); 74 | toNumber("forty-four").should.equal(44); 75 | toNumber("forty four").should.equal(44); 76 | toNumber("seventy").should.equal(70); 77 | toNumber("seventy-seven").should.equal(77); 78 | toNumber("eighty eight").should.equal(88); 79 | toNumber("ninety nine").should.equal(99); 80 | toNumber("one-hundred").should.equal(100); 81 | toNumber("one hundred").should.equal(100); 82 | }); 83 | it("should give right answers for english names of ordinal positions", function(){ 84 | toNumber("zeroth").should.equal(0); 85 | toNumber("first").should.equal(1); 86 | toNumber("second").should.equal(2); 87 | toNumber("third").should.equal(3); 88 | toNumber("fourth").should.equal(4); 89 | toNumber("fifth").should.equal(5); 90 | toNumber("sixth").should.equal(6); 91 | toNumber("seventh").should.equal(7); 92 | toNumber("eighth").should.equal(8); 93 | toNumber("ninth").should.equal(9); 94 | toNumber("tenth").should.equal(10); 95 | toNumber("eleventh").should.equal(11); 96 | toNumber("twelfth").should.equal(12); 97 | toNumber("thirteenth").should.equal(13); 98 | toNumber("fourteenth").should.equal(14); 99 | toNumber("fifteenth").should.equal(15); 100 | toNumber("sixteenth").should.equal(16); 101 | toNumber("seventeenth").should.equal(17); 102 | toNumber("eighteenth").should.equal(18); 103 | toNumber("nineteenth").should.equal(19); 104 | toNumber("twentieth").should.equal(20); 105 | toNumber("twenty first").should.equal(21); 106 | toNumber("twenty second").should.equal(22); 107 | toNumber("twenty third").should.equal(23); 108 | toNumber("twenty fourth").should.equal(24); 109 | toNumber("twenty-fifth").should.equal(25); 110 | toNumber("forty-fourth").should.equal(44); 111 | toNumber("seventieth").should.equal(70); 112 | toNumber("seventy-seventh").should.equal(77); 113 | toNumber("ninetieth").should.equal(90); 114 | toNumber("ninety ninth").should.equal(99); 115 | toNumber("one-hundredth").should.equal(100); 116 | }); 117 | 118 | it("should give the right value for a very large complex number", function(){ 119 | toNumber("one hundred and twenty-three septillion, four hundred and fifty-six sextillion, seven hundred and eighty-nine quintillion, one hundred and twenty-three quadrillion, four hundred and fifty-six trillion, seven hundred and eighty-nine billion, one hundred and twenty-three million, four hundred and fifty-six thousand and seven hundred and eighty-nine").should.equal(123456789123456789123456789); 120 | }); 121 | 122 | it("should give the right value for cardinal numbers", function(){ 123 | toNumber("sixty-one trillion, six hundred and eighty-nine billion, four hundred and seventy-three million, four hundred and fifty-three thousand and five hundred and ninety").should.equal(61689473453590); 124 | }); 125 | 126 | it("should give the right value for cardinal numbers in american form (with ands)", function(){ 127 | toNumber("sixty-one trillion, six hundred eighty-nine billion, four hundred seventy-three million, four hundred fifty-three thousand, five hundred ninety").should.equal(61689473453590); 128 | }); 129 | 130 | it("should give the right value for ordinal numbers", function(){ 131 | toNumber("sixty-one trillion, six hundred and eighty-nine billion, four hundred and seventy-three million, four hundred and fifty-three thousand and five hundred and ninetieth").should.equal(61689473453590); 132 | }); 133 | 134 | it("should give the right value for cardinal numbers in american form (with ands)", function(){ 135 | toNumber("sixty-one trillion, six hundred eighty-nine billion, four hundred seventy-three million, four hundred fifty-three thousand, five hundred ninetieth").should.equal(61689473453590); 136 | }); 137 | 138 | it("should deal with negative numbers", function(){ 139 | toNumber("negative thirty eight thousand two hundred sixty three").should.equal(-38263); 140 | toNumber("- thirty eight thousand two hundred sixty three").should.equal(-38263); 141 | toNumber("negative zero").should.equal(-0); 142 | }); 143 | 144 | }); 145 | 146 | --------------------------------------------------------------------------------