Update fnTools.ts

This commit is contained in:
Andros Fenollosa 2022-02-10 09:51:45 +01:00 committed by GitHub
parent 1f9581e4c1
commit c35eaa5c5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -225,3 +225,30 @@ export async function encodeFileToText(file: File): Promise<string> {
return text; return text;
}); });
} }
/**
* Returns an Array from the union of 2 Arrays of Files avoiding repetitions.
* @param {Array<File>} newFiles
* @param {Array<File>} currentListFiles
* @return Promise<File[]>
*/
export async function getUniqFiles(newFiles: Array<File>, currentListFiles: Array<File>): Promise<File[]> {
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);
});
}
);
});
}