All files / src/Ice EnumBase.js

98.18% Statements 108/110
100% Branches 19/19
87.5% Functions 14/16
98.18% Lines 108/110

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 11141x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 1787x 1787x 1787x 41x 41x 2822x 2822x 41x 41x     41x 41x 5755x 5755x 41x 41x 41x 41x 384x 384x 41x 41x 374x 374x 41x 41x 15x 15x 41x 41x 342x 342x 41x 41x 12x 12x 41x 41x 41x 384x 384x 384x 384x 384x 384x 1784x 1784x 1784x 1784x 1784x 1784x 1784x 1784x 1784x 1400x 1400x 1784x 384x 384x 384x 384x 431x 384x 384x 384x 384x 384x 384x 384x 30x 11x 11x 11x 11x 384x 384x 384x 27x 384x 384x 384x 384x 384x 384x 9624x 384x 384x 384x 384x 384x 384x 384x 384x 384x 384x 384x 384x 384x  
// Copyright (c) ZeroC, Inc.
 
// Declared here to avoid circular dependencies when importing OptionalFormat enum.
const OptionalFormat_Size = {};
Object.defineProperty(OptionalFormat_Size, "value", { value: 4 });
 
//
// Ice.EnumBase
//
class EnumBase {
    constructor(name, value) {
        this._name = name;
        this._value = value;
    }
 
    toString() {
        return this._name;
    }
 
    get name() {
        return this._name;
    }
 
    get value() {
        return this._value;
    }
}
 
class EnumHelper {
    constructor(enumType) {
        this._enumType = enumType;
    }
 
    write(os, v) {
        this._enumType._write(os, v);
    }
 
    writeOptional(os, tag, v) {
        this._enumType._writeOpt(os, tag, v);
    }
 
    read(is) {
        return this._enumType._read(is);
    }
 
    readOptional(is, tag) {
        return this._enumType._readOpt(is, tag);
    }
}
 
export function defineEnum(enumerators) {
    const type = class extends EnumBase {};
 
    const enums = [];
    let maxValue = 0;
 
    for (const idx in enumerators) {
        const e = enumerators[idx][0];
        const value = enumerators[idx][1];
        const enumerator = new type(e, value);
        enums[value] = enumerator;
        Object.defineProperty(type, e, {
            enumerable: true,
            value: enumerator,
        });
        if (value > maxValue) {
            maxValue = value;
        }
    }
 
    Object.defineProperty(type, "minWireSize", { get: () => 1 });
 
    type._write = function (os, v) {
        os.writeEnum(v);
    };
 
    type._read = function (is) {
        return is.readEnum(type);
    };
 
    type._writeOpt = function (os, tag, v) {
        if (v !== undefined && v !== null) {
            if (os.writeOptional(tag, OptionalFormat_Size)) {
                type._write(os, v);
            }
        }
    };
 
    type._readOpt = function (is, tag) {
        return is.readOptionalEnum(tag, type);
    };
 
    type._helper = new EnumHelper(type);
 
    Object.defineProperty(type, "valueOf", {
        value: function (v) {
            return enums[v];
        },
    });
 
    Object.defineProperty(type, "maxValue", {
        value: maxValue,
    });
 
    Object.defineProperty(type.prototype, "maxValue", {
        value: maxValue,
    });
 
    return type;
}