UNPKG

@progress/kendo-e2e

Version:

Kendo UI end-to-end test utilities.

114 lines 5.29 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.matchXLSXFiles = matchXLSXFiles; const yauzl_1 = __importDefault(require("yauzl")); /** * Extracts the content of all files in an .xlsx file except those in the `docProps` folder as Buffers. * @param {string} xlsxFilePath - Path to the .xlsx file. * @returns {Promise<Object>} - A promise that resolves to an object with file names as keys and their data as Buffer values. */ function extractAllContentExceptDocProps(xlsxFilePath) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { yauzl_1.default.open(xlsxFilePath, { lazyEntries: true }, (err, zipfile) => { if (err) { reject(err); return; } const fileBuffers = {}; // Store file contents as key-value pairs zipfile.on('entry', (entry) => { // Exclude files in the `docProps` folder if (!entry.fileName.startsWith('docProps')) { let fileBuffer = Buffer.alloc(0); // Start with an empty buffer zipfile.openReadStream(entry, (err, readStream) => { if (err) { reject(err); return; } readStream.on('data', (chunk) => { fileBuffer = Buffer.concat([fileBuffer, chunk]); }); readStream.on('end', () => { fileBuffers[entry.fileName] = fileBuffer; // Save buffer with the file name as key zipfile.readEntry(); // Continue to the next entry }); readStream.on('error', (err) => { reject(err); }); }); } else { zipfile.readEntry(); // Skip entries in `docProps` and continue } }); zipfile.on('end', () => { resolve(fileBuffers); // Resolve with the collected file buffers }); zipfile.on('error', (err) => { reject(err); }); zipfile.readEntry(); // Start reading entries }); }); }); } /** * Compare the extracted content of two .xlsx files. * @param {Object} fileContents1 - Extracted content of the first file as an object. * @param {Object} fileContents2 - Extracted content of the second file as an object. * @returns {boolean} - True if all files' contents are the same, false otherwise. */ function compareFileContents(fileContents1, fileContents2) { const fileNames1 = Object.keys(fileContents1); const fileNames2 = Object.keys(fileContents2); // Compare the sets of file names if (fileNames1.length !== fileNames2.length || !fileNames1.every((name) => fileNames2.includes(name))) { return false; } // Compare the content of each file for (const fileName of fileNames1) { if (!fileContents1[fileName].equals(fileContents2[fileName])) { return false; } } return true; } /** * Main function to extract, convert, and compare the content of two .xlsx files. * @param {string} filePath1 - Path to the first .xlsx file. * @param {string} filePath2 - Path to the second .xlsx file. */ function matchXLSXFiles(filePath1, filePath2) { return __awaiter(this, void 0, void 0, function* () { try { // Extract content from both files const fileContents1 = yield extractAllContentExceptDocProps(filePath1); const fileContents2 = yield extractAllContentExceptDocProps(filePath2); // Compare the extracted content if (compareFileContents(fileContents1, fileContents2)) { console.log(`The contents of the files are the same.`); return true; } else { console.log(`The contents of the files are different.`); return false; } } catch (error) { throw new Error(`Error: ${error}`); } }); } //# sourceMappingURL=excel.js.map