diff --git a/src/utils/enum/descriptors/bigint-descriptor.ts b/src/utils/enum/descriptors/bigint-descriptor.ts new file mode 100644 index 0000000..376f9aa --- /dev/null +++ b/src/utils/enum/descriptors/bigint-descriptor.ts @@ -0,0 +1,41 @@ +import { EnumDescriptor } from "./enum-descriptor"; + +/** + * This descriptor is used to describe a set of flags stored as a `bigint` value. + */ +export class BigIntDescriptor implements EnumDescriptor { + /** + * @inheritdoc + */ + get name(): "bigint" { + return "bigint"; + } + + /** + * @inheritdoc + */ + get defaultValue(): bigint { + return 0n; + } + + /** + * @inheritdoc + */ + hasFlag(value: bigint, flag: bigint): boolean { + return (value & flag) === flag; + } + + /** + * @inheritdoc + */ + addFlag(value: bigint, flag: bigint): bigint { + return value | flag; + } + + /** + * @inheritdoc + */ + removeFlag(value: bigint, flag: bigint): bigint { + return value & ~flag; + } +} diff --git a/tests/unit/utils/enum/descriptors/bigint-descriptor.spec.ts b/tests/unit/utils/enum/descriptors/bigint-descriptor.spec.ts new file mode 100644 index 0000000..ee78e03 --- /dev/null +++ b/tests/unit/utils/enum/descriptors/bigint-descriptor.spec.ts @@ -0,0 +1,47 @@ +import { BigIntDescriptor } from "@/utils/enum/descriptors/bigint-descriptor"; + +describe("BigIntDescriptor", () => { + const descriptor = new BigIntDescriptor(); + + describe("name", () => { + test("returns 'bigint' as name", () => { + expect(descriptor.name).toBe("bigint"); + }); + }); + + describe("defaultValue", () => { + test("returns 0n as default value", () => { + expect(descriptor.defaultValue).toBe(0n); + }); + }); + + describe("hasFlag", () => { + test("returns true if flag is set", () => { + expect(descriptor.hasFlag(3n, 2n)).toBe(true); + }); + + test("returns false if flag is not set", () => { + expect(descriptor.hasFlag(3n, 4n)).toBe(false); + }); + }); + + describe("addFlag", () => { + test("adds flag to value", () => { + expect(descriptor.addFlag(1n, 2n)).toBe(3n); + }); + + test("does not add flag if it is already set", () => { + expect(descriptor.addFlag(3n, 2n)).toBe(3n); + }); + }); + + describe("removeFlag", () => { + test("removes flag from value", () => { + expect(descriptor.removeFlag(3n, 2n)).toBe(1n); + }); + + test("does not remove flag if it does not exist", () => { + expect(descriptor.removeFlag(1n, 2n)).toBe(1n); + }); + }); +});