diff --git a/src/fnTools.ts b/src/fnTools.ts index d643da3..b4a60cf 100644 --- a/src/fnTools.ts +++ b/src/fnTools.ts @@ -225,3 +225,30 @@ export async function encodeFileToText(file: File): Promise { return text; }); } + +/** + * Returns an Array from the union of 2 Arrays of Files avoiding repetitions. + * @param {Array} newFiles + * @param {Array} currentListFiles + * @return Promise + */ +export async function getUniqFiles(newFiles: Array, currentListFiles: Array): Promise { + return new Promise((resolve) => { + Promise.all(newFiles.map((inputFile) => encodeFileToText(inputFile))).then( + (inputFilesText) => { + // Check all the files to save + Promise.all( + currentListFiles.map((savedFile) => encodeFileToText(savedFile)) + ).then((savedFilesText) => { + let newFileList = currentListFiles; + inputFilesText.forEach((inputFileText, index) => { + if (!savedFilesText.includes(inputFileText)) { + newFileList = newFileList.concat(newFiles[index]); + } + }); + resolve(newFileList); + }); + } + ); + }); +}