is-function-pure
v1.1.1
Published
A utility to check if a JavaScript function is pure.
Downloads
618
Maintainers
Readme
is-function-pure
is-function-pure is a JavaScript utility for detecting if function is pure.
Installation
To install the package, use npm:
npm install is-function-pure
Usage
isPure(singleFunctionSourseCode)
The isPure function checks if a single function in the given code is pure. It returns true if the function is pure, and false otherwise.
Input: A string containing a single JavaScript function.
Output: A boolean indicating whether the function is pure.
const { isPure } = require('is-function-pure');
const pureFunction = `
function multiply(a, b) {
return a * b;
}
`;
console.log(isPure(pureFunction)); // Output: true
separate(sourceCode)
The separate function analyzes a block of code and returns an object containing two arrays: pure and impure. Each array holds details about the functions found in the code.
Input: A string containing JavaScript code.
Output: An object with pure and impure arrays.
const { separate } = require('is-function-pure');
const code = `
const x = 5;
function add(a, b) {
return a + b;
}
function impure(a) {
return a + x;
}
`;
const result = separate(code);
console.log('Pure Functions:', result.pure);
console.log('Impure Functions:', result.impure);
Function is NOT pure if it:
- has references to outside scopes
- uses Date() or Math.random()
- uses this
- does not have return statement
More about what is considered pure or not you can read in test cases: https://github.com/senad87/is-function-pure/blob/main/tests/main-test.js https://github.com/senad87/is-function-pure/blob/main/tests/purity-detector-test.js
Testing
Tests are written using chai and can be run with:
npm run test
Sponsors
Love this project? ❤️ Support its development by becoming a sponsor.
Any amount is greatly appreciated! 🌟