Flags enums
The com.siteblade.util package contains an approximation of this in ECMAScript.
www.npmjs.com/package/com.siteblade.util
You can use either FlagsEnum or Enum.
import { Enum } from 'com.siteblade.util';
const CollisionType = Enum('CollisionType', [ ['CIRCLE'], ['CUBIC_BEZIER_CURVE', [10]], ['RIGID_BODY', ['rigidBody', 2]] ]);
var type = CollisionType('cubicBezierCurve'); console.log( type == 'cubicBezierCurve' );
valueOf() in this case will return String. If you need number, use the 'number' property.
An HTML attachment was scrubbed... URL: esdiscuss/attachments/20200826/dbef69e6/attachment
For what it's worth, TypeScript has Enums built-in and I find myself using string literal types instead:
type CollisionType = 'CIRCLE' | 'CUBIC_BEZIER_CURVE' | 'RIGID_BODY';
function handleCollision(type: CollisionType) {
if (type === 'CIRCLE') {
// ...
} else if (type === 'SQUARE') { // Tooling errors on this line
// ...
}
}
In VSCode (and I assume other IDEs that support TypeScript) you can even do this in vanilla JS with structured JSDoc comments and the tooling will type check for you:
// @ts-check
/** @typedef {'CIRCLE' | 'CUBIC_BEZIER_CURVE' | 'RIGID_BODY'} CollisionType
*/
/**
* @param {CollisionType} type
*/
function handleCollision(type) {
if (type === 'CIRCLE') {
// ...
} else if (type === 'SQUARE') { // Tooling errors on this line
// ...
}
}
As far as I know, comparing strings is nearly as performant in most JS engines as comparing numbers or objects because they use a method called "string interning"
An HTML attachment was scrubbed... URL: esdiscuss/attachments/20200826/231f6f15/attachment
Flags enums would be primitive classes with constants (name:String, value:Number). It'd be very, very efficient.
Of course it's easy to use POJOs (
{}) instead of numeric values, but this feature would only provide performance advantages.The following program implies:
String(E.FNX) == 'fnx'E.FNX.valueOf() == 1String(E.QWL_QWERTY) == 'xxxQwerty'E.QWL_QWERTY.valueOf() == 32String(E.WOW_K) == 'wowK'E.WOW_K.valueOf() == 64enum E { FNX, QWL_QWERTY = ['xxxQwerty', 32]; WOW_K; f() { // Some method } } var e = E('xxxQwerty'); // constant in e // E.prototype.set() // E.prototype.exclude() // E.prototype.toggle() // E.prototype.filter() console.log( 'xxxQwerty' in e ); console.log( ['fnx', 'xxxQwerty'] in e ); console.log( e.set('fnx').exclude('fnx').filter(['fnx', 'xxxQwerty']) ); console.log( e.toggle('xxxQwerty') ); console.log( e.valueOf() ); console.log( E(['fnx', 'xxxQwerty']) );In typed languages, Array and String would implicitly convert to E.
One could define constant either by omitting expression, by an array literal (
[name, value]or[value, name]), by a string literal or by a numeric literal. More examples:Flags enums would be primitive classes with constants (_name_:String, _value_:Number). It'd be very, very efficient. Of course it's easy to use POJOs (`{}`) instead of numeric values, but this feature would only provide performance advantages. The following program implies: - `String(E.FNX) == 'fnx'` - `E.FNX.valueOf() == 1` - `String(E.QWL_QWERTY) == 'xxxQwerty'` - `E.QWL_QWERTY.valueOf() == 32` - `String(E.WOW_K) == 'wowK'` - `E.WOW_K.valueOf() == 64` ```javascript enum E { FNX, QWL_QWERTY = ['xxxQwerty', 32]; WOW_K; f() { // Some method } } var e = E('xxxQwerty'); // constant in e // E.prototype.set() // E.prototype.exclude() // E.prototype.toggle() // E.prototype.filter() console.log( 'xxxQwerty' in e ); console.log( ['fnx', 'xxxQwerty'] in e ); console.log( e.set('fnx').exclude('fnx').filter(['fnx', 'xxxQwerty']) ); console.log( e.toggle('xxxQwerty') ); console.log( e.valueOf() ); console.log( E(['fnx', 'xxxQwerty']) ); ``` In typed languages, Array and String would implicitly convert to E. One could define constant either by omitting expression, by an array literal (`[name, value]` or `[value, name]`), by a string literal or by a numeric literal. More examples: ``` enum E { AA_AA; LNK = 'vVvVvV'; QO = 32; } ``` -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20200820/84a6899f/attachment.html>