# js-cpa

> Code pattern analysis for JS

Latest version **0.2.1** (published 2017-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install js-cpa
pnpm add js-cpa
yarn add js-cpa
bun add js-cpa
```

Provides the command `js-cpa`.

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2017-09-28 |
| First published | 2017-07-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 127 |
| Author | Vignesh Shanmugam |
| Maintainers | vignesh.shanmugam |
| Keywords | code, repetition, pattern, duplication, javascript |

## Links

- npm: https://www.npmjs.com/package/js-cpa
- Repository: https://github.com/vigneshshanmugam/js-cpa
- npm.io page: https://npm.io/package/js-cpa

## Dependencies (6)

- [ejs](https://npm.io/package/ejs.md) ^2.5.7
- [chalk](https://npm.io/package/chalk.md) ^2.0.1
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [babylon](https://npm.io/package/babylon.md) ^7.0.0-beta.18
- [commander](https://npm.io/package/commander.md) ^2.11.0
- [babel-types](https://npm.io/package/babel-types.md) ^7.0.0-alpha.15

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 0.2.1 (latest) — 2017-09-28
- 0.2.0 — 2017-09-28
- 0.1.0 — 2017-07-18
- 0.0.3 — 2017-07-09
- 0.0.2 — 2017-07-08
- 0.0.1 — 2017-07-08
- 0.0.0 — 2017-07-03

## README

# js-cpa

Identify structurally similar functions that are duplicated across a JavaScript bundle/file seamlessly.

Running it on [Inferno](https://github.com/infernojs/inferno)

<img width="490" alt="CPA on Inferno" src="https://user-images.githubusercontent.com/3902525/28320650-6bd7632e-6bd1-11e7-9837-2af42c79c687.png">

### Features

+ Works only across function boundaries
+ Matches the longest common subsequence and ignores the children
+ Ignores comments on the output
+ HTML report generation

### Related

+ [bundle-duplicates-plugin](https://github.com/vigneshshanmugam/bundle-duplicates-plugin) - Identify duplicate functions across all webpack JS bundles.

### Installation

```sh
npm install -g js-cpa
```

### CLI
```sh
Usage: js-cpa [options] <file ...>


Options:

  -V, --version              output the version number
  -f, --filelist             read filelist from STDIN stream - if/when you cross ARG_MAX. eg: ls *.js | js-cpa -f
  -m, --module               parse files with sourceType=module
  -l, --language <language>  language (js|ts|flow)
  -t, --threshold <n>        threshold (in bytes)
  -C, --no-colors            disable colors in output
  -R, --report <type>        generate reports (html|term)
  -o, --report-file <path>   path for report generation
  -h, --help                 output usage information
```

### API

```js
const { findDuplicates, stringify }= require('js-cpa');
const fs = require("fs");
const code = fs.readFileSync(filePath, "utf-8");
const duplicates = findDuplicates(code, {
  filename: "test"
});

process.stdout.write(stringify(duplicates)); // prints to stdout
```

##### Options
------
+ filename - name of the file used in the output
+ sourceType - denotes the mode the code should be parsed in. eg - script|module
+ language - denotes the language. eg - (js|ts|flow)
+ threshold - threshold in bytes

##### findDuplicates(code, opts)
------
Finds the optimal duplicate functions that are structurally similar. It tries to find the longest matching subexpression across the AST which ignores the children(inner function) if the parent(outer function) is already mached

##### findAllDuplicates(code, opts)
------
Finds all duplicate functions that are structurally similar.

##### stringify(duplicates, options)
------
Gets the output in a more presentable way

Options
+ colors - enable colors on the stdout
+ newline - prints newline after each duplicates

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