git-similarity-index
v1.2.2
Published
🔨 This simple tool calculates the similarity index between two files.
Downloads
36
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 mocks/file1.txt mocks/file2.md
# or
git-similarity-index mocks/file1.txt mocks/file2.md --use-git
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(
"mocks/file1.txt",
"mocks/file2.md",
);
console.log(similarityIndex); // 46.34
})();
// getSimilarityIndexForFiles + useGit
(async function () {
const similarityIndex = await getSimilarityIndexForFiles(
"mocks/file1.txt",
"mocks/file2.md",
{ useGit: true },
);
console.log(similarityIndex); // 46.34
})();
Debug
The tool itself uses the debug
library. You can enable debugging by setting the DEBUG
environment variable.
# to display all logs
DEBUG=git-similarity-index:* git-similarity-index mocks/file1.txt mocks/file2.md
# focus only of bytes
DEBUG=git-similarity-index:bytes git-similarity-index mocks/file1.txt mocks/file2.md
# focus only of text
DEBUG=git-similarity-index:text git-similarity-index mocks/file1.txt mocks/file2.md
# focus only of files
DEBUG=git-similarity-index:files git-similarity-index mocks/file1.txt mocks/file2.md
License
The MIT License @ 2024