git-similarity-index
v1.0.1
Published
🔨 This simple tool calculates the similarity index between two files.
Downloads
19
Maintainers
Readme
git-similarity-index
🔨 This simple tool calculates the similarity index between two files.
Motivation
I would like to calculate the similarity between two files. Unfortunately, but Git does not provide a command that would count the ‘similarity index’. Therefore, I decided to write such a tool myself that counts this index.
CLI
npm install -g git-similarity-index
git-similarity-index path/to/file1 path/to/file2
Usage
import {
getSimilarityIndex,
getLinesBytes,
getSimilarityIndexForText,
getSimilarityIndexForFiles
} from "git-similarity-index";
// getSimilarityIndex + getLinesBytes
(function () {
const toBytes = (text) => Buffer.from(text).toJSON().data;
const firstPattern = "a\n";
const secondPattern = "a\nb";
const similarityIndex = getSimilarityIndex(
getLinesBytes(toBytes(firstPattern)),
getLinesBytes(toBytes(secondPattern)),
toBytes(secondPattern).length,
);
console.log(similarityIndex); // 66.67
})();
// getSimilarityIndexForText
(function () {
const firstPattern = "a\nb\nc\n";
const secondPattern = "a\nb\nc\nd";
const similarityIndex = getSimilarityIndexForText(
firstPattern,
secondPattern,
);
console.log(similarityIndex); // 85.71
})();
// getSimilarityIndexForFiles
(async function () {
const similarityIndex = await getSimilarityIndexForFiles(
"path/to/file1",
"path/to/file2",
);
console.log(similarityIndex); // 63.64
})();
License
The MIT License @ 2024