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

60 lines (59 loc) 2.3 kB
import { BaseFlags } from "./BaseFlags"; import type { FlagsWithFlags } from "./types"; /** * 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 */ export declare class LongFlags extends BaseFlags { private static readonly _MAX_FLAGS; private static readonly _MAX_VALUE; protected readonly MAX_FLAGS = 32; protected readonly MAX_VALUE = 4294967295; /** * 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 */ constructor(...flagNames: string[]); /** * 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 */ static fromJSON(json: string | object): LongFlags; /** * Convert the flags to a long value (0-4294967295) * @returns Long representation of all flags */ toLong(): number; /** * 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 */ fromLong(l: number): this; } /** * 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 */ export declare function createLongFlags<T extends string>(...flagNames: T[]): FlagsWithFlags<T, LongFlags>;