implemented of() and named() for numbers

This commit is contained in:
2026-02-22 14:21:22 +01:00
parent 140896bd7c
commit c76b5c3f0a
40 changed files with 1876 additions and 4344 deletions

View File

@@ -61,4 +61,8 @@ export class ArrayFlagSet<T> implements FlagSet<T, T[]> {
minimum(flags: T[]): T[] {
throw new Error('Method not implemented.')
}
public getFlag(alias: string): FlagDefinition<number, number> | undefined {
return this._dictionary.lookUp(alias)
}
}

View File

@@ -127,4 +127,8 @@ export class Base64BitFlagSet implements FlagSet<number, string> {
minimum(flags: string): string {
throw new Error('not implemented')
}
public getFlag(alias: string): FlagDefinition<number, number> | undefined {
return this._dictionary.lookUp(alias)
}
}

View File

@@ -51,4 +51,8 @@ export class BigBitFlagSet implements FlagSet<bigint, bigint> {
public enumerate(flags: bigint): EnumerateFlags<bigint> {
return useIterator(flags, BigBitFlagsIterator)
}
public getFlag(alias: string): FlagDefinition<number, number> | undefined {
return this._dictionary.lookUp(alias)
}
}

View File

@@ -46,4 +46,8 @@ export class CollectionFlagSet<T> implements FlagSet<T, Set<T>> {
maximum(flags: Set<T>): Set<T> {
throw new Error('Method not implemented.')
}
public getFlag(alias: string): FlagDefinition<number, number> | undefined {
return this._dictionary.lookUp(alias)
}
}

View File

@@ -1,4 +1,5 @@
import { EnumerateFlags } from '../enumeration'
import { FlagDefinition } from '../definitions'
/**
* Represents a group of flags of type `F` and the relationships between
@@ -13,6 +14,16 @@ export interface FlagSet<F, S> {
*/
none(): S
/**
* Creates a set of flags from a list of values.
*/
of(...values: F[]): S
/**
* Creates a set of flags from a list of aliases.
*/
named(...aliases: string[]): S
/**
* Computes the union of two sets of flags.
*
@@ -54,6 +65,30 @@ export interface FlagSet<F, S> {
*/
isSuperset(first: S, second: S): boolean
/**
* Checks whether the first set of flags includes at least one of the flags
* from the second set.
*
* A flag is considered to be part of the set only if all of its parents are
* present too.
*
* @param flags - A set of flags.
* @param required - The flags to search for in the first set.
*/
hasAny(flags: S, required: S): boolean
/**
* Checks whether the first set of flags includes all the flags from the
* second set.
*
* A flag is considered to be part of the set only if all of its parents are
* present too.
*
* @param flags - A set of flags.
* @param required - The flags to search for in the first set.
*/
hasAll(flags: S, required: S): boolean
/**
* Returns an iterable over the individual flags in a set.
*
@@ -86,6 +121,14 @@ export interface FlagSet<F, S> {
* @see minimum
*/
maximum(flags: S): S
/**
* Retrieve a flag definition from its alias.
* @param alias The alias of the flag.
* @returns The corresponding definition, or `undefined` if there is no flag
* with this alias.
*/
getFlag(alias: string): FlagDefinition<F, S> | undefined
}
export { ArrayFlagSet } from './array'

View File

@@ -1,11 +1,29 @@
import type { FlagSet } from '.'
import { BitFlagsIterator, EnumerateFlags, useIterator } from '../enumeration'
import { FlagDefinition, FlagsDictionary } from '../definitions'
export class BitFlagSet implements FlagSet<number, number> {
private readonly _dictionary: FlagsDictionary<number, number>
public constructor(dictionary: FlagsDictionary<number, number>) {
this._dictionary = dictionary
}
public none(): number {
return 0
}
public of(...values: number[]): number {
return values.reduce((set, value) => set | value, 0)
}
public named(...aliases: string[]): number {
return aliases.reduce(
(set, alias) => set | (this.getFlag(alias)?.values ?? 0),
0,
)
}
public union(first: number, second: number): number {
return first | second
}
@@ -22,6 +40,14 @@ export class BitFlagSet implements FlagSet<number, number> {
return (first & second) == second
}
public hasAny(flags: number, required: number): boolean {
return false
}
public hasAll(flags: number, required: number): boolean {
return false
}
public enumerate(flags: number): EnumerateFlags<number> {
return useIterator(flags, BitFlagsIterator)
}
@@ -33,4 +59,8 @@ export class BitFlagSet implements FlagSet<number, number> {
public minimum(flags: number): number {
return 0
}
public getFlag(alias: string): FlagDefinition<number, number> | undefined {
return this._dictionary.findByAlias(alias)
}
}