is-pure-function
Simple and fast ES3-check that function is pure.
/** * @param * @param * @param * @return */ {/**...*/};
fn is pure, if not throw when called with thisArg as this and args as arguments in global scope.
Pure function cannot use closures, but can use exists global vars (Array, Object, setTimeout, etc.). Pure function should not have side effects, and cannot throw exceptions. Arrow function may be pure, but then it should not contain 'this' and 'arguments' words. Generators could not be pure (always product new iterator). Result of Function.prototype.bind() could not be pure (because we can not substitute the required value of this).
It is supposed to use in reactive libraries.
Examples
var isPureFunction = ; { return a + b;}// true; var { return { return a + b; }}1;// false (use closure var); { return str;}// false (wrong type of arguments);// true; var {return a;}// false (extra arguments);// false (not enough arguments);// true;// throw ReferenceError, missing argument;// false (not a function at all); var { return thisa;}// false (ReferenceError);// true (this -- context-argument of function); { return null;}// false; var a + b;// true; var this;// false (this bound in arrow functions); // butvar 'this';//true; var arguments;// false (this bound in arrow functions); // butvar { // arguments return 3;}// true; var {};// false (can not substitute the required value of this); // it is unlikely you will needisPureFunction;
Use pure functions for FRP (the best way is arrow functions, without this and arguments object, with a fixed number of arguments).
Tests
Standalone pages test/es3.html && test/es6.html (via Mocha). Install webpack and opener, build scripts, than run tests:
$ npm install$ npm run build$ npm test