biketrack-app/node_modules/@ethersphere/bee-js/dist/mjs/utils/collection.js

98 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-07-11 10:27:11 +02:00
var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P ? value : new P(function (resolve) {
resolve(value);
});
}
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e) {
reject(e);
}
}
function step(result) {
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { BeeArgumentError } from "./error.js";
import { fileArrayBuffer } from "./file.js";
import { isUint8Array } from "./type.js";
export function isCollection(data) {
if (!Array.isArray(data)) {
return false;
}
return data.every(entry => typeof entry === 'object' && entry.data && entry.path && isUint8Array(entry.data));
}
export function assertCollection(data) {
if (!isCollection(data)) {
throw new BeeArgumentError('invalid collection', data);
}
}
function makeFilePath(file) {
if (file.webkitRelativePath && file.webkitRelativePath !== '') {
return file.webkitRelativePath.replace(/.*?\//i, '');
}
if (file.name) {
return file.name;
}
throw new TypeError('file is not valid File object');
}
export function makeCollectionFromFileList(fileList) {
return __awaiter(this, void 0, void 0, function* () {
const collection = [];
for (let i = 0; i < fileList.length; i++) {
const file = fileList[i];
if (file) {
collection.push({
path: makeFilePath(file),
data: new Uint8Array(yield fileArrayBuffer(file))
});
}
}
return collection;
});
}
/**
* Calculate cumulative size of files
*
* @param fileList list of files to check
* @returns size in bytes
*/
export function getCollectionSize(fileList) {
let sum = 0;
for (let i = 0; i < fileList.length; i++) {
const file = fileList[i];
if (file) {
sum += file.size;
}
}
return sum;
}