spread-diff-patch
Version:
Diff & patch SpreadSheet files
162 lines (157 loc) • 5.91 kB
JavaScript
var __accessCheck = (obj, member, msg) => {
if (!member.has(obj))
throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => {
__accessCheck(obj, member, "read from private field");
return getter ? getter.call(obj) : member.get(obj);
};
var __privateAdd = (obj, member, value) => {
if (member.has(obj))
throw TypeError("Cannot add the same private member more than once");
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
var __privateSet = (obj, member, value, setter) => {
__accessCheck(obj, member, "write to private field");
setter ? setter.call(obj, value) : member.set(obj, value);
return value;
};
// src/index.ts
import Papa from "papaparse";
import fs from "fs";
// src/DiffAOA.ts
var _diffCount;
var DiffAOA = class extends Array {
constructor() {
super(...arguments);
__privateAdd(this, _diffCount, 0);
}
/**
* Formats the differences using the provided formatter.
* @param formatter - The formatter object responsible for generating the formatted string.
* @returns The formatted string representing the differences.
*/
format(formatter) {
return formatter.format(this);
}
/**
* Gets the count of differences in the array of arrays.
* @returns The number of differences.
*/
get diffCount() {
return __privateGet(this, _diffCount);
}
/**
* Sets the count of differences in the array of arrays.
* @param value - The number of differences to set.
*/
set diffCount(value) {
__privateSet(this, _diffCount, value);
}
};
_diffCount = new WeakMap();
var DiffAOA_default = DiffAOA;
// src/index.ts
import xlsx, { utils } from "xlsx";
// src/DiffWorkBook.ts
var _diffCount2;
var DiffWorkBook = class {
constructor() {
__privateAdd(this, _diffCount2, 0);
this.sheets = {};
}
/**
* Formats the workbook using the specified formatter.
* @param formatter - The formatter to use for formatting the workbook.
* @returns The formatted workbook.
*/
format(formatter) {
return formatter.format(this.sheets);
}
/**
* Gets the number of differences in the workbook.
*/
get diffCount() {
return __privateGet(this, _diffCount2);
}
/**
* Sets the number of differences in the workbook.
*/
set diffCount(value) {
__privateSet(this, _diffCount2, value);
}
};
_diffCount2 = new WeakMap();
var DiffWorkBook_default = DiffWorkBook;
// src/index.ts
function readCSV(filePath) {
return Papa.parse(fs.readFileSync(filePath, "utf8")).data || [];
}
function readWorkBook(filePath) {
return xlsx.readFile(filePath);
}
function diff(actualAOA, expectedAOA, comparator = (actual, expected) => actual !== expected) {
var _a, _b, _c, _d, _e, _f, _g;
const diffAOA = new DiffAOA_default();
const maxRowCount = Math.max(actualAOA.length, expectedAOA.length);
for (let i = 0; i < maxRowCount; i += 1) {
const diffRow = [];
const maxColCount = Math.max(((_a = actualAOA == null ? void 0 : actualAOA[i]) == null ? void 0 : _a.length) || 0, ((_b = expectedAOA == null ? void 0 : expectedAOA[i]) == null ? void 0 : _b.length) || 0);
for (let j = 0; j < maxColCount; j += 1) {
if (comparator((_c = actualAOA == null ? void 0 : actualAOA[i]) == null ? void 0 : _c[j], (_d = expectedAOA == null ? void 0 : expectedAOA[i]) == null ? void 0 : _d[j])) {
diffAOA.diffCount += 1;
diffRow[j] = [(_e = actualAOA == null ? void 0 : actualAOA[i]) == null ? void 0 : _e[j], (_f = expectedAOA == null ? void 0 : expectedAOA[i]) == null ? void 0 : _f[j]];
} else {
diffRow[j] = (_g = expectedAOA == null ? void 0 : expectedAOA[i]) == null ? void 0 : _g[j];
}
}
diffAOA.push(diffRow);
}
return diffAOA;
}
function diffWorkBook(actualWorkBook, expectedWorkBook, comparator = (actual, expected) => actual !== expected, sheetPatcher = (actual, expected) => {
let patchedString = "";
if (actual)
patchedString += `(-)(${actual})`;
if (expected)
patchedString += `(+)(${expected})`;
return patchedString;
}) {
const diffWorkBook2 = new DiffWorkBook_default();
const actualSheets = actualWorkBook.SheetNames;
const expectedSheets = expectedWorkBook.SheetNames;
const maxSheetCount = Math.max((actualSheets == null ? void 0 : actualSheets.length) || 0, (expectedSheets == null ? void 0 : expectedSheets.length) || 0);
for (let i = 0; i < maxSheetCount; i += 1) {
if (comparator(actualSheets == null ? void 0 : actualSheets[i], expectedSheets == null ? void 0 : expectedSheets[i])) {
const actualPatchedSheet = sheetPatcher(actualSheets == null ? void 0 : actualSheets[i], null);
diffWorkBook2.sheets[actualPatchedSheet] = diff(
utils.sheet_to_json(actualWorkBook.Sheets[actualSheets == null ? void 0 : actualSheets[i]], { header: 1 }),
[],
comparator
);
diffWorkBook2.diffCount += diffWorkBook2.sheets[actualPatchedSheet].diffCount;
const expectedPatchedSheet = sheetPatcher(null, expectedSheets == null ? void 0 : expectedSheets[i]);
diffWorkBook2.sheets[expectedPatchedSheet] = diff(
[],
utils.sheet_to_json(expectedWorkBook.Sheets[expectedSheets == null ? void 0 : expectedSheets[i]], { header: 1 }),
comparator
);
diffWorkBook2.diffCount += diffWorkBook2.sheets[expectedPatchedSheet].diffCount;
} else {
diffWorkBook2.sheets[expectedSheets[i]] = diff(
utils.sheet_to_json(actualWorkBook.Sheets[actualSheets == null ? void 0 : actualSheets[i]], { header: 1 }),
utils.sheet_to_json(expectedWorkBook.Sheets[expectedSheets == null ? void 0 : expectedSheets[i]], { header: 1 }),
comparator
);
diffWorkBook2.diffCount += diffWorkBook2.sheets[expectedSheets[i]].diffCount;
}
}
return diffWorkBook2;
}
export {
diff,
diffWorkBook,
readCSV,
readWorkBook
};
//# sourceMappingURL=index.mjs.map