@unblocks/utils
Version:
Collection of utility functions and classes
110 lines (106 loc) • 3.05 kB
JavaScript
'use client'
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
ModeManager: () => ModeManager,
Sizer: () => Sizer
});
module.exports = __toCommonJS(index_exports);
// 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)];
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ModeManager,
Sizer
});