Skip to content

Instantly share code, notes, and snippets.

@olooney
Created May 28, 2011 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olooney/997192 to your computer and use it in GitHub Desktop.
Save olooney/997192 to your computer and use it in GitHub Desktop.
javascript operators wrapped in functions so you can refer to them by name
op = {
"!!": function(x) { return !!x; },
"!": function(x) { return !x; },
"&&": function(x, y) { return x && y; },
"||": function(x, y) { return x || y; },
"~": function(x) { return ~x; },
"negative": function(x) { return -x; },
"+": function(x, y) { return x + y; },
"-": function(x, y) { return x - y; },
"/": function(x, y) { return x / y; },
"%": function(x, y) { return x % y; },
"*": function(x, y) { return x * y; },
"&": function(x, y) { return x & y; },
"|": function(x, y) { return x | y; },
"^": function(x, y) { return x ^ y; },
"<<": function(x, y) { return x << y; },
">>": function(x, y) { return x >> y; },
">>>": function(x, y) { return x >>> y; },
"==": function(x, y) { return x == y; },
"!=": function(x, y) { return x != y; },
"===": function(x, y) { return x === y; },
"!==": function(x, y) { return x !== y; },
">": function(x, y) { return x > y; },
"<": function(x, y) { return x < y; },
">=": function(x, y) { return x >= y; },
"<=": function(x, y) { return x <= y; },
"typeof": function(x) { return typeof x; },
"in": function(x, y) { return x in y; },
"instanceof": function(x, y) { return x instanceof y; }
};
// english aliases?
op["truth"] = op['!!'];
op["not"] = op['!'];
op["and"] = op['&&'];
op["or"] = op['||'];
op["plus"] = op['+'];
op["minus"] = op['-'];
op["divided by"] = op['/'];
op["times"] = op['*'];
op["modulo"] = op['%'];
op["equals"] = op['==='];
op["does not equal"] = op['!=='];
op["greater than"] = op[">"];
op["less than"] = op["<"];
op["not greater than"] = op["<="];
op["not less than"] = op[">="];
op["contains"] = function(x, y) { return y in x; },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment