UNPKG

screening

Version:

Tools for numbering screening plate

96 lines 3 kB
import { numberToPosition } from "./utils/numberToPosition.js"; import { positionToNumber } from "./utils/positionToNumber.js"; export class Plate { width; height; size; wells = []; constructor(options = {}) { this.width = options.width ?? 12; this.height = options.height ?? (this.width / 3) * 2; this.size = this.width * this.height; this.initialize(); } getWells() { return this.wells; } select(range) { if (range) { // reset all for (const well of this.wells) { well.selected = false; } const reg = /(\d+)(-(\d+))?,?/g; let m; while ((m = reg.exec(range))) { const from = Number(m[1]); const to = m[3] ? Number(m[3]) : undefined; if (to && from <= to) { for (let i = from; i <= to; i++) { const well = this.wells[i - 1]; if (!isEmpty(well.info)) well.selected = true; } } else { const well = this.wells[from - 1]; if (!isEmpty(well.info)) well.selected = true; } } } else { // auto-select wells with info for (const well of this.wells) { well.selected = !isEmpty(well.info); } } } getArrayElement(index) { return this.wells[index]; } updateColor() { for (const well of this.wells) { if (isEmpty(well.info)) { // empty well = white well.info.color = 'white'; } else if (well.selected) { // selected = strong highlight well.info.color = 'rgba(144, 238, 144, 1)'; } else { // unselected = faded highlight well.info.color = 'rgba(144, 238, 144, 0.3)'; } } } getByPosition(position) { const index = positionToNumber(position, this.width) - 1; return this.wells[index]; } getByNumber(number) { return this.wells[number - 1]; } initialize() { this.wells = new Array(this.size); for (let row = 0; row < this.height; row++) { for (let column = 0; column < this.width; column++) { const i = row * this.width + column; this.wells[i] = { number: i + 1, position: numberToPosition(i + 1, this.width), info: {}, }; } } } } // ----------- Helpers ----------- function isEmpty(object) { return (object && typeof object === 'object' && !Array.isArray(object) && Object.keys(object).length === 0); } //# sourceMappingURL=plate.js.map