What are enum Flags in TypeScript?

后端 未结 5 1244
死守一世寂寞
死守一世寂寞 2021-01-30 17:08

I\'m learning TypeScript using this ebook as a reference. I\'ve checked the TypeScript Official Documentation but I don\'t find information about enum flags.

5条回答
  •  时光取名叫无心
    2021-01-30 17:19

    You can check this solution, that add some infra, but if you will use it a lot it really nice, the usage goes like that:

    let FlaggedExample: IFlaggedEnum = FlaggedEnum.create(Example, 1 << 2);  // class definition
    
    let example = new FlaggedExample(3); // Alpha,Beta instance of the class
    
    export module FlaggedEnum {
        "use strict";
        export interface IFlaggedEnumGenerator {
            (_enum: any, _max: number): IFlaggedEnum;
        }
    
        export interface IFlaggedEnum {
            (val: IFlaggedEnum): void;
            (val: number): void;
            (val: string): void;
    
            /** array of the individual enum flags that represent the value 
             */
            toArray(): IFlaggedEnum[];
    
            /** does this instance contain all the flags of the value 
             */
            contains(val: IFlaggedEnum): boolean;
            contains(val: number): boolean;
            contains(val: string): boolean;
    
            /** adds the flags to the value and returns a new instance 
             */
            add(val: IFlaggedEnum): IFlaggedEnum;
            add(val: number): IFlaggedEnum;
            add(val: string): IFlaggedEnum;
    
            /** removes the flags from the value and returns a new instance 
             */
            remove(val: IFlaggedEnum): IFlaggedEnum;
            remove(val: number): IFlaggedEnum;
            remove(val: string): IFlaggedEnum;
    
            /** returns an instance containing all intersecting flags 
             */
            intersect(val: IFlaggedEnum): IFlaggedEnum;
            intersect(val: number): IFlaggedEnum;
            intersect(val: string): IFlaggedEnum;
    
            /** does the two instances equal each other 
             */
            equals(val: IFlaggedEnum): boolean;
            equals(val: number): boolean;
            equals(val: string): boolean;
    
        }
    
        /** create a class definition for a Flagged Enum
         * @method create
         * @param _enum {enum} The enum definition being exteded
         * @param _max {number} the maximum possible value of the enum being extended
         * @returns {IFlaggedEnum} the class definition for the provided enum
         */
        export var create: IFlaggedEnumGenerator = function (_enum: any, _max: number): IFlaggedEnum {
    
            var base: any = _enum,
                max: number = _max;
    
            var Base: IFlaggedEnum = function (val: any): void {
                if (typeof (val) === "string") {
                    val = base[val];
                }
                this.value = val + 0;
            };
    
            var proto: any = Base.prototype;
    
            proto.valueOf = function (): number { return this.value; };
            proto.toString = function (): string {
                var list: string[] = [];
                for (var i: number = 1; i < max; i = i << 1) {
                    if ((this.value & i) !== 0) {
                        list.push(base[i]);
                    }
                }
                return list.toString();
            };
    
            proto.toArray = function (): IFlaggedEnum[] {
                var list: IFlaggedEnum[] = [];
                for (var i: number = 1; i < max; i = i << 1) {
                    if ((this.value & i) !== 0) {
                        list.push(new Base(i));
                    }
                }
                return list;
            };
    
            proto.contains = function (val: any): boolean {
                if (typeof (val) === "string") {
                    val = base[val];
                }
                return (this.value & val) === (val + 0);
            };
    
            proto.add = function (val: any): IFlaggedEnum {
                if (typeof (val) === "string") {
                    val = base[val];
                }
                return new Base(this.value | val);
            };
    
            proto.remove = function (val: any): IFlaggedEnum {
                if (typeof (val) === "string") {
                    val = this.base[val];
                }
                return new Base((this.value ^ val) & this.value);
            };
    
            proto.intersect = function (val: any): IFlaggedEnum {
                if (typeof (val) === "string") {
                    val = base[val];
                }
                var final: number = 0;
                for (var i: number = 1; i < max; i = (i << 1)) {
                    if ((this.value & i) !== 0 && (val & i) !== 0) {
                        final += i;
                    }
                }
                return new Base(final);
            };
    
            proto.equals = function (val: any): boolean {
                if (typeof (val) === "string") {
                    val = base[val];
                }
                return this.value === (val + 0);
            };
    
            return Base;
    
        };
    }
    

提交回复
热议问题