API

API

Use jscpd programmatically in your applications.

Overview

jscpd provides a programming API for integrating copy/paste detection into your applications.

Basic Usage

Promise API

import { IClone } from '@jscpd/core';
import { jscpd } from 'jscpd';

const clones: Promise<IClone[]> = jscpd(process.argv);

Async/Await API

import { IClone } from '@jscpd/core';
import { jscpd } from 'jscpd';

(async () => {
  const clones: IClone[] = await jscpd(['', '', __dirname, '-r', 'json']);
  console.log(clones);
})();

Options

Pass options programmatically:

import { jscpd } from 'jscpd';

const options = [
  '', // placeholder for node
  '', // placeholder for script
  './src',
  '--min-lines', '5',
  '--min-tokens', '50',
  '--reporters', 'json',
  '--output', './reports'
];

const clones = await jscpd(options);

Clone Interface

Each detected clone has the following structure:

interface IClone {
  format: string;
  lines: number;
  tokens: number;
  firstFile: ISourceLocation;
  secondFile: ISourceLocation;
  duplicationA: ISourceLines;
  duplicationB: ISourceLines;
}

interface ISourceLocation {
  name: string;
  start: number;
  end: number;
  startLoc: ILocation;
  endLoc: ILocation;
}