UNPKG

git-similarity-index

Version:

🔨 This simple tool calculates the similarity index between two files.

29 lines (28 loc) • 1.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLinesBytes = getLinesBytes; exports.getSimilarityIndexForText = getSimilarityIndexForText; const debug_1 = require("debug"); const getSimilarityIndex_1 = require("./getSimilarityIndex"); const logger = (0, debug_1.default)("git-similarity-index:text"); function getLinesBytes(bytes) { return bytes.reduce((acc, byte) => { if (byte === 10) { acc[acc.length - 1].push(10); acc.push([]); } else { acc[acc.length - 1].push(byte); } return acc; }, [[]]); } function getSimilarityIndexForText(content1, content2) { logger("Calculating similarity index for text", { content1, content2 }); const bytes1 = Buffer.from(content1).toJSON(); const lineBytes1 = getLinesBytes(bytes1.data); const bytes2 = Buffer.from(content2).toJSON(); const size2 = bytes2.data.length; const lineBytes2 = getLinesBytes(bytes2.data); return (0, getSimilarityIndex_1.getSimilarityIndex)(lineBytes1, lineBytes2, size2); }