Implemented enum descriptor for number enums

This commit is contained in:
Kir_Antipov 2023-01-17 05:00:10 +00:00
parent 33da1eacff
commit 6256d33db6
2 changed files with 88 additions and 0 deletions

View file

@ -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<number> {
/**
* @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;
}
}

View file

@ -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);
});
});
});