UNPKG

@piyawasin/byte-flags

Version:

A lightweight, type-safe utility library for efficiently storing and managing boolean flags in compact numeric values (8, 16, or 32 bits). Perfect for permission systems, feature flags, status tracking, and any scenario requiring compact boolean storage w

118 lines (117 loc) 4.71 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ShortFlags = void 0; exports.createShortFlags = createShortFlags; var BaseFlags_1 = require("./BaseFlags"); /** * Efficiently stores and manages up to 16 boolean flags in a single short (16-bit value). * * Ideal for permission systems, feature flags, or status tracking where compact * serialization is needed and more than 8 flags are required. * * @example * // Recommended usage with type safety: * const flags = createShortFlags('read', 'write', 'execute', 'admin', 'guest'); * flags.admin = true; // TypeScript knows this is a boolean * * @see {@link createShortFlags} for better TypeScript support */ var ShortFlags = /** @class */ (function (_super) { __extends(ShortFlags, _super); /** * Create a new ShortFlags instance with the specified flag names * @param flagNames Names of flags to initialize (1-16 flags) * @throws Error if more than 16 flags are provided or if flag names are invalid */ function ShortFlags() { var flagNames = []; for (var _i = 0; _i < arguments.length; _i++) { flagNames[_i] = arguments[_i]; } var _this = this; if (flagNames.length > ShortFlags._MAX_FLAGS) { throw new Error("Cannot have more than ".concat(ShortFlags._MAX_FLAGS, " flags")); } _this = _super.apply(this, flagNames) || this; // Maximum number of flags that can be stored in a single short (16 bits) _this.MAX_FLAGS = ShortFlags._MAX_FLAGS; // Maximum short value (2^16 - 1) _this.MAX_VALUE = ShortFlags._MAX_VALUE; return _this; } /** * Create a ShortFlags instance from a JSON string * @param json JSON string or object from toJSON() * @returns New ShortFlags instance * @throws Error if JSON is invalid or flags don't match */ ShortFlags.fromJSON = function (json) { var obj = typeof json === "string" ? JSON.parse(json) : json; var instance = new (ShortFlags.bind.apply(ShortFlags, __spreadArray([void 0], Object.keys(obj), false)))(); return instance.setFlags(obj); }; /** * Convert the flags to a short value (0-65535) * @returns Short representation of all flags */ ShortFlags.prototype.toShort = function () { return this.toValue(); }; /** * Load flags from a short value * @param s Short value to load (0-65535) * @returns This instance for chaining * @throws Error if the short value is invalid */ ShortFlags.prototype.fromShort = function (s) { return this.fromValue(s); }; // For use in constructor ShortFlags._MAX_FLAGS = 16; ShortFlags._MAX_VALUE = 0xffff; return ShortFlags; }(BaseFlags_1.BaseFlags)); exports.ShortFlags = ShortFlags; /** * Creates a type-safe ShortFlags instance with the specified flag names * @example * // Basic usage * const flags = createShortFlags('read', 'write', 'admin', 'guest'); * flags.admin = true; // TypeScript knows this is a boolean * * // With explicit type * type AppFlags = 'darkMode' | 'notifications' | 'analytics'; * const features = createShortFlags<AppFlags>('darkMode', 'notifications', 'analytics'); * features.darkMode = true; // OK */ function createShortFlags() { var flagNames = []; for (var _i = 0; _i < arguments.length; _i++) { flagNames[_i] = arguments[_i]; } return new (ShortFlags.bind.apply(ShortFlags, __spreadArray([void 0], flagNames, false)))(); }