# @dwidge/function-ops

> Instrument a function with a logger function.

Latest version **1.0.2** (published 2023-01-07) · BSL-1.0 license · 0 weekly downloads

## Install

```sh
npm install @dwidge/function-ops
pnpm add @dwidge/function-ops
yarn add @dwidge/function-ops
bun add @dwidge/function-ops
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2023-01-07 |
| First published | 2023-01-07 |
| Weekly downloads | 0 |
| License | BSL-1.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | DWJ |
| Maintainers | dwidge |

## Links

- npm: https://www.npmjs.com/package/@dwidge/function-ops
- npm.io page: https://npm.io/package/@dwidge/function-ops

## Dependencies (1)

- [transform-ast](https://npm.io/package/transform-ast.md) ^2.4.4

## Recent versions

- 1.0.2 (latest) — 2023-01-07
- 1.0.1 — 2023-01-07

## README

# @dwidge/function-ops

This package provides a utility to instrument a function with a logger function.

The instrument function returns a new function that behaves the same as the original function, but calls the logger function whenever an expression is evaluated.

## Installation

`npm i @dwidge/function-ops`

## Usage

```js
instrument(
  () => {},
  (type, [start, end], [exp, val], ...args) => {}
);
```

```js
const instrument = require("@dwidge/function-ops");

const f = function (a, b, c) {
  a = b + c * 2;
  return a - Math.min(1, 2, 3, c);
};

const logger = (type, [start, end], [exp, val], ...args) => {
  console.log(`${type}@${start}-${end}`);
  console.log(`  ${exp} = ${val}`);
  console.log(
    `  where ${args.map(([exp, val]) => `${exp} = ${val}`).join(" and ")}`
  );
};

const flog = instrument(f, logger);
console.log(flog(3, 4, 5));
```

```
BinaryExpression@28-37
  b + c * 2 = 14
  where b = 4 and c * 2 = 10
BinaryExpression@32-37
  c * 2 = 10
  where c = 5 and 2 = 2
BinaryExpression@49-73
  a - Math.min(1, 2, 3, c) = 13
  where a = 14 and Math.min(1, 2, 3, c) = 1
CallExpression@53-73
  Math.min(1, 2, 3, c) = 1
  where 1 = 1 and 2 = 2 and 3 = 3 and c = 5
13
```

## Logger

```js
(type, [start, end], [exp, val], ...args) => {};
```

| Argument     | Type   | Description                                                                                                                                              |
| ------------ | ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| type         | string | The type of the expression being logged                                                                                                                  |
| [start, end] | array  | An array representing the start and end positions of the expression in the original function                                                             |
| [exp, val]   | array  | An array representing the expression and its value                                                                                                       |
| args         | array  | An array of arrays representing the arguments of the expression, each array containing two elements [exp, val] representing the expression and its value |

## Testing

`npm test`

## Credits

transform-ast  
ChatGPT

## License

This package is licensed under the Boost Software License. See the LICENSE file for more information.

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