spread-diff-patch
Version:
Diff & patch SpreadSheet files
199 lines (194 loc) • 7.74 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
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
var src_exports = {};
__export(src_exports, {
diff: () => diff,
diffWorkBook: () => diffWorkBook,
readCSV: () => readCSV,
readWorkBook: () => readWorkBook
});
module.exports = __toCommonJS(src_exports);
var import_papaparse = __toESM(require("papaparse"));
var import_fs = __toESM(require("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
var import_xlsx = __toESM(require("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 import_papaparse.default.parse(import_fs.default.readFileSync(filePath, "utf8")).data || [];
}
function readWorkBook(filePath) {
return import_xlsx.default.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(
import_xlsx.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(
[],
import_xlsx.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(
import_xlsx.utils.sheet_to_json(actualWorkBook.Sheets[actualSheets == null ? void 0 : actualSheets[i]], { header: 1 }),
import_xlsx.utils.sheet_to_json(expectedWorkBook.Sheets[expectedSheets == null ? void 0 : expectedSheets[i]], { header: 1 }),
comparator
);
diffWorkBook2.diffCount += diffWorkBook2.sheets[expectedSheets[i]].diffCount;
}
}
return diffWorkBook2;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
diff,
diffWorkBook,
readCSV,
readWorkBook
});
//# sourceMappingURL=index.js.map