Skip to content

Enumeration

Overview

This implementation auto-detects the enumeration values by Object.keys and instanceof. Every property that has a type matching the enumeration type is marked as a value. See sample usage in Orientation.ts.

For general pattern see https://github.com/phetsims/phet-info/blob/main/doc/phet-software-design-patterns.md#enumeration

This creates 2-way maps (key-to-value and value-to-key) for ease of use and to enable phet-io serialization.

class T extends EnumerationValue { static a=new T(); static b =new T(); getName(){return 'he';} get thing(){return 'text';} static get age(){return 77;} static enumeration = new Enumeration( T ); } T.enumeration.keys => ['a', 'b'] T.enumeration.values => [T, T]

Note how keys only picks up 'a' and 'b'.

@author Sam Reid (PhET Interactive Simulations) @author Michael Kauzmann (PhET Interactive Simulations)

Class Enumeration

import { Enumeration } from 'scenerystack/phet-core';

Constructor

new Enumeration( Enumeration : Constructor<T>, providedOptions? : EnumerationOptions<T> )

Instance Methods

getKey( value : T ) : string

getValue( key : string ) : T

includes( value : T ) : boolean

Instance Properties

values : T[]

(readonly)

keys : string[]

(readonly)

Enumeration : Constructor<T> & Record<string, T>

(readonly)

phetioDocumentation : string

(readonly)

Type EnumerationOptions

import type { EnumerationOptions } from 'scenerystack/phet-core';
  • phetioDocumentation?: string
  • instanceType?: Constructor<T>

Source Code

See the source for Enumeration.ts in the phet-core repository.