Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BreezeEnum

Base class for all Breeze enumerations, such as EntityState, DataType, FetchStrategy, MergeStrategy etc. A Breeze Enum is a namespaced set of constant values. Each Enum consists of a group of related constants, called 'symbols'. Unlike enums in some other environments, each 'symbol' can have both methods and properties.

class DayOfWeek extends BreezeEnum {
  dayIndex: number;
  isWeekend?: boolean;
  nextDay() {
    let nextIndex = (this.dayIndex + 1) % 7;
    return DayOfWeek.getSymbols()[nextIndex];
  }

  static Monday = new DayOfWeek( { dayIndex: 0});
  static Tuesday = new DayOfWeek( { dayIndex: 1 });
  static Wednesday = new DayOfWeek( { dayIndex: 2 });
  static Thursday = new DayOfWeek( { dayIndex: 3 });
  static Friday = new DayOfWeek( { dayIndex: 4 });
  static Saturday = new DayOfWeek( { dayIndex: 5, isWeekend: true });
  static Sunday = new DayOfWeek( { dayIndex: 6, isWeekend: true });
}

describe("DayOfWeek", () => {
  test("should support full enum capabilities", function() {
    // // custom methods
    let dowSymbols = DayOfWeek.getSymbols();
    expect(dowSymbols.length).toBe(7);
    expect(DayOfWeek.Monday.nextDay()).toBe(DayOfWeek.Tuesday);
    expect(DayOfWeek.Sunday.nextDay()).toBe(DayOfWeek.Monday);
  // // custom properties
    expect(DayOfWeek.Tuesday.isWeekend).toBe(undefined);
    expect(DayOfWeek.Saturday.isWeekend).toBe(true);
  // // Standard enum capabilities
    expect(DayOfWeek.Thursday instanceof DayOfWeek).toBe(true);
    expect(BreezeEnum.isSymbol(DayOfWeek.Wednesday)).toBe(true);
    expect(DayOfWeek.contains(DayOfWeek.Thursday)).toBe(true);
    expect(DayOfWeek.Friday.toString()).toBe("Friday");
  });

}); Note that we have Error['x'] = ... in some places in the code to prevent Terser from optimizing out some important calls.

dynamic

Hierarchy

Index

Constructors

constructor

  • new BreezeEnum(propertiesObj?: Object): BreezeEnum

Properties

_$typeName

_$typeName: string

Type of the enum; set in prototype of each enum

name

name: string

The name of this symbol

Methods

toJSON

  • toJSON(): { _$typeName: any; name: string }
  • Return enum name and symbol name

    Returns { _$typeName: any; name: string }

    • _$typeName: any
    • name: string

toString

  • toString(): string
  • Returns the string name of this Enum

    Returns string

Static contains

  • Returns whether an Enum contains a specified symbol.

    let symbol = DayOfWeek.Friday;
    if (DayOfWeek.contains(symbol)) {
        // do something
    }
    

    Parameters

    Returns boolean

    Whether this Enum contains the specified symbol.

Static fromName

  • fromName(name: string): any
  • Returns an Enum symbol given its name.

    let dayOfWeek = DayOfWeek.from("Thursday");
    // nowdayOfWeek === DayOfWeek.Thursday
    

    Parameters

    • name: string

      Name for which an enum symbol should be returned.

    Returns any

    The symbol that matches the name or 'undefined' if not found.

Static getNames

  • getNames(): string[]
  • Returns the names of all of the symbols contained within this Enum.

    let symbols = DayOfWeek.getNames();
    

    Returns string[]

    All of the names of the symbols contained within this Enum.

Static getSymbols

  • Returns all of the symbols contained within this Enum.

    let symbols = DayOfWeek.getSymbols();
    

    Returns BreezeEnum[]

    All of the symbols contained within this Enum.

Static resolveSymbols

  • resolveSymbols(): { name: string; symbol: BreezeEnum }[]
  • Seals this enum so that no more symbols may be added to it. This should only be called after all symbols have already been added to the Enum. This method also sets the 'name' property on each of the symbols.

    DayOfWeek.resolveSymbols();
    

    Returns { name: string; symbol: BreezeEnum }[]

Generated using TypeDoc