UNPKG

@unblocks/utils

Version:
83 lines (80 loc) 2 kB
'use client' // src/utils/ModeManager.ts var ModeManager = class { constructor(modes) { this.modes = modes; this.modeToIndexMap = modes.reduce( (acc, mode, index) => { acc[mode] = index; return acc; }, {} ); } /** * Get the mode at the specified index. * Throws an error if the index is out of bounds. * @param index * @returns mode */ getMode(index) { if (index < 0 || index >= this.modes.length) { throw new Error(`Invalid index: ${index}`); } return this.modes[index]; } /** * Get the index of the specified mode. * Throws an error if the mode name is not found in the list of available modes. * @param mode * @returns index */ getIndex(mode) { const index = this.modeToIndexMap[mode]; if (index === void 0) { throw new Error(`Invalid mode name: ${mode}`); } return index; } }; // src/utils/Sizer.ts var Sizer = class { constructor(options) { this.options = options; } indexOf(size) { const index = this.options.indexOf(size); if (index === -1) { throw new Error(`Invalid size: ${size}`); } return index; } /** * Adjust size down * @param size size to change * @param amount offset * @param minSize minimum size allowed * @returns new size */ down(size, amount = 1, minSize) { const index = this.indexOf(size); const minIndex = typeof minSize !== "undefined" ? this.options.indexOf(minSize) : 0; return this.options[Math.max(index - amount, minIndex)]; } /** * Adjust size up * @param size size to change * @param amount offset * @param maxSize maximum size allowed * @returns new size */ up(size, amount = 1, maxSize) { const index = this.indexOf(size); const maxIndex = typeof maxSize !== "undefined" ? this.options.indexOf(maxSize) : this.options.length - 1; return this.options[Math.min(index + amount, maxIndex)]; } }; export { ModeManager, Sizer };