bakana
Version:
Backend for kana's single-cell analyses. This supports single or multiple samples, execution in Node.js or the browser, in-memory caching of results for iterative analyses, and serialization to/from file for redistribution.
57 lines (51 loc) • 1.43 kB
JavaScript
export class SimpleFile {
#mode;
#buffer;
#file;
#name;
constructor(x, { name = null } = {}) {
if (x instanceof File) {
this.#mode = "file";
this.#file = x;
if (name === null) {
name = x.name;
}
this.#name = name;
} else if (x instanceof Uint8Array) {
this.#mode = "buffer";
this.#buffer = x;
if (name === null) {
throw new Error("'name' must be provided for Uint8Array inputs in SimpleFile constructor");
}
this.#name = name;
} else {
throw new Error("unknown type '" + typeof(x) + "' for SimpleFile constructor");
}
}
buffer({ copy = false } = {}) {
if (this.#mode == "file") {
let reader = new FileReaderSync();
let b = reader.readAsArrayBuffer(this.#file);
return new Uint8Array(b);
} else {
if (copy) {
return this.#buffer.slice();
} else {
return this.#buffer;
}
}
}
size() {
if (this.#mode == "file") {
return this.#file.size;
} else {
return this.#buffer.length;
}
}
name() {
return this.#name;
}
content({ copy = false } = {}) {
return this.buffer({ copy: copy });
}
}