mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-25 09:51:01 -05:00
Implemented enum descriptor for number enums
This commit is contained in:
parent
33da1eacff
commit
6256d33db6
2 changed files with 88 additions and 0 deletions
41
src/utils/enum/descriptors/number-descriptor.ts
Normal file
41
src/utils/enum/descriptors/number-descriptor.ts
Normal 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;
|
||||
}
|
||||
}
|
47
tests/unit/utils/enum/descriptors/number-descriptor.spec.ts
Normal file
47
tests/unit/utils/enum/descriptors/number-descriptor.spec.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue