New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

is-function-pure

Package Overview
Dependencies
Maintainers
0
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

is-function-pure

A utility to check if a JavaScript function is pure.

latest
Source
npmnpm
Version
1.1.1
Version published
Maintainers
0
Created
Source

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.

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

Any amount is greatly appreciated! 🌟

Keywords

pure

FAQs

Package last updated on 28 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts