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.68 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.LongFlags = void 0; exports.createLongFlags = createLongFlags; var BaseFlags_1 = require("./BaseFlags"); /** * Efficiently stores and manages up to 32 boolean flags in a single long (32-bit value). * * Ideal for permission systems, feature flags, or status tracking where compact * serialization is needed and many flags are required. * * @example * // Recommended usage with type safety: * const flags = createLongFlags('read', 'write', 'execute', 'admin', 'guest'); * flags.admin = true; // TypeScript knows this is a boolean * * @see {@link createLongFlags} for better TypeScript support */ var LongFlags = /** @class */ (function (_super) { __extends(LongFlags, _super); /** * Create a new LongFlags instance with the specified flag names * @param flagNames Names of flags to initialize (1-32 flags) * @throws Error if more than 32 flags are provided or if flag names are invalid */ function LongFlags() { var flagNames = []; for (var _i = 0; _i < arguments.length; _i++) { flagNames[_i] = arguments[_i]; } var _this = this; if (flagNames.length > LongFlags._MAX_FLAGS) { throw new Error("Cannot have more than ".concat(LongFlags._MAX_FLAGS, " flags")); } _this = _super.apply(this, flagNames) || this; // Maximum number of flags that can be stored in a single long (32 bits) _this.MAX_FLAGS = LongFlags._MAX_FLAGS; // Maximum long value (2^32 - 1) _this.MAX_VALUE = LongFlags._MAX_VALUE; return _this; } /** * Create a LongFlags instance from a JSON string * @param json JSON string or object from toJSON() * @returns New LongFlags instance * @throws Error if JSON is invalid or flags don't match */ LongFlags.fromJSON = function (json) { var obj = typeof json === "string" ? JSON.parse(json) : json; var instance = new (LongFlags.bind.apply(LongFlags, __spreadArray([void 0], Object.keys(obj), false)))(); return instance.setFlags(obj); }; /** * Convert the flags to a long value (0-4294967295) * @returns Long representation of all flags */ LongFlags.prototype.toLong = function () { return this.toValue(); }; /** * Load flags from a long value * @param l Long value to load (0-4294967295) * @returns This instance for chaining * @throws Error if the long value is invalid */ LongFlags.prototype.fromLong = function (l) { return this.fromValue(l); }; // For use in constructor LongFlags._MAX_FLAGS = 32; LongFlags._MAX_VALUE = 0xffffffff; return LongFlags; }(BaseFlags_1.BaseFlags)); exports.LongFlags = LongFlags; /** * Creates a type-safe LongFlags instance with the specified flag names * @example * // Basic usage * const flags = createLongFlags('read', 'write', 'admin', 'guest'); * flags.admin = true; // TypeScript knows this is a boolean * * // With explicit type * type AppFlags = 'darkMode' | 'notifications' | 'analytics'; * const features = createLongFlags<AppFlags>('darkMode', 'notifications', 'analytics'); * features.darkMode = true; // OK */ function createLongFlags() { var flagNames = []; for (var _i = 0; _i < arguments.length; _i++) { flagNames[_i] = arguments[_i]; } return new (LongFlags.bind.apply(LongFlags, __spreadArray([void 0], flagNames, false)))(); }