UNPKG

spread-diff-patch

Version:
1 lines 10.3 kB
{"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 (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 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 DiffAOA<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,IAAkB;AAAA,EAC9C;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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