Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Aug 9, 2019
1 parent f459c36 commit add517a
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/enum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
/** A symbol indicating that an object is an enumeration. */
const isEnum: symbol = Symbol('Enum');

/**
* Returns a value indicating whether the specified enumeration is a TypeScript one.
* @param enumType An enumerated type.
* @return `true` if the specified enumeration is a TypeScript one, otherwise `false`.
*/
function hasEnumSymbol(enumType: object): boolean {
return Reflect.has(enumType, isEnum) && (Reflect.get(enumType, isEnum) === true);
}

/**
* Returns a value indicating whether the specified enumeration is a string one.
* @param enumType An enumerated type.
* @return `true` if the specified enumeration is a string one, otherwise `false`.
*/
function isStringEnum(enumType: object): boolean {
return Object.values(enumType).every(value => typeof value == 'string');
}

/** Provides helper methods for enumerations. */
export abstract class Enum {

Expand Down Expand Up @@ -59,7 +77,7 @@ export abstract class Enum {
* @typeparam T The type of the specified enumeration.
*/
static entries<T extends object>(enumType: T): Array<[string, T[keyof T]]> {
return Enum._hasEnumSymbol(enumType) || Enum._isStringEnum(enumType)
return hasEnumSymbol(enumType) || isStringEnum(enumType)
? Object.entries(enumType)
: Enum.names(enumType).map(name => [name, Reflect.get(enumType, name)]);
}
Expand Down Expand Up @@ -104,7 +122,7 @@ export abstract class Enum {
* @return An array that contains the names of the constants in the specified enumeration.
*/
static names(enumType: object): string[] {
return Enum._hasEnumSymbol(enumType) || Enum._isStringEnum(enumType)
return hasEnumSymbol(enumType) || isStringEnum(enumType)
? Object.keys(enumType)
: Object.values(enumType).filter(value => typeof value == 'string');
}
Expand All @@ -116,28 +134,10 @@ export abstract class Enum {
* @typeparam T The type of the specified enumeration.
*/
static values<T extends object>(enumType: T): Array<T[keyof T]> {
return Enum._hasEnumSymbol(enumType) || Enum._isStringEnum(enumType)
return hasEnumSymbol(enumType) || isStringEnum(enumType)
? Object.values(enumType)
: Object.values(enumType).filter(value => typeof value == 'number');
}

/**
* Returns a value indicating whether the specified enumeration is a TypeScript one.
* @param enumType An enumerated type.
* @return `true` if the specified enumeration is a TypeScript one, otherwise `false`.
*/
private static _hasEnumSymbol(enumType: object): boolean {
return Reflect.has(enumType, isEnum) && (Reflect.get(enumType, isEnum) === true);
}

/**
* Returns a value indicating whether the specified enumeration is a string one.
* @param enumType An enumerated type.
* @return `true` if the specified enumeration is a string one, otherwise `false`.
*/
private static _isStringEnum(enumType: object): boolean {
return Object.values(enumType).every(value => typeof value == 'string');
}
}

/**
Expand Down

0 comments on commit add517a

Please sign in to comment.