Mathemancy.js – a Framework for Natural Language Arithmetic in JavaScript

by Peter Jaric

A while ago, while I was riding my bike to work, I thought: “Would it possible to make one.plus.two.times.three evaluate to 7 in JavaScript?” I wanted to see if it was possible, and with some invaluable help from Mathias Bynens and Ingvar Stepanyan I was able to make it work.

I give you:

Mathemancy.js

With mathemancy.js , you can write your arithmetic expressions using natural language in property access notation (this was the hard part). Great, huh? Some examples:

three.plus.two === 3 + 2
two.three.minus.four === 23 - 4

var result = six.dividedBy.two.minus.nine.times.four;
result === 6 / 2 - 9 * 4

Please try it out yourself over at the small test bed I created for this purpose.

Limitations

It is not possible to write for example minus.two, or just two.

Neither parentheses nor other notation except +, -, * and / is supported.

This won’t work:

var forty = five.times.eight;
var fortytwo = forty.plus.two;

Code

All code can be found at Github: https://github.com/peterjaric/mathemancy.js

Please feel free to create pull requests! Look at the Limitations section for inspiration. ;)

Seriously?

This is a joke framework of course, but the code works, try it out yourself!