# @putout/compare

> compare AST-nodes according to 🦎 PutoutScript

Latest version **19.5.0** (published 2026-07-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install @putout/compare
pnpm add @putout/compare
yarn add @putout/compare
bun add @putout/compare
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 19.5.0 |
| Published | 2026-07-12 |
| First published | 2019-09-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22 |
| Dependencies | 6 |
| Unpacked size | 33.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 795 |
| Author | coderaiser |
| Maintainers | coderaiser |
| Keywords | putout, putout-engine, compare |

## Links

- npm: https://www.npmjs.com/package/@putout/compare
- Repository: https://github.com/coderaiser/putout
- Homepage: https://github.com/coderaiser/putout/tree/master/packages/compare#readme
- Issues: https://github.com/coderaiser/putout/issues
- npm.io page: https://npm.io/package/@putout/compare

## Dependencies (6)

- [obug](https://npm.io/package/obug.md) ^2.1.1
- [jessy](https://npm.io/package/jessy.md) ^5.0.0
- [nessy](https://npm.io/package/nessy.md) ^6.0.0
- [@putout/babel](https://npm.io/package/@putout/babel.md) ^5.0.0
- [@putout/operate](https://npm.io/package/@putout/operate.md) ^15.0.0
- [@putout/engine-parser](https://npm.io/package/@putout/engine-parser.md) ^15.0.1

## Recent versions

- 19.5.0 (latest) — 2026-07-12
- 19.4.0 — 2026-06-19
- 19.3.1 — 2026-03-05
- 19.3.0 — 2026-03-04
- 19.2.0 — 2026-02-11
- 19.1.0 — 2026-02-11
- 19.0.5 — 2026-01-11
- 19.0.4 — 2026-01-11
- 19.0.3 — 2026-01-06
- 19.0.2 — 2025-12-30
- 19.0.1 — 2025-11-28
- 19.0.0 — 2025-11-28
- 18.2.0 — 2025-11-23
- 18.1.0 — 2025-07-26
- 18.0.2 — 2025-06-01
- … 144 more at https://npm.io/package/@putout/compare/versions

## README

# @putout/compare [![NPM version][NPMIMGURL]][NPMURL]

[NPMIMGURL]: https://img.shields.io/npm/v/@putout/compare.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/compare "npm"

Compare AST-nodes according to 🦎[**PutoutScript**](https://github.com/coderaiser/putout/blob/master/docs/putout-script.md#-putoutscript).

## Install

```
npm i @putout/compare
```

## API

### getTemplateValues(node, template)

Get template values from `node` according to 🦎**PutouScript** `template`.

- `node` - `AST-node` or `code` that will be generated;
- `template` - 🦎**PutouScript**;

```js
const {operator} = require('putout');
const {template} = operator;
const node = template.ast('const [] = a');

getTemplateValues(node, 'const __array = array');

// returns
({
    __array: {
        type: 'ArrayPattern',
    },
});
```

### compare(node: string | Node, template: string | Node [, options: Options])

- `node` - `AST-node` or `code` that will be generated;
- `template` - `AST-node` with support of `template variables`.
- `options` - (optional) - object with properties:
- object with properties:
  - `findUp` (default: `true`) - find up template node;

### compareAll(node: string | Node, templates: string[] | Node|Nodes[], [, options: Options])

Compare nodes feats `templates`.

### compareAny(node: string | Node, templates: string[] | Node|Nodes[], [, options: Options])

Compare any nodes that feats one of `templates `

```js
compareAny(path, 'const __a = __b', {
    findUp: false,
});
```

#### Supported template variables:

##### __

Any node.

```js
compare('const x = data', 'const __ = __');
compare('const {x} = data', 'const __ = __');
compare('const x = {data}', 'const __ = __');
compare('<h1>hello</h1>', '<h1>__</h1>');
// returns
true;
```

##### __object

`ObjectPattern` or `ObjectExpression` with any count of `properties`.

```js
compare('const {} = data', 'const __object = __');
compare('const {hello} = data', 'const __object = __');
// returns
true;
```

##### __array

`ArrayPattern` or `ArrayExpression` with any count of `elements`.

```js
compare('const [] = data', 'const __array = __');
compare('const [hello] = data', 'const __array = __');
compare('const hello = [data]', 'const __ = __array');
// returns
true;
```

##### __args, __args__a

Any count of `arguments`:

```js
compare('(a, b, c) => {}', '(__args) => {}');
compare('(a, b) => {}', '(__args) => {}');
compare('() => {}', '(__args) => {}');
// returns
true;
```

Or linked `arguments`:

```js
compare('((a) => fn(a))(value)', '((__args__a) => __c(__args__a))(__args__b)');
// returns
true;

compare('((a) => fn(42))(value)', '((__args__a) => __c(__args__a))(__args__b)');
// returns
false;
```

##### __imports

Any count of `import specifiers`:

```js
compare('import React, {Component} from "react"', 'import __imports from "react"');
// returns
true;
```

##### __exports

Any count of `export specifiers`:

```js
compare('export {scan, fix, report}', 'export {__exports}');
// returns
true;

compare(`export {scan, fix, report} from './plugin.js'`, `export __exports from './plugin.js'`);
// returns
true;
```

##### "__"

Any string literal.

```js
compare('const a = "hello"', 'const __ = "__"');
```

##### __a

Linked node.

```js
compare('const __a = "hello"', 'const __a = "hello"');
```

##### "__a"

Linked string literal.

```js
compare('const a = "hello"', 'const __a = "__b"');
```

##### `__a`

Linked template literal.

```js
compare('const a = `hello`', 'const __a = `__b`');
// returns
true;
```

##### __body

Any `BlockStatement`, `TSModuleBlock` and `ClassBody`.

```js
compare('const a = () => 0', 'const a = () => __body');
// returns
false;

compare('const a = () => {}', 'const a = () => __body');
// returns
true;

compare('function a(b) {return b;}', 'function __(__args) {__body}');
// returns
true;

compare(`class a {hello: 'world'}`, 'class __a {__body}');
// returns
true;

compare(`declare global {var al: any}`, 'declare global {__body}');
// returns
true;
```

##### __jsx_children

Any count of children of `JSXElement`:

```js
compare('<div hello="world"></div>', '<div hello="world">__jsx_children</div>');
// returns
true;

compare('<div hello="world"><span>hi</span></div>', '<div hello="world">__jsx_children</div>');
// returns
true;
```

##### __jsx_attributes

Any count of attributes of `JSXElement`:

```js
compare('<div hello="world"></div>', '<__a __jsx_attributes/>');
// returns
true;
```

##### __nop

Any `Function` with no `arguments` and empty body;

```js
compare('const a = () => {}', 'const __a = __nop');
// returns
true;

compare('const a = async () => {}', 'const a = __nop');
// returns
true;
```

##### __identifier

Any `Identifier`

```js
compare('const a = 5', 'const __identifier = 5');
// returns
true;
```

##### __bool

Any `Boolean`

```js
compare('const a = true', 'const a = __bool');
// returns
true;
```

##### /__a/

Any `regexp`

```js
compare('const a = /hello/g', 'const a = /__a/');
// returns
true;
```

##### `__type_params`

Any count of TSTypeParameter's.

```js
compare('function clear<es, ax, di>() {}', 'function __a<__type_params>(): __c {__body}');
// returns
true;
```

## License

MIT

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