is-function-pure

1.1.1 • Public • Published

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:

  1. has references to outside scopes
  2. uses Date() or Math.random()
  3. uses this
  4. 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.

Support me on Ko-fi Donate via PayPal Support me on Patreon

Any amount is greatly appreciated! 🌟

Package Sidebar

Install

npm i is-function-pure

Weekly Downloads

102

Version

1.1.1

License

MIT

Unpacked Size

32.9 kB

Total Files

9

Last publish

Collaborators

  • senad87