Home > Enterprise >  Map the elements of an array and add the times that javascript does
Map the elements of an array and add the times that javascript does

Time:07-13

I have an array with several elements, this array carries inside the info of a buffer that paints the info of a pdf. Imagine that within the matrix there are ten pdf files something like this:

[{
   correlative: "G-22-1-06",
   content: <Buffer 25 50 44 46 2d 31 2e 34 0a 25 d3 eb e9 e1 0a 31 20 30 20 6f 62 6a 0a 3c 3c 2f 43 72 65 61 74 6f 72 20 28 43 68 72 6f 72 6d 7 6d 6 509 64 ... 277154 more bytes>,
name_Patient: "NamePatient"
},
{
  correlative: "G-22-1-07",
   content: <Buffer 25 50 44 46 2d 31 2e 34 0a 25 d3 eb e9 e1 0a 31 20 30 20 6f 62 6a 0a 3c 3c 2f 43 72 65 61 74 6f 72 20 28 43 68 72 6f 72 6d 7 6d 6 509 64 ... 275969 more bytes>,
name_Patient: "NamePatient"
}
]

And if the matrix exceeds a limit in MB that matrix is ​​divided, let's say that inside the matrix there are 20 pdf files, but since they access the limit of MB they are divided two by two (it is an example) how could I obtain the times that it was divided ? That is how many times it had to be divided to complete the 20 files

// Get the total in MB of the files in the array

const totalSize: number = this.getSizeFromAttachments(attachments);

//Divide the MB limit by a set total limit

const chunkSplit = Math.floor(isNaN(totalSize) ? 1 : totalSize / this.LIMIT_ATTACHMENTS)   1;


//Split the matrix if it is greater than the established limit

const attachmentsChunk: any[][] = _.chunk(attachments, chunkSplit);

// accessing the split matrix info
 attachmentsChunk?.map(async (attachment: EmailAttachment[], index) => {
//more action
}

CodePudding user response:

Let us assume that the file limit is 5MB

const FILE_LIMIT = 5;

and the file size is 26 for example. so it should be divided 6 times

[5, 5, 5, 5, 5, 1] exact sizes of splitted parts

const fileSize = 26;

you can create this function that calculates the number of splitted parts

function calculateNumberOfSplits(fileSize:number, maxSize:number):number{
 return Math.ceil(fileSize/ maxSize);
}

let us test it

function calculateNumberOfSplits(fileSize, maxSize){
 return Math.ceil(fileSize/ maxSize);
}


let fileSize = 26;
let maxSize = 5;

let numOfSplits = calculateNumberOfSplits(fileSize, maxSize);

console.log(numOfSplits)

and the snippet results in 6 which is correct

  • Related