spread-diff-patch
Advanced tools
+6
-5
@@ -57,9 +57,10 @@ import { D as DiffAOA, W as WorkbookFormatter } from './workbook-BYxHbFy7.mjs'; | ||
| * @template T - The type of the elements in the workbooks. | ||
| * @param {WorkBook} actualWorkBook - The actual workbook. | ||
| * @param {WorkBook} expectedWorkBook - The expected workbook. | ||
| * @param {(actual: T, expected: T) => boolean} [comparator] - The comparator function to compare elements in the workbooks. Defaults to a function that checks for strict inequality. | ||
| * @returns {DiffWorkBook<T>} - The diff workbook containing the differences between the two workbooks. | ||
| * @param actualWorkBook - The actual workbook. | ||
| * @param expectedWorkBook - The expected workbook. | ||
| * @param comparator - The comparator function to compare elements in the workbooks. Defaults to a function that checks for inequality. | ||
| * @param sheetPatcher - The function to generate a patched string for sheet names. Defaults to a function that generates a string with added and removed sheet names. | ||
| * @returns - The diff workbook containing the differences between the two workbooks. | ||
| */ | ||
| declare function diffWorkBook<T>(actualWorkBook: WorkBook, expectedWorkBook: WorkBook, comparator?: (actual: T, expected: T) => boolean): DiffWorkBook<T>; | ||
| declare function diffWorkBook<T>(actualWorkBook: WorkBook, expectedWorkBook: WorkBook, comparator?: (actual: T, expected: T) => boolean, sheetPatcher?: (actual: string | null, expected: string | null) => string): DiffWorkBook<T>; | ||
| export { diff, diffWorkBook, readCSV, readWorkBook }; |
+6
-5
@@ -57,9 +57,10 @@ import { D as DiffAOA, W as WorkbookFormatter } from './workbook-CVjI4Nft.js'; | ||
| * @template T - The type of the elements in the workbooks. | ||
| * @param {WorkBook} actualWorkBook - The actual workbook. | ||
| * @param {WorkBook} expectedWorkBook - The expected workbook. | ||
| * @param {(actual: T, expected: T) => boolean} [comparator] - The comparator function to compare elements in the workbooks. Defaults to a function that checks for strict inequality. | ||
| * @returns {DiffWorkBook<T>} - The diff workbook containing the differences between the two workbooks. | ||
| * @param actualWorkBook - The actual workbook. | ||
| * @param expectedWorkBook - The expected workbook. | ||
| * @param comparator - The comparator function to compare elements in the workbooks. Defaults to a function that checks for inequality. | ||
| * @param sheetPatcher - The function to generate a patched string for sheet names. Defaults to a function that generates a string with added and removed sheet names. | ||
| * @returns - The diff workbook containing the differences between the two workbooks. | ||
| */ | ||
| declare function diffWorkBook<T>(actualWorkBook: WorkBook, expectedWorkBook: WorkBook, comparator?: (actual: T, expected: T) => boolean): DiffWorkBook<T>; | ||
| declare function diffWorkBook<T>(actualWorkBook: WorkBook, expectedWorkBook: WorkBook, comparator?: (actual: T, expected: T) => boolean, sheetPatcher?: (actual: string | null, expected: string | null) => string): DiffWorkBook<T>; | ||
| export { diff, diffWorkBook, readCSV, readWorkBook }; |
+17
-5
@@ -153,3 +153,12 @@ "use strict"; | ||
| } | ||
| function diffWorkBook(actualWorkBook, expectedWorkBook, comparator = (actual, expected) => actual !== expected) { | ||
| function diffWorkBook(actualWorkBook, expectedWorkBook, comparator = (actual, expected) => actual !== expected, sheetPatcher = (actual, expected) => { | ||
| let patchedString = ""; | ||
| if (actual) | ||
| patchedString += `(-)(${actual})`; | ||
| if (actual && expected) | ||
| patchedString += " "; | ||
| if (expected) | ||
| patchedString += `(+)(${expected})`; | ||
| return patchedString; | ||
| }) { | ||
| const diffWorkBook2 = new DiffWorkBook_default(); | ||
@@ -161,3 +170,4 @@ const actualSheets = actualWorkBook.SheetNames; | ||
| if (comparator(actualSheets == null ? void 0 : actualSheets[i], expectedSheets == null ? void 0 : expectedSheets[i])) { | ||
| diffWorkBook2.sheets[actualSheets == null ? void 0 : actualSheets[i]] = diff( | ||
| 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 }), | ||
@@ -167,4 +177,6 @@ [], | ||
| ); | ||
| diffWorkBook2.diffCount += diffWorkBook2.sheets[actualSheets == null ? void 0 : actualSheets[i]].diffCount; | ||
| diffWorkBook2.sheets[expectedSheets == null ? void 0 : expectedSheets[i]] = diff( | ||
| diffWorkBook2.diffCount += diffWorkBook2.sheets[actualPatchedSheet].diffCount; | ||
| const expectedPatchedSheet = sheetPatcher(null, expectedSheets == null ? void 0 : expectedSheets[i]); | ||
| console.log({ actualPatchedSheet, expectedPatchedSheet }); | ||
| diffWorkBook2.sheets[expectedPatchedSheet] = diff( | ||
| [], | ||
@@ -174,3 +186,3 @@ import_xlsx.utils.sheet_to_json(expectedWorkBook.Sheets[expectedSheets == null ? void 0 : expectedSheets[i]], { header: 1 }), | ||
| ); | ||
| diffWorkBook2.diffCount += diffWorkBook2.sheets[expectedSheets == null ? void 0 : expectedSheets[i]].diffCount; | ||
| diffWorkBook2.diffCount += diffWorkBook2.sheets[expectedPatchedSheet].diffCount; | ||
| } else { | ||
@@ -177,0 +189,0 @@ diffWorkBook2.sheets[expectedSheets[i]] = diff( |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../src/index.ts","../src/DIffAOA.ts","../src/DiffWorkBook.ts"],"sourcesContent":["import Papa from \"papaparse\";\r\nimport fs from 'fs';\r\nimport DiffAOA from \"./DIffAOA\";\r\nimport xlsx, { WorkBook, utils } from 'xlsx';\r\nimport DiffWorkBook from \"./DiffWorkBook\";\r\n\r\n/**\r\n * Reads a CSV file and parses its content into a two-dimensional array.\r\n * @param filePath - The path to the CSV file.\r\n * @returns A two-dimensional array representing the parsed CSV data.\r\n * @template T - The type of data in the CSV file.\r\n */\r\nexport function readCSV<T>(filePath: string): T[][] {\r\n return Papa.parse<T[]>(fs.readFileSync(filePath, \"utf8\")).data || [];\r\n}\r\n/**\r\n * Reads a workbook from the specified file path.\r\n * @param filePath - The path to the workbook file.\r\n * @returns The parsed workbook object.\r\n */\r\nexport function readWorkBook(filePath: string) {\r\n return xlsx.readFile(filePath)\r\n}\r\n\r\n/**\r\n * Compares two arrays of arrays (AOA) and identifies differences based on a custom comparator function.\r\n * @param actualAOA - The actual array of arrays.\r\n * @param expectedAOA - The expected array of arrays for comparison.\r\n * @param comparator - A function to compare individual elements in the arrays (default is a simple !== comparison).\r\n * @returns An instance of DiffAOA<T> representing the differences between the two arrays of arrays.\r\n * @template T - The type of data in the arrays of arrays.\r\n */\r\nexport function diff<T>(\r\n actualAOA: T[][],\r\n expectedAOA: T[][],\r\n comparator: (actual: T, expected: T) => boolean = (actual, expected) => actual !== expected\r\n): DiffAOA<T> {\r\n const diffAOA: DiffAOA<T> = new DiffAOA();\r\n const maxRowCount = Math.max(actualAOA.length, expectedAOA.length);\r\n\r\n for (let i = 0; i < maxRowCount; i += 1) {\r\n const diffRow: (T | (T | null)[])[] = [];\r\n const maxColCount = Math.max(actualAOA?.[i]?.length || 0, expectedAOA?.[i]?.length || 0);\r\n\r\n for (let j = 0; j < maxColCount; j += 1) {\r\n if (comparator(actualAOA?.[i]?.[j], expectedAOA?.[i]?.[j])) {\r\n diffAOA.diffCount += 1;\r\n diffRow[j] = [actualAOA?.[i]?.[j], expectedAOA?.[i]?.[j]];\r\n } else {\r\n diffRow[j] = expectedAOA?.[i]?.[j];\r\n }\r\n }\r\n\r\n diffAOA.push(diffRow);\r\n }\r\n return diffAOA;\r\n}\r\n\r\n/**\r\n * Calculates the difference between two workbooks.\r\n * @template T - The type of the elements in the workbooks.\r\n * @param {WorkBook} actualWorkBook - The actual workbook.\r\n * @param {WorkBook} expectedWorkBook - The expected workbook.\r\n * @param {(actual: T, expected: T) => boolean} [comparator] - The comparator function to compare elements in the workbooks. Defaults to a function that checks for strict inequality.\r\n * @returns {DiffWorkBook<T>} - The diff workbook containing the differences between the two workbooks.\r\n */\r\nexport function diffWorkBook<T>(\r\n actualWorkBook: WorkBook,\r\n expectedWorkBook: WorkBook,\r\n comparator: (actual: T, expected: T) => boolean = (actual, expected) => actual !== expected\r\n) {\r\n const diffWorkBook: DiffWorkBook<T> = new DiffWorkBook()\r\n const actualSheets = actualWorkBook.SheetNames\r\n const expectedSheets = expectedWorkBook.SheetNames\r\n const maxSheetCount = Math.max(actualSheets?.length || 0, expectedSheets?.length || 0)\r\n for (let i = 0; i < maxSheetCount; i += 1) {\r\n if (comparator(actualSheets?.[i] as T, expectedSheets?.[i] as T)) {\r\n diffWorkBook.sheets[actualSheets?.[i]] = diff<T>(\r\n utils.sheet_to_json(actualWorkBook.Sheets[actualSheets?.[i]], { header: 1 }),\r\n [],\r\n comparator\r\n )\r\n diffWorkBook.diffCount += diffWorkBook.sheets[actualSheets?.[i]].diffCount\r\n diffWorkBook.sheets[expectedSheets?.[i]] = diff<T>(\r\n [],\r\n utils.sheet_to_json(expectedWorkBook.Sheets[expectedSheets?.[i]], { header: 1 }),\r\n comparator\r\n )\r\n diffWorkBook.diffCount += diffWorkBook.sheets[expectedSheets?.[i]].diffCount\r\n }\r\n else {\r\n diffWorkBook.sheets[expectedSheets[i]] = diff<T>(\r\n utils.sheet_to_json(actualWorkBook.Sheets[actualSheets?.[i]], { header: 1 }),\r\n utils.sheet_to_json(expectedWorkBook.Sheets[expectedSheets?.[i]], { header: 1 }),\r\n comparator\r\n )\r\n diffWorkBook.diffCount += diffWorkBook.sheets[expectedSheets[i]].diffCount\r\n }\r\n }\r\n return diffWorkBook\r\n}","import Formatter from \"./formatter\";\r\n\r\n/**\r\n * Represents the differences between two arrays of arrays (AOA) with a customizable format.\r\n * @template T - The type of data in the arrays of arrays.\r\n */\r\nclass DiffAOA<T> extends Array<Array<T | Array<T | null>>> {\r\n #diffCount: number = 0;\r\n\r\n /**\r\n * Formats the differences using the provided formatter.\r\n * @param formatter - The formatter object responsible for generating the formatted string.\r\n * @returns The formatted string representing the differences.\r\n */\r\n format(formatter: Formatter<T>): string {\r\n return formatter.format(this as T[][]);\r\n }\r\n\r\n /**\r\n * Gets the count of differences in the array of arrays.\r\n * @returns The number of differences.\r\n */\r\n get diffCount() {\r\n return this.#diffCount;\r\n }\r\n\r\n /**\r\n * Sets the count of differences in the array of arrays.\r\n * @param value - The number of differences to set.\r\n */\r\n set diffCount(value: number) {\r\n this.#diffCount = value;\r\n }\r\n}\r\n\r\nexport default DiffAOA;","import DiffAOA from \"./DIffAOA\";\r\nimport { WorkbookFormatter } from \"./formatter/workbook\";\r\n/**\r\n * Represents a workbook that stores the differences between two workbooks.\r\n * @template T - The type of data stored in the workbook.\r\n */\r\nclass DiffWorkBook<T> {\r\n sheets: { [sheet: string]: DiffAOA<T>; };\r\n #diffCount: number = 0;\r\n\r\n constructor() {\r\n this.sheets = {};\r\n }\r\n\r\n /**\r\n * Formats the workbook using the specified formatter.\r\n * @param formatter - The formatter to use for formatting the workbook.\r\n * @returns The formatted workbook.\r\n */\r\n format(formatter: WorkbookFormatter<T>) {\r\n return formatter.format(this.sheets);\r\n }\r\n\r\n /**\r\n * Gets the number of differences in the workbook.\r\n */\r\n get diffCount() {\r\n return this.#diffCount;\r\n }\r\n\r\n /**\r\n * Sets the number of differences in the workbook.\r\n */\r\n set diffCount(value: number) {\r\n this.#diffCount = value;\r\n }\r\n}\r\n\r\nexport default DiffWorkBook;"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAiB;AACjB,gBAAe;;;ACDf;AAMA,IAAM,UAAN,cAAyB,MAAkC;AAAA,EAA3D;AAAA;AACI,mCAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOrB,OAAO,WAAiC;AACpC,WAAO,UAAU,OAAO,IAAa;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,YAAY;AACZ,WAAO,mBAAK;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,UAAU,OAAe;AACzB,uBAAK,YAAa;AAAA,EACtB;AACJ;AA1BI;AA4BJ,IAAO,kBAAQ;;;ADhCf,kBAAsC;;;AEHtC,IAAAA;AAMA,IAAM,eAAN,MAAsB;AAAA,EAIlB,cAAc;AAFd,uBAAAA,aAAqB;AAGjB,SAAK,SAA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../src/DiffWorkBook.ts"],"sourcesContent":["import Papa from \"papaparse\";\r\nimport fs from 'fs';\r\nimport DiffAOA from \"./DIffAOA\";\r\nimport xlsx, { WorkBook, utils } from 'xlsx';\r\nimport DiffWorkBook from \"./DiffWorkBook\";\r\n\r\n/**\r\n * Reads a CSV file and parses its content into a two-dimensional array.\r\n * @param filePath - The path to the CSV file.\r\n * @returns A two-dimensional array representing the parsed CSV data.\r\n * @template T - The type of data in the CSV file.\r\n */\r\nexport function readCSV<T>(filePath: string): T[][] {\r\n return Papa.parse<T[]>(fs.readFileSync(filePath, \"utf8\")).data || [];\r\n}\r\n/**\r\n * Reads a workbook from the specified file path.\r\n * @param filePath - The path to the workbook file.\r\n * @returns The parsed workbook object.\r\n */\r\nexport function readWorkBook(filePath: string) {\r\n return xlsx.readFile(filePath)\r\n}\r\n\r\n/**\r\n * Compares two arrays of arrays (AOA) and identifies differences based on a custom comparator function.\r\n * @param actualAOA - The actual array of arrays.\r\n * @param expectedAOA - The expected array of arrays for comparison.\r\n * @param comparator - A function to compare individual elements in the arrays (default is a simple !== comparison).\r\n * @returns An instance of DiffAOA<T> representing the differences between the two arrays of arrays.\r\n * @template T - The type of data in the arrays of arrays.\r\n */\r\nexport function diff<T>(\r\n actualAOA: T[][],\r\n expectedAOA: T[][],\r\n comparator: (actual: T, expected: T) => boolean = (actual, expected) => actual !== expected\r\n): DiffAOA<T> {\r\n const diffAOA: DiffAOA<T> = new DiffAOA();\r\n const maxRowCount = Math.max(actualAOA.length, expectedAOA.length);\r\n\r\n for (let i = 0; i < maxRowCount; i += 1) {\r\n const diffRow: (T | (T | null)[])[] = [];\r\n const maxColCount = Math.max(actualAOA?.[i]?.length || 0, expectedAOA?.[i]?.length || 0);\r\n\r\n for (let j = 0; j < maxColCount; j += 1) {\r\n if (comparator(actualAOA?.[i]?.[j], expectedAOA?.[i]?.[j])) {\r\n diffAOA.diffCount += 1;\r\n diffRow[j] = [actualAOA?.[i]?.[j], expectedAOA?.[i]?.[j]];\r\n } else {\r\n diffRow[j] = expectedAOA?.[i]?.[j];\r\n }\r\n }\r\n\r\n diffAOA.push(diffRow);\r\n }\r\n return diffAOA;\r\n}\r\n\r\n/**\r\n * Calculates the difference between two workbooks.\r\n * @template T - The type of the elements in the workbooks.\r\n * @param actualWorkBook - The actual workbook.\r\n * @param expectedWorkBook - The expected workbook.\r\n * @param comparator - The comparator function to compare elements in the workbooks. Defaults to a function that checks for inequality.\r\n * @param sheetPatcher - The function to generate a patched string for sheet names. Defaults to a function that generates a string with added and removed sheet names.\r\n * @returns - The diff workbook containing the differences between the two workbooks.\r\n */\r\nexport function diffWorkBook<T>(\r\n actualWorkBook: WorkBook,\r\n expectedWorkBook: WorkBook,\r\n comparator: (actual: T, expected: T) => boolean = (actual, expected) => actual !== expected,\r\n sheetPatcher = (actual: string | null, expected: string | null) => {\r\n let patchedString = \"\"\r\n if (actual)\r\n patchedString += `(-)(${actual})`\r\n if (actual && expected)\r\n patchedString += \" \"\r\n if (expected)\r\n patchedString += `(+)(${expected})`\r\n return patchedString\r\n }\r\n) {\r\n const diffWorkBook: DiffWorkBook<T> = new DiffWorkBook()\r\n const actualSheets = actualWorkBook.SheetNames\r\n const expectedSheets = expectedWorkBook.SheetNames\r\n const maxSheetCount = Math.max(actualSheets?.length || 0, expectedSheets?.length || 0)\r\n for (let i = 0; i < maxSheetCount; i += 1) {\r\n if (comparator(actualSheets?.[i] as T, expectedSheets?.[i] as T)) {\r\n const actualPatchedSheet = sheetPatcher(actualSheets?.[i],null)\r\n diffWorkBook.sheets[actualPatchedSheet] = diff<T>(\r\n utils.sheet_to_json(actualWorkBook.Sheets[actualSheets?.[i]], { header: 1 }),\r\n [],\r\n comparator\r\n )\r\n diffWorkBook.diffCount += diffWorkBook.sheets[actualPatchedSheet].diffCount\r\n const expectedPatchedSheet = sheetPatcher(null,expectedSheets?.[i])\r\n console.log({actualPatchedSheet,expectedPatchedSheet})\r\n diffWorkBook.sheets[expectedPatchedSheet] = diff<T>(\r\n [],\r\n utils.sheet_to_json(expectedWorkBook.Sheets[expectedSheets?.[i]], { header: 1 }),\r\n comparator\r\n )\r\n diffWorkBook.diffCount += diffWorkBook.sheets[expectedPatchedSheet].diffCount\r\n }\r\n else {\r\n diffWorkBook.sheets[expectedSheets[i]] = diff<T>(\r\n utils.sheet_to_json(actualWorkBook.Sheets[actualSheets?.[i]], { header: 1 }),\r\n utils.sheet_to_json(expectedWorkBook.Sheets[expectedSheets?.[i]], { header: 1 }),\r\n comparator\r\n )\r\n diffWorkBook.diffCount += diffWorkBook.sheets[expectedSheets[i]].diffCount\r\n }\r\n }\r\n return diffWorkBook\r\n}","import Formatter from \"./formatter\";\r\n\r\n/**\r\n * Represents the differences between two arrays of arrays (AOA) with a customizable format.\r\n * @template T - The type of data in the arrays of arrays.\r\n */\r\nclass DiffAOA<T> extends Array<Array<T | Array<T | null>>> {\r\n #diffCount: number = 0;\r\n\r\n /**\r\n * Formats the differences using the provided formatter.\r\n * @param formatter - The formatter object responsible for generating the formatted string.\r\n * @returns The formatted string representing the differences.\r\n */\r\n format(formatter: Formatter<T>): string {\r\n return formatter.format(this as T[][]);\r\n }\r\n\r\n /**\r\n * Gets the count of differences in the array of arrays.\r\n * @returns The number of differences.\r\n */\r\n get diffCount() {\r\n return this.#diffCount;\r\n }\r\n\r\n /**\r\n * Sets the count of differences in the array of arrays.\r\n * @param value - The number of differences to set.\r\n */\r\n set diffCount(value: number) {\r\n this.#diffCount = value;\r\n }\r\n}\r\n\r\nexport default DiffAOA;","import DiffAOA from \"./DIffAOA\";\r\nimport { WorkbookFormatter } from \"./formatter/workbook\";\r\n/**\r\n * Represents a workbook that stores the differences between two workbooks.\r\n * @template T - The type of data stored in the workbook.\r\n */\r\nclass DiffWorkBook<T> {\r\n sheets: { [sheet: string]: DiffAOA<T>; };\r\n #diffCount: number = 0;\r\n\r\n constructor() {\r\n this.sheets = {};\r\n }\r\n\r\n /**\r\n * Formats the workbook using the specified formatter.\r\n * @param formatter - The formatter to use for formatting the workbook.\r\n * @returns The formatted workbook.\r\n */\r\n format(formatter: WorkbookFormatter<T>) {\r\n return formatter.format(this.sheets);\r\n }\r\n\r\n /**\r\n * Gets the number of differences in the workbook.\r\n */\r\n get diffCount() {\r\n return this.#diffCount;\r\n }\r\n\r\n /**\r\n * Sets the number of differences in the workbook.\r\n */\r\n set diffCount(value: number) {\r\n this.#diffCount = value;\r\n }\r\n}\r\n\r\nexport default DiffWorkBook;"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAiB;AACjB,gBAAe;;;ACDf;AAMA,IAAM,UAAN,cAAyB,MAAkC;AAAA,EAA3D;AAAA;AACI,mCAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOrB,OAAO,WAAiC;AACpC,WAAO,UAAU,OAAO,IAAa;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,YAAY;AACZ,WAAO,mBAAK;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,UAAU,OAAe;AACzB,uBAAK,YAAa;AAAA,EACtB;AACJ;AA1BI;AA4BJ,IAAO,kBAAQ;;;ADhCf,kBAAsC;;;AEHtC,IAAAA;AAMA,IAAM,eAAN,MAAsB;AAAA,EAIlB,cAAc;AAFd,uBAAAA,aAAqB;AAGjB,SAAK,SAAS,CAAC;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,WAAiC;AACpC,WAAO,UAAU,OAAO,KAAK,MAAM;AAAA,EACvC;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,YAAY;AACZ,WAAO,mBAAKA;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,UAAU,OAAe;AACzB,uBAAKA,aAAa;AAAA,EACtB;AACJ;AA5BIA,cAAA;AA8BJ,IAAO,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||
| function diffWorkBook(actualWorkBook, expectedWorkBook, comparator = (actual, expected) => actual !== expected) { | ||
| function diffWorkBook(actualWorkBook, expectedWorkBook, comparator = (actual, expected) => actual !== expected, sheetPatcher = (actual, expected) => { | ||
| let patchedString = ""; | ||
| if (actual) | ||
| patchedString += `(-)(${actual})`; | ||
| if (actual && expected) | ||
| patchedString += " "; | ||
| if (expected) | ||
| patchedString += `(+)(${expected})`; | ||
| return patchedString; | ||
| }) { | ||
| const diffWorkBook2 = new DiffWorkBook_default(); | ||
@@ -125,3 +134,4 @@ const actualSheets = actualWorkBook.SheetNames; | ||
| if (comparator(actualSheets == null ? void 0 : actualSheets[i], expectedSheets == null ? void 0 : expectedSheets[i])) { | ||
| diffWorkBook2.sheets[actualSheets == null ? void 0 : actualSheets[i]] = diff( | ||
| 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 }), | ||
@@ -131,4 +141,6 @@ [], | ||
| ); | ||
| diffWorkBook2.diffCount += diffWorkBook2.sheets[actualSheets == null ? void 0 : actualSheets[i]].diffCount; | ||
| diffWorkBook2.sheets[expectedSheets == null ? void 0 : expectedSheets[i]] = diff( | ||
| diffWorkBook2.diffCount += diffWorkBook2.sheets[actualPatchedSheet].diffCount; | ||
| const expectedPatchedSheet = sheetPatcher(null, expectedSheets == null ? void 0 : expectedSheets[i]); | ||
| console.log({ actualPatchedSheet, expectedPatchedSheet }); | ||
| diffWorkBook2.sheets[expectedPatchedSheet] = diff( | ||
| [], | ||
@@ -138,3 +150,3 @@ utils.sheet_to_json(expectedWorkBook.Sheets[expectedSheets == null ? void 0 : expectedSheets[i]], { header: 1 }), | ||
| ); | ||
| diffWorkBook2.diffCount += diffWorkBook2.sheets[expectedSheets == null ? void 0 : expectedSheets[i]].diffCount; | ||
| diffWorkBook2.diffCount += diffWorkBook2.sheets[expectedPatchedSheet].diffCount; | ||
| } else { | ||
@@ -141,0 +153,0 @@ diffWorkBook2.sheets[expectedSheets[i]] = diff( |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../src/index.ts","../src/DIffAOA.ts","../src/DiffWorkBook.ts"],"sourcesContent":["import Papa from \"papaparse\";\r\nimport fs from 'fs';\r\nimport DiffAOA from \"./DIffAOA\";\r\nimport xlsx, { WorkBook, utils } from 'xlsx';\r\nimport DiffWorkBook from \"./DiffWorkBook\";\r\n\r\n/**\r\n * Reads a CSV file and parses its content into a two-dimensional array.\r\n * @param filePath - The path to the CSV file.\r\n * @returns A two-dimensional array representing the parsed CSV data.\r\n * @template T - The type of data in the CSV file.\r\n */\r\nexport function readCSV<T>(filePath: string): T[][] {\r\n return Papa.parse<T[]>(fs.readFileSync(filePath, \"utf8\")).data || [];\r\n}\r\n/**\r\n * Reads a workbook from the specified file path.\r\n * @param filePath - The path to the workbook file.\r\n * @returns The parsed workbook object.\r\n */\r\nexport function readWorkBook(filePath: string) {\r\n return xlsx.readFile(filePath)\r\n}\r\n\r\n/**\r\n * Compares two arrays of arrays (AOA) and identifies differences based on a custom comparator function.\r\n * @param actualAOA - The actual array of arrays.\r\n * @param expectedAOA - The expected array of arrays for comparison.\r\n * @param comparator - A function to compare individual elements in the arrays (default is a simple !== comparison).\r\n * @returns An instance of DiffAOA<T> representing the differences between the two arrays of arrays.\r\n * @template T - The type of data in the arrays of arrays.\r\n */\r\nexport function diff<T>(\r\n actualAOA: T[][],\r\n expectedAOA: T[][],\r\n comparator: (actual: T, expected: T) => boolean = (actual, expected) => actual !== expected\r\n): DiffAOA<T> {\r\n const diffAOA: DiffAOA<T> = new DiffAOA();\r\n const maxRowCount = Math.max(actualAOA.length, expectedAOA.length);\r\n\r\n for (let i = 0; i < maxRowCount; i += 1) {\r\n const diffRow: (T | (T | null)[])[] = [];\r\n const maxColCount = Math.max(actualAOA?.[i]?.length || 0, expectedAOA?.[i]?.length || 0);\r\n\r\n for (let j = 0; j < maxColCount; j += 1) {\r\n if (comparator(actualAOA?.[i]?.[j], expectedAOA?.[i]?.[j])) {\r\n diffAOA.diffCount += 1;\r\n diffRow[j] = [actualAOA?.[i]?.[j], expectedAOA?.[i]?.[j]];\r\n } else {\r\n diffRow[j] = expectedAOA?.[i]?.[j];\r\n }\r\n }\r\n\r\n diffAOA.push(diffRow);\r\n }\r\n return diffAOA;\r\n}\r\n\r\n/**\r\n * Calculates the difference between two workbooks.\r\n * @template T - The type of the elements in the workbooks.\r\n * @param {WorkBook} actualWorkBook - The actual workbook.\r\n * @param {WorkBook} expectedWorkBook - The expected workbook.\r\n * @param {(actual: T, expected: T) => boolean} [comparator] - The comparator function to compare elements in the workbooks. Defaults to a function that checks for strict inequality.\r\n * @returns {DiffWorkBook<T>} - The diff workbook containing the differences between the two workbooks.\r\n */\r\nexport function diffWorkBook<T>(\r\n actualWorkBook: WorkBook,\r\n expectedWorkBook: WorkBook,\r\n comparator: (actual: T, expected: T) => boolean = (actual, expected) => actual !== expected\r\n) {\r\n const diffWorkBook: DiffWorkBook<T> = new DiffWorkBook()\r\n const actualSheets = actualWorkBook.SheetNames\r\n const expectedSheets = expectedWorkBook.SheetNames\r\n const maxSheetCount = Math.max(actualSheets?.length || 0, expectedSheets?.length || 0)\r\n for (let i = 0; i < maxSheetCount; i += 1) {\r\n if (comparator(actualSheets?.[i] as T, expectedSheets?.[i] as T)) {\r\n diffWorkBook.sheets[actualSheets?.[i]] = diff<T>(\r\n utils.sheet_to_json(actualWorkBook.Sheets[actualSheets?.[i]], { header: 1 }),\r\n [],\r\n comparator\r\n )\r\n diffWorkBook.diffCount += diffWorkBook.sheets[actualSheets?.[i]].diffCount\r\n diffWorkBook.sheets[expectedSheets?.[i]] = diff<T>(\r\n [],\r\n utils.sheet_to_json(expectedWorkBook.Sheets[expectedSheets?.[i]], { header: 1 }),\r\n comparator\r\n )\r\n diffWorkBook.diffCount += diffWorkBook.sheets[expectedSheets?.[i]].diffCount\r\n }\r\n else {\r\n diffWorkBook.sheets[expectedSheets[i]] = diff<T>(\r\n utils.sheet_to_json(actualWorkBook.Sheets[actualSheets?.[i]], { header: 1 }),\r\n utils.sheet_to_json(expectedWorkBook.Sheets[expectedSheets?.[i]], { header: 1 }),\r\n comparator\r\n )\r\n diffWorkBook.diffCount += diffWorkBook.sheets[expectedSheets[i]].diffCount\r\n }\r\n }\r\n return diffWorkBook\r\n}","import Formatter from \"./formatter\";\r\n\r\n/**\r\n * Represents the differences between two arrays of arrays (AOA) with a customizable format.\r\n * @template T - The type of data in the arrays of arrays.\r\n */\r\nclass DiffAOA<T> extends Array<Array<T | Array<T | null>>> {\r\n #diffCount: number = 0;\r\n\r\n /**\r\n * Formats the differences using the provided formatter.\r\n * @param formatter - The formatter object responsible for generating the formatted string.\r\n * @returns The formatted string representing the differences.\r\n */\r\n format(formatter: Formatter<T>): string {\r\n return formatter.format(this as T[][]);\r\n }\r\n\r\n /**\r\n * Gets the count of differences in the array of arrays.\r\n * @returns The number of differences.\r\n */\r\n get diffCount() {\r\n return this.#diffCount;\r\n }\r\n\r\n /**\r\n * Sets the count of differences in the array of arrays.\r\n * @param value - The number of differences to set.\r\n */\r\n set diffCount(value: number) {\r\n this.#diffCount = value;\r\n }\r\n}\r\n\r\nexport default DiffAOA;","import DiffAOA from \"./DIffAOA\";\r\nimport { WorkbookFormatter } from \"./formatter/workbook\";\r\n/**\r\n * Represents a workbook that stores the differences between two workbooks.\r\n * @template T - The type of data stored in the workbook.\r\n */\r\nclass DiffWorkBook<T> {\r\n sheets: { [sheet: string]: DiffAOA<T>; };\r\n #diffCount: number = 0;\r\n\r\n constructor() {\r\n this.sheets = {};\r\n }\r\n\r\n /**\r\n * Formats the workbook using the specified formatter.\r\n * @param formatter - The formatter to use for formatting the workbook.\r\n * @returns The formatted workbook.\r\n */\r\n format(formatter: WorkbookFormatter<T>) {\r\n return formatter.format(this.sheets);\r\n }\r\n\r\n /**\r\n * Gets the number of differences in the workbook.\r\n */\r\n get diffCount() {\r\n return this.#diffCount;\r\n }\r\n\r\n /**\r\n * Sets the number of differences in the workbook.\r\n */\r\n set diffCount(value: number) {\r\n this.#diffCount = value;\r\n }\r\n}\r\n\r\nexport default DiffWorkBook;"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,OAAO,UAAU;AACjB,OAAO,QAAQ;;;ACDf;AAMA,IAAM,UAAN,cAAyB,MAAkC;AAAA,EAA3D;AAAA;AACI,mCAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOrB,OAAO,WAAiC;AACpC,WAAO,UAAU,OAAO,IAAa;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,YAAY;AACZ,WAAO,mBAAK;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,UAAU,OAAe;AACzB,uBAAK,YAAa;AAAA,EACtB;AACJ;AA1BI;AA4BJ,IAAO,kBAAQ;;;ADhCf,OAAO,QAAkB,aAAa;;;AEHtC,IAAAA;AAMA,IAAM,eAAN,MAAsB;AAAA,EAIlB,cAAc;AAFd,uBAAAA,aAAqB;AAGjB,SAAK,SAAS,CAAC;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,WAAiC;AACpC,WAAO,UAAU,OAAO,KAAK,MAAM;AAAA,EACvC;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,YAAY;AACZ,WAAO,mBAAKA;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,UAAU,OAAe;AACzB,uBAAKA,aAAa;AAAA,EACtB;AACJ;AA5BIA,cAAA;AA8BJ,IAAO,uBAAQ;;;AF1BR,SAAS,QAAW,UAAyB;AAChD,SAAO,KAAK,MAAW,GAAG,aAAa,UAAU,MAAM,CAAC,EAAE,QAAQ,CAAC;AACvE;AAMO,SAAS,aAAa,UAAkB;AAC3C,SAAO,KAAK,SAAS,QAAQ;AACjC;AA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AaA,cAAa,OAAO,6CAAe,EAAE,EAAE;AACjE,MAAAA,cAAa,OAAO,iDAAiB,EAAE,IAAI;AAAA,QACvC,CAAC;AAAA,QACD,MAAM,cAAc,iBAAiB,OAAO,iDAAiB,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC;AAAA,QAC/E;AAAA,MACJ;AACA,MAAAA,cAAa,aAAaA,cAAa,OAAO,iDAAiB,EAAE,EAAE;AAAA,IACvE,OACK;AACD,MAAAA,cAAa,OAAO,eAAe,CAAC,CAAC,IAAI;AAAA,QACrC,MAAM,cAAc,eAAe,OAAO,6CAAe,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC;AAAA,QAC3E,MAAM,cAAc,iBAAiB,OAAO,iDAAiB,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC;AAAA,QAC/E;AAAA,MACJ;AACA,MAAAA,cAAa,aAAaA,cAAa,OAAO,eAAe,CAAC,CAAC,EAAE;AAAA,IACrE;AAAA,EACJ;AACA,SAAOA;AACX;","names":["_diffCount","diffWorkBook"]} | ||
| {"version":3,"sources":["../src/index.ts","../src/DIffAOA.ts","../src/DiffWorkBook.ts"],"sourcesContent":["import Papa from \"papaparse\";\r\nimport fs from 'fs';\r\nimport DiffAOA from \"./DIffAOA\";\r\nimport xlsx, { WorkBook, utils } from 'xlsx';\r\nimport DiffWorkBook from \"./DiffWorkBook\";\r\n\r\n/**\r\n * Reads a CSV file and parses its content into a two-dimensional array.\r\n * @param filePath - The path to the CSV file.\r\n * @returns A two-dimensional array representing the parsed CSV data.\r\n * @template T - The type of data in the CSV file.\r\n */\r\nexport function readCSV<T>(filePath: string): T[][] {\r\n return Papa.parse<T[]>(fs.readFileSync(filePath, \"utf8\")).data || [];\r\n}\r\n/**\r\n * Reads a workbook from the specified file path.\r\n * @param filePath - The path to the workbook file.\r\n * @returns The parsed workbook object.\r\n */\r\nexport function readWorkBook(filePath: string) {\r\n return xlsx.readFile(filePath)\r\n}\r\n\r\n/**\r\n * Compares two arrays of arrays (AOA) and identifies differences based on a custom comparator function.\r\n * @param actualAOA - The actual array of arrays.\r\n * @param expectedAOA - The expected array of arrays for comparison.\r\n * @param comparator - A function to compare individual elements in the arrays (default is a simple !== comparison).\r\n * @returns An instance of DiffAOA<T> representing the differences between the two arrays of arrays.\r\n * @template T - The type of data in the arrays of arrays.\r\n */\r\nexport function diff<T>(\r\n actualAOA: T[][],\r\n expectedAOA: T[][],\r\n comparator: (actual: T, expected: T) => boolean = (actual, expected) => actual !== expected\r\n): DiffAOA<T> {\r\n const diffAOA: DiffAOA<T> = new DiffAOA();\r\n const maxRowCount = Math.max(actualAOA.length, expectedAOA.length);\r\n\r\n for (let i = 0; i < maxRowCount; i += 1) {\r\n const diffRow: (T | (T | null)[])[] = [];\r\n const maxColCount = Math.max(actualAOA?.[i]?.length || 0, expectedAOA?.[i]?.length || 0);\r\n\r\n for (let j = 0; j < maxColCount; j += 1) {\r\n if (comparator(actualAOA?.[i]?.[j], expectedAOA?.[i]?.[j])) {\r\n diffAOA.diffCount += 1;\r\n diffRow[j] = [actualAOA?.[i]?.[j], expectedAOA?.[i]?.[j]];\r\n } else {\r\n diffRow[j] = expectedAOA?.[i]?.[j];\r\n }\r\n }\r\n\r\n diffAOA.push(diffRow);\r\n }\r\n return diffAOA;\r\n}\r\n\r\n/**\r\n * Calculates the difference between two workbooks.\r\n * @template T - The type of the elements in the workbooks.\r\n * @param actualWorkBook - The actual workbook.\r\n * @param expectedWorkBook - The expected workbook.\r\n * @param comparator - The comparator function to compare elements in the workbooks. Defaults to a function that checks for inequality.\r\n * @param sheetPatcher - The function to generate a patched string for sheet names. Defaults to a function that generates a string with added and removed sheet names.\r\n * @returns - The diff workbook containing the differences between the two workbooks.\r\n */\r\nexport function diffWorkBook<T>(\r\n actualWorkBook: WorkBook,\r\n expectedWorkBook: WorkBook,\r\n comparator: (actual: T, expected: T) => boolean = (actual, expected) => actual !== expected,\r\n sheetPatcher = (actual: string | null, expected: string | null) => {\r\n let patchedString = \"\"\r\n if (actual)\r\n patchedString += `(-)(${actual})`\r\n if (actual && expected)\r\n patchedString += \" \"\r\n if (expected)\r\n patchedString += `(+)(${expected})`\r\n return patchedString\r\n }\r\n) {\r\n const diffWorkBook: DiffWorkBook<T> = new DiffWorkBook()\r\n const actualSheets = actualWorkBook.SheetNames\r\n const expectedSheets = expectedWorkBook.SheetNames\r\n const maxSheetCount = Math.max(actualSheets?.length || 0, expectedSheets?.length || 0)\r\n for (let i = 0; i < maxSheetCount; i += 1) {\r\n if (comparator(actualSheets?.[i] as T, expectedSheets?.[i] as T)) {\r\n const actualPatchedSheet = sheetPatcher(actualSheets?.[i],null)\r\n diffWorkBook.sheets[actualPatchedSheet] = diff<T>(\r\n utils.sheet_to_json(actualWorkBook.Sheets[actualSheets?.[i]], { header: 1 }),\r\n [],\r\n comparator\r\n )\r\n diffWorkBook.diffCount += diffWorkBook.sheets[actualPatchedSheet].diffCount\r\n const expectedPatchedSheet = sheetPatcher(null,expectedSheets?.[i])\r\n console.log({actualPatchedSheet,expectedPatchedSheet})\r\n diffWorkBook.sheets[expectedPatchedSheet] = diff<T>(\r\n [],\r\n utils.sheet_to_json(expectedWorkBook.Sheets[expectedSheets?.[i]], { header: 1 }),\r\n comparator\r\n )\r\n diffWorkBook.diffCount += diffWorkBook.sheets[expectedPatchedSheet].diffCount\r\n }\r\n else {\r\n diffWorkBook.sheets[expectedSheets[i]] = diff<T>(\r\n utils.sheet_to_json(actualWorkBook.Sheets[actualSheets?.[i]], { header: 1 }),\r\n utils.sheet_to_json(expectedWorkBook.Sheets[expectedSheets?.[i]], { header: 1 }),\r\n comparator\r\n )\r\n diffWorkBook.diffCount += diffWorkBook.sheets[expectedSheets[i]].diffCount\r\n }\r\n }\r\n return diffWorkBook\r\n}","import Formatter from \"./formatter\";\r\n\r\n/**\r\n * Represents the differences between two arrays of arrays (AOA) with a customizable format.\r\n * @template T - The type of data in the arrays of arrays.\r\n */\r\nclass DiffAOA<T> extends Array<Array<T | Array<T | null>>> {\r\n #diffCount: number = 0;\r\n\r\n /**\r\n * Formats the differences using the provided formatter.\r\n * @param formatter - The formatter object responsible for generating the formatted string.\r\n * @returns The formatted string representing the differences.\r\n */\r\n format(formatter: Formatter<T>): string {\r\n return formatter.format(this as T[][]);\r\n }\r\n\r\n /**\r\n * Gets the count of differences in the array of arrays.\r\n * @returns The number of differences.\r\n */\r\n get diffCount() {\r\n return this.#diffCount;\r\n }\r\n\r\n /**\r\n * Sets the count of differences in the array of arrays.\r\n * @param value - The number of differences to set.\r\n */\r\n set diffCount(value: number) {\r\n this.#diffCount = value;\r\n }\r\n}\r\n\r\nexport default DiffAOA;","import DiffAOA from \"./DIffAOA\";\r\nimport { WorkbookFormatter } from \"./formatter/workbook\";\r\n/**\r\n * Represents a workbook that stores the differences between two workbooks.\r\n * @template T - The type of data stored in the workbook.\r\n */\r\nclass DiffWorkBook<T> {\r\n sheets: { [sheet: string]: DiffAOA<T>; };\r\n #diffCount: number = 0;\r\n\r\n constructor() {\r\n this.sheets = {};\r\n }\r\n\r\n /**\r\n * Formats the workbook using the specified formatter.\r\n * @param formatter - The formatter to use for formatting the workbook.\r\n * @returns The formatted workbook.\r\n */\r\n format(formatter: WorkbookFormatter<T>) {\r\n return formatter.format(this.sheets);\r\n }\r\n\r\n /**\r\n * Gets the number of differences in the workbook.\r\n */\r\n get diffCount() {\r\n return this.#diffCount;\r\n }\r\n\r\n /**\r\n * Sets the number of differences in the workbook.\r\n */\r\n set diffCount(value: number) {\r\n this.#diffCount = value;\r\n }\r\n}\r\n\r\nexport default DiffWorkBook;"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,OAAO,UAAU;AACjB,OAAO,QAAQ;;;ACDf;AAMA,IAAM,UAAN,cAAyB,MAAkC;AAAA,EAA3D;AAAA;AACI,mCAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOrB,OAAO,WAAiC;AACpC,WAAO,UAAU,OAAO,IAAa;AAAA,EACzC;AAAA;AAAA;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M,cAAc,iBAAiB,OAAO,iDAAiB,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC;AAAA,QAC/E;AAAA,MACJ;AACA,MAAAA,cAAa,aAAaA,cAAa,OAAO,oBAAoB,EAAE;AAAA,IACxE,OACK;AACD,MAAAA,cAAa,OAAO,eAAe,CAAC,CAAC,IAAI;AAAA,QACrC,MAAM,cAAc,eAAe,OAAO,6CAAe,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC;AAAA,QAC3E,MAAM,cAAc,iBAAiB,OAAO,iDAAiB,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC;AAAA,QAC/E;AAAA,MACJ;AACA,MAAAA,cAAa,aAAaA,cAAa,OAAO,eAAe,CAAC,CAAC,EAAE;AAAA,IACrE;AAAA,EACJ;AACA,SAAOA;AACX;","names":["_diffCount","diffWorkBook"]} |
+1
-1
| { | ||
| "name": "spread-diff-patch", | ||
| "version": "1.1.2", | ||
| "version": "1.1.3", | ||
| "description": "Diff & patch SpreadSheet files", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
91461
3.42%1013
2.53%