background

More or less in a project, you’ll come across enumerations/quickcodes/maps/dictionaries that look like this. (PS: I don’t know what to call it)

The following code is used to show the definition where it is needed.

<div>{{ statusList[status] }}</div>
Copy the code

In code, the following form is used. Doing so would flood your code with ‘draft’ strings, which would be unmanageable.

if (status === 'draft') {
    // do sth...
}
Copy the code

In this case, a variable is declared first.

const DRAFT = 'draft'

if (status === DRAFT) {
    // do sth...
}
Copy the code

This is done in case the entire project will be used.

export const statusList = {
  draft: 'draft'.pending: 'Pending',}export const statusKeys = {
  draft: 'draft'.pending: 'pending',}Copy the code

In Java, enumerations are generally defined and used in the following form. So I came up with the idea of writing this tool class.

public enum Status {
	DRAFT('draft'.'draft');
    
    Status(String code, String name) {
    	this.code = code;
        this.name = name;
    }
    
    public String getCode(a) {
        return code;
    }

    public String getName(a) {
        returnname; }}public void aFunction(a) {
    const draftCode = Status.DRAFT.getCode();
}
Copy the code

Constants

Go straight to code

const noop = () = > {}

class Constants {
  constructor(obj) {
    Object.keys(obj).forEach((key) = > {
      const initValue = obj[key];
      if (initValue instanceof Object) {
        console.error(`Warnning: this util only support primitive values, current value is The ${JSON.stringify(initValue)}`)
        // throw new Error(`Warnning: this util only support primitive values, current value is ${JSON.stringify(initValue)}`)
      }
      const newKey = ` _${key}`;
      this[newKey] = initValue;
      Object.defineProperty(this, key, {
        configurable : true.enumerable : true.get: function() {
          const value = this[newKey];
          const constructorOfValue = value.constructor;
          const entry = [key, value];
          ['getKey'.'getValue'].forEach((item, index) = > {
            constructorOfValue.prototype[item] = () = > {constructorOfValue.prototype.getKey = noop;
              constructorOfValue.prototype.getValue = noop;
              returnentry[index]; }})return value;
        },
        set: function(newValue) {
          this[newKey] = newValue; }})}); }}Copy the code

test

const testValues = {
  draft: 'draft'.id: 1.money: 1.2.isTest: true.testObj: {},
  testArray: [],}const constants = new Constants(testValues)

const test = (result, expect) = > {
  const isExpected = result === expect
  if (isExpected) {
    console.log(`PASS: The result is ${result}`)}else {
    console.error(`FAIL: the result is ${result}, should be ${expect}`)
  }
}

test(constants.draft, 'draft')
test(constants.draft.getKey(), 'draft')
test(constants.draft.getValue(), 'draft')

test(constants.id, 1)
test(constants.id.getKey(), 'id')
test(constants.id.getValue(), 1)

test(constants.money, 1.2)
test(constants.money.getKey(), 'money')
test(constants.money.getValue(), 1.2)

test(constants.isTest, true)
test(constants.isTest.getKey(), 'isTest')
test(constants.isTest.getValue(), true)
a = 'test'
test(a.getKey(), undefined)
test(a.getValue(), undefined)
Copy the code