How to use destructuring assignment to define enumerations in ES6?

Aadit M Shah

You can use destructuring assignment to define enumerations in ES6 as follows:

var [red, green, blue] = [0, 1, 2];

Instead, I'd like the right hand side of the destructuring assignment to be dynamic. For example:

var MAX_ENUM_SIZE = 32;
var ENUM = new Array(MAX_ENUM_SIZE);
for (var i = 0; i < MAX_ENUM_SIZE; i++) ENUM[i] = i;

var [red, green, blue] = ENUM;

Unfortunately, this seems like a hack. What if I want a bigger enumeration in the future? Hence, I was thinking of using destructuring assignment with an iterator as follows:

var [red, green, blue] = enumeration(/* I don't want to specify size */);

However, I don't think it's possible to use destructuring assignment with iterators[citation needed]. Is there any way to accomplish this goal?

Thank you

Use a generator

function* enumerator() {
  let i = 0;
  while (true) yield i++;
};

let [red,green,blue] = enumerator();
console.log(red, green, blue); // 0 1 2

let [a,b,c,d,e] = enumerator();
console.log(a,b,c,d,e); // 0 1 2 3 4

The generator is flexible making this pretty neat for implementing different types of enums – for example, these cute bitmask enums

function* bitmask() {
  let i = 0;
  while (i < 32) yield 1 << i++;
  throw Error("bitmask enumerator exceeds 32 bits");
}

let [R,W,X] = bitmask();

const read = p => (p & R) !== 0;
const write = p => (p & W) !== 0;
const exec = p => (p & X) !== 0;

{
  let p = R | W; // read and write only
  console.log("can read?", read(p));   // true
  console.log("can write?", write(p)); // true
  console.log("can exec?", exec(p));   // false
}

{
  let p = R | X; // read and execute only
  console.log("can read?", read(p));    // true
  console.log("can write?", write(p));  // false
  console.log("can exec?", exec(p));    // true
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ES6 Destructuring assignment with `this`

How to mix const and let when using object or array destructuring assignment in ES6?

Must use destructuring props assignment (react/destructuring-assignment)

Non-local destructuring assignment in ES6

ES6 destructuring, dynamic assignment

What can I use as placeholders in es6 array destructuring?

How to use Scala Enumerations in Classes

How to use deep destructuring on imports in ES6 syntax?

Using destructuring to define optional parameters in ES6

javascript es6: use case for destructuring rest parameter

Destructuring assignment vs object property access in ES6

how to bind data to this by destructuring assignment?

Use of Colon in object assignment destructuring Javascript

How can I use this in the left side of the object destructuring assignment?

Must use destructuring props assignment

How to convert es6 destructuring and rename argument to typescript?

How to use special characters (like hyphen) in destructuring assignment syntax?

How to use a destructuring assignment inside an object

Reassign variables using ES6 destructuring assignment syntax

Must use destructuring props assignment issue

How to namespace Destructuring assignment?

Must use destructuring props assignment in className

Must use destructuring props assignment error in constructor

ES6 destructuring assignment with more than one variable type

Error: Must use destructuring state assignment

How to use array destructuring and assign to an object property in JavaScript ES6

How to export a part of destructuring assignment?

How to use ES6 destructuring assignment to assign one object to another

Must use destructuring theme assignment react/destructuring-assignment