Skip to content

breeze-client / BreezeEnum

Class: BreezeEnum ​

Defined in: src/core/enum.ts:55

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.

ts
  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");
    });
});

Each enum module calls resolveSymbols() at import time. Until v3 those calls were written as Error['x'] = MyEnum.resolveSymbols(), to keep a bundler from discarding a call whose result nothing uses. That is no longer needed: package.json declares every module but the entity-graph mixin side-effect free, so a module is kept exactly when something uses one of its exports - and whatever needs the resolved symbols does. See the sideEffects section of CHANGES-DEV.md.

Extended by ​

Constructors ​

Constructor ​

new BreezeEnum(propertiesObj?): BreezeEnum

Defined in: src/core/enum.ts:72

Creates a symbol, copying each property of propertiesObj onto it. Used to declare the symbols of an enum, as static members of its class; its name is set when the class calls BreezeEnum.resolveSymbols.

Parameters ​

propertiesObj? ​

Object

Returns ​

BreezeEnum

Properties ​

_$typeName ​

_$typeName: string

Defined in: src/core/enum.ts:60

Type of the enum; set in prototype of each enum


name ​

name: string

Defined in: src/core/enum.ts:58

The name of this symbol

Methods ​

toJSON() ​

toJSON(): object

Defined in: src/core/enum.ts:180

Return enum name and symbol name

Returns ​

_$typeName ​

_$typeName: any

The name of the enum, such as "EntityState".

name ​

name: string

The name of the symbol, such as "Added".


toString() ​

toString(): string

Defined in: src/core/enum.ts:175

Returns the string name of this Enum

Returns ​

string


contains() ​

static contains(sym): boolean

Defined in: src/core/enum.ts:154

Returns whether an Enum contains a specified symbol.

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

Parameters ​

sym ​

BreezeEnum

Object or symbol to test.

Returns ​

boolean

Whether this Enum contains the specified symbol.


fromName() ​

static fromName(name): any

Defined in: src/core/enum.ts:113

Returns an Enum symbol given its name.

ts
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.


getNames() ​

static getNames(): string[]

Defined in: src/core/enum.ts:100

Returns the names of all of the symbols contained within this Enum.

ts
let symbols = DayOfWeek.getNames();

Returns ​

string[]

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


getSymbols() ​

static getSymbols<T>(this): InstanceType<T>[]

Defined in: src/core/enum.ts:86

Returns all of the symbols contained within this Enum.

ts
let symbols = DayOfWeek.getSymbols();

Type Parameters ​

T ​

T extends typeof BreezeEnum

Parameters ​

this ​

T

Returns ​

InstanceType<T>[]

All of the symbols contained within this Enum.


resolveSymbols() ​

static resolveSymbols(): object[]

Defined in: src/core/enum.ts:124

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.

ts
DayOfWeek.resolveSymbols();

Returns ​

Released under the MIT License.