chromium/mojo/public/tools/bindings/generators/js_templates/enum_definition.tmpl

{%- macro enum_def(enum_name, enum) -%}
  {{enum_name}} = {};
{%-   set prev_enum = 0 %}
{%-   for field in enum.fields %}
  {{enum_name}}.{{field.name}} = {{field.numeric_value}};
{%-   endfor %}
{%-   if enum.min_value is not none %}
  {{enum_name}}.MIN_VALUE = {{enum.min_value}};
{%-   endif %}
{%-   if enum.max_value is not none %}
  {{enum_name}}.MAX_VALUE = {{enum.max_value}};
{%-   endif %}
{%-   if enum.default_field %}
  {{enum_name}}.DEFAULT_VALUE = {{enum.default_field.numeric_value}};
{%-   endif %}

  {{enum_name}}.isKnownEnumValue = function(value) {
{%-   if enum.fields %}
    switch (value) {
{%-       for enum_field in enum.fields|groupby('numeric_value') %}
    case {{enum_field[0]}}:
{%-     endfor %}
      return true;
    }
{%-   endif %}
    return false;
  };

  {{enum_name}}.toKnownEnumValue = function(value) {
{%-   if enum.extensible and enum.default_field %}
    if (this.isKnownEnumValue(value))
      return value;
    return this.DEFAULT_VALUE;
{%-   else %}
    return value;
{%-   endif %}
  };

  {{enum_name}}.validate = function(enumValue) {
    const isExtensible = {% if enum.extensible %}true{% else %}false{% endif %};
    if (isExtensible || this.isKnownEnumValue(enumValue))
      return validator.validationError.NONE;

    return validator.validationError.UNKNOWN_ENUM_VALUE;
  };
{%- endmacro %}