{"id":19181384,"url":"https://github.com/uid11/is-pure-function","last_synced_at":"2025-06-12T03:08:20.212Z","repository":{"id":57277550,"uuid":"67455307","full_name":"uid11/is-pure-function","owner":"uid11","description":"Simple and fast ES3-check that function is pure.","archived":false,"fork":false,"pushed_at":"2016-09-22T21:46:23.000Z","size":11,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-23T02:20:02.172Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/uid11.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-05T22:47:05.000Z","updated_at":"2016-09-14T23:11:36.000Z","dependencies_parsed_at":"2022-09-18T08:41:09.462Z","dependency_job_id":null,"html_url":"https://github.com/uid11/is-pure-function","commit_stats":null,"previous_names":["uid-11222/is-pure-function"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/uid11/is-pure-function","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uid11%2Fis-pure-function","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uid11%2Fis-pure-function/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uid11%2Fis-pure-function/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uid11%2Fis-pure-function/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uid11","download_url":"https://codeload.github.com/uid11/is-pure-function/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uid11%2Fis-pure-function/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259387616,"owners_count":22849747,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T10:53:13.586Z","updated_at":"2025-06-12T03:08:20.169Z","avatar_url":"https://github.com/uid11.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# is-pure-function #\n\n[![NPM version][npm-image]][npm-url] ![dependencies][dependencies-image] [![License MIT][license-image]](LICENSE)\n\n[![NPM](https://nodei.co/npm/is-pure-function.png)](https://nodei.co/npm/is-pure-function/)\n\nSimple and fast ES3-check that function is pure.\n```js\n/** \n * @param {function} fn\n * @param {*} thisArg\n * @param {Array} args\n * @return {boolean}\n */\nfunction isPureFunction(fn, thisArg, args) {/**...*/};\n```\n*fn* is pure, if not throw when called with *thisArg* as this and *args* as arguments in global scope.\n\nPure function cannot use closures, but can use exists global vars (Array, Object, setTimeout, etc.).\nPure function should not have side effects, and cannot throw exceptions.\nArrow function may be pure, but then it should not contain 'this' and 'arguments' words.\nGenerators could not be pure (always product new iterator).\nResult of Function.prototype.bind() could not be pure (because we can not substitute the required value of this).\n\nIt is supposed to use in reactive libraries.\n\n## Examples ##\n```js\nvar isPureFunction = require('is-pure-function');\n\nfunction f(a, b) {\n  return a + b;\n}\n// true\nisPureFunction(f, null, [1, 2]);\n\n\nvar g = function(a) {\n  return function(b) {\n    return a + b;\n  }\n}(1);\n// false (use closure var)\nisPureFunction(g, {}, [1, 2]);\n\n\nfunction getIndex(str, char) {\n  return str.indexOf(char);\n}\n// false (wrong type of arguments)\nisPureFunction(getIndex, null, [1, 2]);\n// true\nisPureFunction(getIndex, null, ['str', 'c']);\n\n\nvar f1 = function(a) {return a;}\n// false (extra arguments)\nisPureFunction(f1, undefined, [1, 2]);\n// false (not enough arguments)\nisPureFunction(f1, undefined, []);\n// true\nisPureFunction(f1, undefined, [1]);\n// throw ReferenceError, missing argument\nisPureFunction(f1, undefined);\n// false (not a function at all)\nisPureFunction({}, undefined, []);\n\n\nvar getThisA = function() {\n  return this.a;\n}\n// false (ReferenceError)\nisPureFunction(getThisA, null, []);\n// true (this -- context-argument of function)\nisPureFunction(getThisA, {}, []);\n\n\nfunction* generator() {\n  return yield null;\n}\n// false\nisPureFunction(generator, 0, []);\n\n\nvar arrow = (a, b) =\u003e a + b;\n// true\nisPureFunction(arrow, '', [1, 2]);\n\n\nvar arrowThis = () =\u003e this;\n// false (this bound in arrow functions)\nisPureFunction(arrowThis, 0, []);\n\n// but\nvar arrowStr = () =\u003e 'this';\n//true\nisPureFunction(arrowStr, 0, []);\n\n\nvar arrowArgs = () =\u003e arguments;\n// false (this bound in arrow functions)\nisPureFunction(arrowArgs, null, []);\n\n// but\nvar arrowComment = () =\u003e {\n  // arguments\n  return 3;\n}\n// true\nisPureFunction(arrowComment, null, []);\n\n\nvar boundFn = function() {}.bind();\n// false (can not substitute the required value of this)\nisPureFunction(boundFn, 0, []);\n\n// it is unlikely you will need\nisPureFunction.clearCache();\n```\nUse pure functions for FRP (the best way is arrow functions, without this and arguments object, with a fixed number of arguments).\n\n## Tests ##\nStandalone pages test/es3.html \u0026\u0026 test/es6.html (via Mocha). Install webpack and opener, build scripts, than run tests:\n```bash\n$ npm install\n$ npm run build\n$ npm test\n```\n\n## License ##\n[MIT](LICENSE)\n\n[license-image]: https://img.shields.io/badge/license-MIT-blue.svg \"license-image\"\n[dependencies-image]: https://img.shields.io/gemnasium/mathiasbynens/he.svg?maxAge=2592000 \"dependencies-image\"\n[npm-image]: https://img.shields.io/npm/v/is-pure-function.svg \"npm-image\"\n[npm-url]: https://www.npmjs.com/package/is-pure-function \"is-pure-function\"","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuid11%2Fis-pure-function","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuid11%2Fis-pure-function","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuid11%2Fis-pure-function/lists"}