Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added vanilla js version

  • Loading branch information...
commit 69aff198299ae344fe7979d2895b374a6462c8cb 1 parent 5393f1d
@peterolson authored
Showing with 243 additions and 0 deletions.
  1. +242 −0 wordMath.vanilla.js
  2. +1 −0  wordMath.vanilla.min.js
View
242 wordMath.vanilla.js
@@ -0,0 +1,242 @@
+var wordMath = (function ($) {
+ var toFixed = function (x) {
+ var negative = false;
+ if (x < 0) { negative = true; x = -x; }
+ if (x < 1.0) {
+ var e = parseInt(x.toString().split('e-')[1]);
+ if (e) {
+ x *= Math.pow(10, e - 1);
+ x = '0.' + (new Array(e)).join('0') + x.toString().substring(2);
+ }
+ } else {
+ var e = parseInt(x.toString().split('+')[1]);
+ if (e > 20) {
+ e -= 20;
+ x /= Math.pow(10, e);
+ x += (new Array(e + 1)).join('0');
+ }
+ }
+ return negative ? "-" + x : x;
+ };
+
+ var digits = {
+ zero: 0,
+ one: 1,
+ two: 2,
+ three: 3,
+ four: 4,
+ five: 5,
+ six: 6,
+ seven: 7,
+ eight: 8,
+ nine: 9,
+ ten: 10,
+ eleven: 11,
+ twelve: 12,
+ thirteen: 13,
+ fourteen: 14,
+ fifteen: 15,
+ sixteen: 16,
+ seventeen: 17,
+ eighteen: 18,
+ nineteen: 19,
+ twenty: 20,
+ thirty: 30,
+ forty: 40,
+ fourty: 40,
+ fifty: 50,
+ sixty: 60,
+ seventy: 70,
+ eighty: 80,
+ ninety: 90,
+ pi: Math.PI,
+ e: Math.E
+ };
+ var place = function (n) { return Math.pow(1000, n); };
+ var names = ["thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion",
+ "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "quattuordecillion",
+ "quindecillion", "sexdecillion", "septendecillion", "octodecillion", "novemdecillion", "vigintillion"];
+ var places = {};
+
+ for (var i = 0; i < names.length; i++) {
+ var value = place(i + 1);
+ places[names[i]] = value;
+ var inverse = 1 / value;
+ places[names[i] + "th"] = inverse;
+ };
+
+ places.tenth = 0.1;
+ places.hundred = 100; places.hundredth = 0.01;
+
+ for (var i in places) {
+ if (!places.hasOwnProperty(i)) continue;
+ places[i + "s"] = places[i];
+ }
+
+ var fillers = {
+ "and": 0,
+ "": 1
+ };
+
+ var negation = {
+ minus: 0,
+ negative: 1
+ };
+
+ var toString = function (n) {
+ var ones = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
+ var tens = [, "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
+ var places = [''].concat(names);
+ var decimals = ["tenth", "hundredth"];
+ for (var i = 0; i < names.length; i++) {
+ decimals.push(names[i] + "th");
+ decimals.push("ten " + names[i] + "th");
+ decimals.push("hundred " + names[i] + "th");
+ }
+ var str = toFixed(n).toString();
+ var ret = [];
+ var toString = function (str) {
+ var ret = [], negative = false;
+ if (str[str.length - 1] === "-") {
+ negative = true;
+ str = str.slice(0, -1);
+ }
+ var start = str.length - str.length % 3;
+ for (var i = start; i >= 0; i -= 3) {
+ var tmp = [];
+ if (str[i + 2] > 0) tmp.push(ones[str[i + 2]], "hundred");
+ if (str[i + 1] === "1") {
+ var x = (str[i + 1] * 10) + +str[i];
+ tmp.push(ones[x]);
+ } else {
+ if (str[i + 1] > 0) tmp.push(tens[str[i + 1]]);
+ if (str[i] > 0) tmp.push(ones[str[i]]);
+ }
+ if (i > 0 && tmp.length) tmp.push(places[i / 3]);
+ ret = ret.concat(tmp);
+ }
+ if (!ret.length) ret.push("zero");
+ if (negative) ret.unshift("negative");
+ return ret.join(" ");
+ }
+ str = str.split(".");
+ ret.push(toString(str[0].split("").reverse().join("")));
+ if (str[1]) {
+ var s = str[1];
+ for (var i = 0; i < s.length; i++) {
+ if (s[i] < 1) continue;
+ ret.push("and");
+ ret.push(ones[s[i]]);
+ var x = decimals[i];
+ if (s[i] > 1 || i % 3 !== 2) x += "s";
+ ret.push(x);
+ }
+ }
+ return ret.join(" ");
+ };
+
+ var number = function (n) {
+ return {
+ value: n,
+ add: function (a) {
+ return number(n + math(a).value);
+ },
+ subtract: function (a) {
+ return number(n - math(a).value);
+ },
+ multiply: function (a) {
+ return number(n * math(a).value);
+ },
+ divide: function (a) {
+ return number(n / math(a).value);
+ },
+ modulo: function (a) {
+ return number(n % math(a).value);
+ },
+ toPower: function (a) {
+ return number(Math.pow(n, math(a).value));
+ },
+ negate: function () {
+ return number(-n);
+ },
+ absoluteValue: function () {
+ return number(Math.abs(n));
+ },
+ round: function () {
+ return number(Math.round(n));
+ },
+ floor: function () {
+ return number(Math.floor(n));
+ },
+ ceiling: function () {
+ return number(Math.ceil(n));
+ },
+ toSquareRoot: function () {
+ return number(Math.sqrt(n));
+ },
+ sine: function () {
+ return number(Math.sin(n));
+ },
+ cosine: function () {
+ return number(Math.cos(n));
+ },
+ tangent: function () {
+ return number(Math.tan(n));
+ },
+ arcSine: function () {
+ return number(Math.asin(n));
+ },
+ arcCosine: function () {
+ return number(Math.acos(n));
+ },
+ arcTangent: function () {
+ return number(Math.atan(n));
+ },
+ toString: function () {
+ return toString(n);
+ }
+ };
+ };
+
+ var multPowers = function (x, y) {
+ if (y < 1 && x > 1) x = 1 / x;
+ return x * y;
+ };
+
+ var math = function (text) {
+ if (typeof text !== "string" || !text.length) throw "Invalid number";
+ var words = text.toLowerCase().split(/[^A-Za-z]+/);
+ var negate = 1;
+ for (var i = 0; i < words.length; i++) {
+ var word = words[i];
+ if (negation.hasOwnProperty(word)) {
+ negate *= -1;
+ words.splice(i, 1);
+ }
+ else if (fillers.hasOwnProperty(word)) words.splice(i, 1);
+ else if (!digits.hasOwnProperty(word) && !places.hasOwnProperty(word)) throw "Invalid word: " + word;
+
+ }
+ var sum = 0;
+ for (var i = 0; i < words.length; i++) {
+ var word = words[i];
+ if (digits.hasOwnProperty(word)) {
+ var digit = digits[word], place = 1, subSum = 0;
+ while (places.hasOwnProperty(words[i + 1])) place = multPowers(place, places[words[++i]]);
+ subSum = digit * place;
+ if (place > 0 && place < 1000) {
+ while (digits.hasOwnProperty(words[i + 1])) subSum += digits[words[++i]];
+ place = 1;
+ while (places.hasOwnProperty(words[i + 1])) place = multPowers(place, places[words[++i]]);
+ subSum = subSum * place;
+ }
+ sum += subSum;
+ }
+ else throw "Unexpected word: " + word;
+ }
+ return number(sum * negate);
+ };
+ $.wordMath = math;
+ $.wordMath.toString = toString;
+ return $.wordMath;
+})({});
View
1  wordMath.vanilla.min.js
@@ -0,0 +1 @@
+var wordMath=function(g){for(var n=function(a){var c=false;if(a<0){c=true;a=-a}if(a<1){var b=parseInt(a.toString().split("e-")[1]);if(b){a*=Math.pow(10,b-1);a="0."+new Array(b).join("0")+a.toString().substring(2)}}else{var b=parseInt(a.toString().split("+")[1]);if(b>20){b-=20;a/=Math.pow(10,b);a+=new Array(b+1).join("0")}}return c?"-"+a:a},f={zero:0,one:1,two:2,three:3,four:4,five:5,six:6,seven:7,eight:8,nine:9,ten:10,eleven:11,twelve:12,thirteen:13,fourteen:14,fifteen:15,sixteen:16,seventeen:17,eighteen:18,nineteen:19,twenty:20,thirty:30,forty:40,fourty:40,fifty:50,sixty:60,seventy:70,eighty:80,ninety:90,pi:Math.PI,e:Math.E},o=function(a){return Math.pow(1e3,a)},c=["thousand","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion","nonillion","decillion","undecillion","duodecillion","tredecillion","quattuordecillion","quindecillion","sexdecillion","septendecillion","octodecillion","novemdecillion","vigintillion"],b={},d=0;d<c.length;d++){var j=o(d+1);b[c[d]]=j;var m=1/j;b[c[d]+"th"]=m}b.tenth=.1;b.hundred=100;b.hundredth=.01;for(var d in b){if(!b.hasOwnProperty(d))continue;b[d+"s"]=b[d]}var l={and:0,"":1},k={minus:0,negative:1},i=function(l){for(var f=["zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"],k=[,"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"],j=[""].concat(c),e=["tenth","hundredth"],a=0;a<c.length;a++){e.push(c[a]+"th");e.push("ten "+c[a]+"th");e.push("hundred "+c[a]+"th")}var d=n(l).toString(),b=[],i=function(a){var d=[],e=false;if(a[a.length-1]==="-"){e=true;a=a.slice(0,-1)}for(var g=a.length-a.length%3,b=g;b>=0;b-=3){var c=[];a[b+2]>0&&c.push(f[a[b+2]],"hundred");if(a[b+1]==="1"){var h=a[b+1]*10+ +a[b];c.push(f[h])}else{a[b+1]>0&&c.push(k[a[b+1]]);a[b]>0&&c.push(f[a[b]])}b>0&&c.length&&c.push(j[b/3]);d=d.concat(c)}!d.length&&d.push("zero");e&&d.unshift("negative");return d.join(" ")};d=d.split(".");b.push(i(d[0].split("").reverse().join("")));if(d[1])for(var g=d[1],a=0;a<g.length;a++){if(g[a]<1)continue;b.push("and");b.push(f[g[a]]);var h=e[a];if(g[a]>1||a%3!==2)h+="s";b.push(h)}return b.join(" ")},a=function(b){return{value:b,add:function(c){return a(b+e(c).value)},subtract:function(c){return a(b-e(c).value)},multiply:function(c){return a(b*e(c).value)},divide:function(c){return a(b/e(c).value)},modulo:function(c){return a(b%e(c).value)},toPower:function(c){return a(Math.pow(b,e(c).value))},negate:function(){return a(-b)},absoluteValue:function(){return a(Math.abs(b))},round:function(){return a(Math.round(b))},floor:function(){return a(Math.floor(b))},ceiling:function(){return a(Math.ceil(b))},toSquareRoot:function(){return a(Math.sqrt(b))},sine:function(){return a(Math.sin(b))},cosine:function(){return a(Math.cos(b))},tangent:function(){return a(Math.tan(b))},arcSine:function(){return a(Math.asin(b))},arcCosine:function(){return a(Math.acos(b))},arcTangent:function(){return a(Math.atan(b))},toString:function(){return i(b)}}},h=function(a,b){if(b<1&&a>1)a=1/a;return a*b},e=function(j){if(typeof j!=="string"||!j.length)throw"Invalid number";for(var d=j.toLowerCase().split(/[^A-Za-z]+/),m=1,c=0;c<d.length;c++){var g=d[c];if(k.hasOwnProperty(g)){m*=-1;d.splice(c,1)}else if(l.hasOwnProperty(g))d.splice(c,1);else if(!f.hasOwnProperty(g)&&!b.hasOwnProperty(g))throw"Invalid word: "+g;}for(var n=0,c=0;c<d.length;c++){var g=d[c];if(f.hasOwnProperty(g)){var o=f[g],e=1,i=0;while(b.hasOwnProperty(d[c+1]))e=h(e,b[d[++c]]);i=o*e;if(e>0&&e<1e3){while(f.hasOwnProperty(d[c+1]))i+=f[d[++c]];e=1;while(b.hasOwnProperty(d[c+1]))e=h(e,b[d[++c]]);i=i*e}n+=i}else throw"Unexpected word: "+g;}return a(n*m)};g.wordMath=e;g.wordMath.toString=i;return g.wordMath}({})

0 comments on commit 69aff19

Please sign in to comment.
Something went wrong with that request. Please try again.