Nodejs String to Enum Convert toEnumFlag(Type)

Here you can find the source of toEnumFlag(Type)

Method Source Code

"use strict";/*  ww  w.  ja  v  a2 s  .  com*/
var toEnumFlag = function (string, Type) {
    var keys = string.split(",");
    var intList = keys.filter(function (string) {
        return Type[string.trim()] == null ? false : true;
    }).map(function (string) {
        return Type[string.trim()];
    });
    if (intList.length === 0) {
        return 0;
    }
    return intList.reduce(function (last, next) {
        return last | next;
    });
};
String.prototype.toEnumFlag = function (Type) {
    return toEnumFlag(this, Type);
};
module.exports = toEnumFlag;
//# sourceMappingURL=toEnumFlag.js.map

Related

  1. toEnum(Type)
    "use strict";
    var toEnum = function (string, Type) {
        var value = parseFloat(Type[string.trim()].valueOf());
        if (isNaN(value)) {
            throw new Error("Coundn't resolve string to an Enum value.");
        return value;
    };
    String.prototype.toEnum = function (Type) {
    ...
    
  2. toEnumFlagString(Type)
    Number.prototype.toEnumFlagString = function (Type) {
        var value = this.valueOf();
        var response = Object.keys(Type).filter(function (key) {
            return ((value & Type[key]) == Type[key]) && Type[key] != 0 && typeof Type[key].name === "string";
        }).map(function (key) {
            return Type[key].name || key;
        }).join(", ");
        return (response.length > 0 ? response : "None");
    };
    ...
    
  3. toEnumString(Type)
    Number.prototype.toEnumString = function (Type) {
        var number = this.valueOf();
        var response = Object.keys(Type).filter(function (key) {
            return Type[key] != null && typeof Type[key].name === "string" && Type[key].valueOf() === number.valueOf();
        });
        var string = response[0];
        if (string == null) {
            throw new Error("Couldn't find Enum string value.");
        return string;
    };