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

    Class FlagsRegistry<TFlags>

    A registry for managing bitwise flags. This class maps flag keys (strings) to unique bit positions using BigInt for scalable storage.

    Type Parameters

    • TFlags extends FlagKey

      The type of flag keys, extending string.

    Implements

    Index

    Methods

    • Combines multiple flag keys into a single flag instance.

      Parameters

      • ...flagKeys: TFlags[]

        The flag keys to combine.

      Returns IFlag<TFlags>

      A flag instance representing the combined flags.

      const registry = FlagsRegistry.from("READ", "WRITE", "EXECUTE");
      const combinedFlag = registry.combine("READ", "EXECUTE");
      combinedFlag.has("READ"); // true
      combinedFlag.has("WRITE"); // false
      combinedFlag.has("EXECUTE"); // true
    • Returns a flag instance representing no set flags (i.e., a bitmask value of 0n).

      Returns IFlag<TFlags>

      A flag instance with no flags set.

      const registry = FlagsRegistry.from("READ", "WRITE");
      const emptyFlag = registry.empty();
      emptyFlag.isEmpty(); // true
    • Retrieves the BigInt value associated with the given flag name.

      Parameters

      • flagName: TFlags

        The name of the flag to retrieve.

      Returns bigint | undefined

      The BigInt value of the flag, or undefined if not found.

      const registry = FlagsRegistry.from("READ", "WRITE");
      registry.get("READ"); // 1n
      registry.get("WRITE"); // 2n
      registry.get("EXECUTE"); // undefined
    • Parses a number value to create a flag instance.

      Parameters

      • value: number

        The numeric value to parse.

      Returns IFlag<TFlags>

      A flag instance representing the parsed value.

      If the value is negative or contains unknown flags.

      const registry = FlagsRegistry.from("READ", "WRITE");
      const flag = registry.parse(3); // Represents both READ and WRITE flags
      flag.has("READ"); // true
      flag.has("WRITE"); // true
    • Parses a bigint value to create a flag instance.

      Parameters

      • value: bigint

        The BigInt value to parse.

      Returns IFlag<TFlags>

      A flag instance representing the parsed value.

      If the value is negative or contains unknown flags.

      const registry = FlagsRegistry.from("READ", "WRITE");
      const flag = registry.parse(3n); // Represents both READ and WRITE flags
      flag.has("READ"); // true
      flag.has("WRITE"); // true
    • Parses a string value to create a flag instance.

      Parameters

      • value: string

        The string value to parse.

      • Optionalradix: number

        The radix to use when parsing the string (default is 10).

      Returns IFlag<TFlags>

      A flag instance representing the parsed value.

      If the value cannot be parsed, is negative, or contains unknown flags.

      const registry = FlagsRegistry.from("READ", "WRITE");
      const flag = registry.parse("3"); // Represents both READ and WRITE flags
      flag.has("READ"); // true
      flag.has("WRITE"); // true

      const hexFlag = registry.parse("3", 16); // Parses "3" as hexadecimal
      hexFlag.has("READ"); // true
      hexFlag.has("WRITE"); // true

      const binaryFlag = registry.parse("11", 2); // Parses "11" as binary
      binaryFlag.has("READ"); // true
      binaryFlag.has("WRITE"); // true
    • Returns an iterator over the flag values in the registry.

      Returns MapIterator<bigint>

      An iterator over the flag values.

    • Creates a new FlagsRegistry from the provided flag keys, assigning each a unique bit position.

      Type Parameters

      • TFlags extends string

      Parameters

      • ...flagKeys: TFlags[]

        An array of flag keys to include in the registry.

      Returns FlagsRegistry<TFlags>

      A new FlagsRegistry instance with the specified flags.

      const registry = FlagsRegistry.from("READ", "WRITE", "EXECUTE");
      console.log(registry.get("READ")); // 1n
      console.log(registry.get("WRITE")); // 2n
      console.log(registry.get("EXECUTE")); // 4n