Henok Tesfaye
Henok Tesfaye

Reputation: 9560

How to access object key values semantically

const COMPUTE_CODES = Object.freeze({
  'D+1': '0011111',
  'A+1': '0110111',
  'D-1': '0001110',
  'A-1': '0110010',
  'D+A': '0000010',
  'D-A': '0010011',
  'A-D': '0000111'
  // more 
})

I want to get the value if key is either D+1 or 1+D, but since there are many keys, I don't want to add more redundant semantic keys.

I tried to sort the given key, '1+D'.split('').sort().join('').toUpperCase() // +1D but it is +1D not D+1.

The question is to access all keys, not D+1, it is a specific example.

const COMPUTE_CODES = Object.freeze({
      'D+1': '0011111',
      '1+D': '0011111',
      'A+1': '0110111',
      '1+A': '0110111',
      'D-1': '0001110',
      '1-D': '0001110',
      'A-1': '0110010',
      '1-A': '0110010',
      // more 
    })

I don't want to write like this.

Upvotes: 0

Views: 96

Answers (5)

KooiInc
KooiInc

Reputation: 122916

You can use a Proxy for that

const semanticGetterForProps = {
  get: (obj, name) => obj[name] || obj[[...name].reverse().join("")]
};
const COMPUTE_CODES =  new Proxy({
  'D+1': '0011111',
  'A+1': '0110111',
  'D-1': '0001110',
  'A-1': '0110010',
  'D+A': '0000010',
  'D-A': '0010011',
  'A-D': '0000111',
}, semanticGetterForProps);

console.log(COMPUTE_CODES["1+D"]);
console.log(COMPUTE_CODES["D+1"]);
console.log(COMPUTE_CODES["1-A"]);
console.log(COMPUTE_CODES["A-1"]);
console.log(COMPUTE_CODES["A+1"]);
.as-console-wrapper { top: 0; max-height: 100% !important; }

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386654

You could take a Proxy and use a reversed key, if necessary.

const
    COMPUTE_CODES = { 'D+1': '0011111', 'A+1': '0110111', 'D-1': '0001110', 'A-1': '0110010', 'D+A': '0000010', 'D-A': '0010011', 'A-D': '0000111' },
    codes = new Proxy(COMPUTE_CODES, {
        get: function(target, prop, receiver) {
            return prop in target
                ? target[prop]
                : target[prop.split(/([+-])/).reverse().join('')];
        }
    });
    

console.log(codes['1+D']);

Upvotes: 6

Koushik Seal
Koushik Seal

Reputation: 25

Simply check straight and reverse

function getCode(code){
 return COMPUTE_CODES[code] || COMPUTE_CODES[code.split('').reverse().join('')]
}

Upvotes: 2

David Alvarez
David Alvarez

Reputation: 1286

const COMPUTE_CODES = Object.freeze({
  'D+1': '0011111',
  'A+1': '0110111',
  'D-1': '0001110',
  'A-1': '0110010',
  'D+A': '0000010',
  'D-A': '0010011',
  'A-D': '0000111'
  // more 
})

function get(key) {
  return COMPUTE_CODES[key] || COMPUTE_CODES[reverseKey(key)]
}

function reverseKey(key) {
  const separator = key.charAt(1)
  const [first, second] = key.split(separator)
  return `${second}${separator}${first}`
}

console.log(get('1+D'))

Upvotes: 1

Barmar
Barmar

Reputation: 781245

Try the key that's given. If it doesn't exist, flip it around the + or - character and try that.

function get_compute_code(key) {
    if (key in COMPUTE_CODES) {
        return COMPUTE_CODES[key];
    }
    // try flipping the key around
    key = key.replace(/^(.*)([-+])(.*)/, '$3$2$1')
    return COMPUTE_CODES[key];
}

Upvotes: 2

Related Questions