# is-function-pure

> A utility to check if a JavaScript function is pure.

Latest version **1.1.1** (published 2024-10-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install is-function-pure
pnpm add is-function-pure
yarn add is-function-pure
bun add is-function-pure
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2024-10-28 |
| First published | 2024-10-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 32.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Senad Mehic |
| Maintainers | senad87 |
| Keywords | pure, function, javascript, utility |

## Links

- npm: https://www.npmjs.com/package/is-function-pure
- Repository: https://github.com/senad87/is-pure
- Homepage: https://github.com/senad87/is-pure#readme
- Issues: https://github.com/senad87/is-pure/issues
- npm.io page: https://npm.io/package/is-function-pure

## Dependencies (3)

- [escope](https://npm.io/package/escope.md) 3.6.0
- [recast](https://npm.io/package/recast.md) 0.12.8
- [esprima](https://npm.io/package/esprima.md) 4.0.0

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2024-10-28
- 1.1.0 — 2024-10-22
- 1.0.8 — 2024-10-20
- 1.0.7 — 2024-10-20
- 1.0.6 — 2024-10-20
- 1.0.5 — 2024-10-20
- 1.0.4 — 2024-10-20
- 1.0.2 — 2024-10-20
- 1.0.1 — 2024-10-20
- 1.0.0 — 2024-10-20

## 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:

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](https://img.shields.io/badge/Ko--fi-Buy%20Me%20a%20Coffee-FF5E5B?style=for-the-badge&logo=ko-fi&logoColor=white)](https://ko-fi.com/senad87)
[![Donate via PayPal](https://img.shields.io/badge/PayPal-Donate-blue?style=for-the-badge&logo=paypal&logoColor=white)](https://www.paypal.com/paypalme/senadmehic87)
[![Support me on Patreon](https://img.shields.io/badge/Patreon-Support%20Me-F96854?style=for-the-badge&logo=patreon&logoColor=white)](https://www.patreon.com/senad87)

Any amount is greatly appreciated! 🌟

---
_Source: https://npm.io/package/is-function-pure · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
