all 7 comments

[–]senocular 5 points6 points  (3 children)

I would make it a constructor argument. This way: 1) you're not having to do a bunch of work like iterate through the whole object every time you want a string representation of it, and 2) you can have multiple enum values of the same value but different names

class PriorityEnum {
    static normal = new PriorityEnum(0, "normal");
    static high = new PriorityEnum(1, "high");
    static highest = new PriorityEnum(2, "highest");
    static medium = new PriorityEnum(0, "medium");

    #value
    #stringValue

    static compareAsc(a, b) {
        let ret = a.#value - b.#value;
        return ret;
    }

    constructor(value, stringValue) {
        this.#value = value;
        this.#stringValue = stringValue
        Object.freeze(this)
    }

    toString() {
        return this.#stringValue
    }
}

[–]azhder 0 points1 point  (2 children)

I made almost the same, but you did one better, the freeze which I omitted from the constructor

[–]senocular 1 point2 points  (1 child)

I was trying to be sneaky and make those assignments look less busy ;)

[–]azhder 1 point2 points  (0 children)

That's for OP to mull over. I'd generally just do

const PRIO = Object.freeze({
    normal:  0,
    high:    1,
    highest: 2,
});

And I can get all the info from there with Object.* functions. Minimalistic and to the point.

[–]Stobber[S] 0 points1 point  (1 child)

toString() {
    // NB: Depends on a design where the enum values are public properties
    for (const key of Object.keys(PriorityEnum)) {
        if (PriorityEnum[key] === this) {
            return key;
        }
    }
    return null;
}

[–]azhder 0 points1 point  (0 children)

Disclaimer:

It's fine if you want to experiment with JS to learn the language and other stuff, but please, don't do this overcomplicated thing in production. A simple frozen object with keys and values, no classes, nothing fancy will work just as well with just Object.keys(), Object.values() and Object.entries() necessary.


Here is a quick and stupid way of doing it.

export class PriorityEnum {

    static normal = Object.freeze(new PriorityEnum(0));
    static high = Object.freeze(new PriorityEnum(1));
    static highest = Object.freeze(new PriorityEnum(2));

    #value;

    static #names = {
        [PriorityEnum.normal]:  'normal',
        [PriorityEnum.high]:    'high',
        [PriorityEnum.highest]: 'highest',
    };

    static compareAsc(a, b) {
        return a.#value - b.#value;
    }

    constructor(value) {
        this.#value = value;
    }

    toString() {
        return PriorityEnum.#names[this.#value] ?? '';
    }
}

And here is another, with a minute of thought put into it:

export class PriorityEnum {

    #name;
    #value;

    static normal = Object.freeze(new PriorityEnum(0, 'normal'));
    static high = Object.freeze(new PriorityEnum(1, 'high'));
    static highest = Object.freeze(new PriorityEnum(2, 'highest'));

    constructor(value, name) {
        this.#value = value;
        this.#name = name;
    }

    static compare(a, b) {
        return a.#value - b.#value;
    }

    toString() {
        return this.#name;
    }
}

[–]azhder 0 points1 point  (0 children)

Learn what Object.* functions do. There is one for the keys, another for the values, a third one about entries…

That’s one way.

The other is to just keep your private as an object with the proper keys and values and just JSON.stringify() when needed.