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