bitwise-flag - v1.1.0
    Preparing search index...

    Interface IFlag<TFlags>

    Represents a bitwise combination of flags from a registry. This class encapsulates a bitmask value derived from one or more flag keys, enabling efficient storage and manipulation of boolean states (e.g., permissions, features, or configurations) using bitwise operations.

    interface IFlag<TFlags extends FlagKey> {
        alias: string;
        value: bigint;
        add(...flagNames: TFlags[]): IFlag<TFlags>;
        has(flagName: TFlags): boolean;
        isEmpty(): boolean;
        remove(...flagNames: TFlags[]): IFlag<TFlags>;
        toString(): string;
    }

    Type Parameters

    Implemented by

    Index

    Properties

    alias: string

    A computed, human-readable alias for the flag combination.

    • For empty flags (value 0n), returns "EMPTY_FLAG".
    • For single flags, returns e.g., "[READ]".
    • For multiple flags, returns e.g., "[READ+WRITE]".
    value: bigint

    The raw BigInt bitmask representing the combined flags in this instance. This value is read-only and reflects the bitwise OR of all set flags.

    Methods

    • Adds one or more flag keys to this flag combination, creating a new instance with the updated bitmask.

      • Idempotent: If a flag is already set, it remains unchanged.
      • Only adds flags that exist in the registry; unknown keys throw an error.
      • Returns the current instance if no changes are made (e.g., all flags already present).

      Parameters

      • ...flagNames: TFlags[]

        One or more flag keys to add.

      Returns IFlag<TFlags>

      A new Flag instance with the added flags, or the current instance if unchanged.

      If any flagName is not registered in the registry (e.g., Flag with key UNKNOWN is not found.).

    • Tests whether a specific flag key is set in this flag combination.

      Performs a bitwise AND between the instance's value and the bitmask of the given flag key. Returns false if the key is not found in the registry.

      Parameters

      • flagName: TFlags

        The flag key to check (must be a valid key in the registry).

      Returns boolean

      true if the flag is set, false otherwise.

    • Checks if this flag instance represents no set flags (i.e., the bitmask value is 0n).

      Returns boolean

      true if the flag is empty, false otherwise.

    • Removes one or more flag keys from this flag combination, creating a new instance with the updated bitmask.

      • Idempotent: If a flag is not set, it remains unchanged.
      • Only removes flags that exist in the registry; unknown keys throw an error.
      • Returns the current instance if no changes are made (e.g., none of the flags were present).

      Parameters

      • ...flagNames: TFlags[]

        One or more flag keys to remove.

      Returns IFlag<TFlags>

      A new Flag instance with the removed flags, or the current instance if unchanged.

      If any flagName is not registered in the registry (e.g., Flag with key UNKNOWN is not found.).

    • Returns a human-readable string representation of this flag instance.

      The format is Flag(${alias}: ${value}), where alias is the computed alias (e.g., [READ+WRITE]) and value is the raw BigInt bitmask.

      Returns string

      A string like Flag([READ+WRITE]: 3).