From 46530972250b93a46d30df740eaca9aacffb3808 Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Wed, 24 Jan 2024 15:30:22 +0500 Subject: [PATCH] chore: move utils to separate file --- client/src/app/xlmap/utils/file.utils.ts | 31 ++++++++++++++++++++++++ client/src/app/xlmap/xlmap.component.ts | 22 +++-------------- 2 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 client/src/app/xlmap/utils/file.utils.ts diff --git a/client/src/app/xlmap/utils/file.utils.ts b/client/src/app/xlmap/utils/file.utils.ts new file mode 100644 index 0000000..a7daed7 --- /dev/null +++ b/client/src/app/xlmap/utils/file.utils.ts @@ -0,0 +1,31 @@ +export const blobToFile = (blob: Blob, fileName: string): File => { + const file = new File([blob], fileName, { + lastModified: new Date().getTime() + }) + return file +} + +/** + * Convert an array of bytes (Uint8Array) to a binary string. + * @param {Uint8Array} res - The array of bytes to convert. + * @returns {string} The binary string representation of the array of bytes. + */ +export const byteArrayToBinaryString = (res: Uint8Array): string => { + // Create a Uint8Array from the input array (if it's not already) + const bytes = new Uint8Array(res) + + // Initialize an empty string to store the binary representation + let binary = '' + + // Get the length of the byte array + const length = bytes.byteLength + + // Iterate through each byte in the array + for (let i = 0; i < length; i++) { + // Convert each byte to its binary representation and append to the string + binary += String.fromCharCode(bytes[i]) + } + + // Return the binary string + return binary +} diff --git a/client/src/app/xlmap/xlmap.component.ts b/client/src/app/xlmap/xlmap.component.ts index 6bd1281..3d490a4 100644 --- a/client/src/app/xlmap/xlmap.component.ts +++ b/client/src/app/xlmap/xlmap.component.ts @@ -21,6 +21,7 @@ import { SasStoreService } from '../services' import { getCellAddress, getFinishingCell } from './utils/xl.utils' +import { blobToFile, byteArrayToBinaryString } from './utils/file.utils' interface XLMapRule { XLMAP_ID: string @@ -141,13 +142,6 @@ export class XLMapComponent implements AfterContentInit, AfterViewInit, OnInit { private sasService: SasService ) {} - private blobToFile(theBlob: Blob, fileName: string): File { - const b: any = theBlob - b.lastModifiedDate = new Date() - b.name = fileName - return b as File - } - public xlmapOnClick(xlmap: XLMapListItem) { if (xlmap.id !== this.selectedXLMap?.id) { this.selectedXLMap = xlmap @@ -218,7 +212,7 @@ export class XLMapComponent implements AfterContentInit, AfterViewInit, OnInit { const reader = new FileReader() reader.onload = async (theFile: any) => { /* read workbook */ - const bstr = this.toBstr(theFile.target.result) + const bstr = byteArrayToBinaryString(theFile.target.result) let wb: XLSX.WorkBook | undefined = undefined const xlsxOptions: XLSX.ParsingOptions = { @@ -271,16 +265,6 @@ export class XLMapComponent implements AfterContentInit, AfterViewInit, OnInit { } } - public toBstr(res: any) { - const bytes = new Uint8Array(res) - let binary = '' - const length = bytes.byteLength - for (let i = 0; i < length; i++) { - binary += String.fromCharCode(bytes[i]) - } - return binary - } - public discardExtractedData() { this.isLoading = false this.isLoadingDesc = '' @@ -330,7 +314,7 @@ export class XLMapComponent implements AfterContentInit, AfterViewInit, OnInit { .join('\n') const blob = new Blob([csvContent], { type: 'application/csv' }) - const file: File = this.blobToFile(blob, this.filename + '.csv') + const file: File = blobToFile(blob, this.filename + '.csv') filesToUpload.push({ file: file,