projects/wms-framework/src/lib/baseframework/SmEnumHelper.ts
Helper for manipulating enums
Methods |
|
Static enumParse | ||||||||||||
enumParse(enumDefinition: RuntimeTypeInfo, valueToParse: string, caseInsensitive?: boolean)
|
||||||||||||
Parses the value of an enum element for the given enum definition
Parameters :
Returns :
unknown
{unknown} |
Private Static getEnumInfo | ||||||
getEnumInfo(enumType: any)
|
||||||
Gets the enum info from the enum type
Parameters :
Returns :
ReifiedEnumInfo
|
Static getName |
getName(enumDefinition: any, enumValue: any)
|
Gets the name of the given enum entry
Returns :
string
{string} |
Static getNames | ||||||
getNames(enumType: unknown)
|
||||||
Gets the names of the elements of the given enum definition
Parameters :
Returns :
string[]
{string[]} |
Static getValues | ||||||
getValues(enumType: unknown)
|
||||||
Gets the values of the given enum definition
Parameters :
Returns :
unknown[]
{unknown[]} |
Static isEnumItemDefined | |||||||||
isEnumItemDefined(enumDefinition: RuntimeTypeInfo, value: unknown)
|
|||||||||
Verifies if the given element on an enum is defined
Parameters :
Returns :
boolean
{unknown} |
Static tryParse | |||||||||||||||
tryParse(enumDefinition: any, valueToParse: string, result: out<any>, caseInsensitive?: boolean)
|
|||||||||||||||
Type parameters :
|
|||||||||||||||
Tries to creates an enum element with an string representation of an enum entry.
Parameters :
Returns :
boolean
{boolean} |
Private Static trySpecialEnumParse | |||||||||||||||
trySpecialEnumParse(enumType: ReifiedEnumInfo, value: string, result: out<any>, caseInsensitive?: boolean)
|
|||||||||||||||
Try to apply an special conversion for a enum value
Parameters :
Returns :
boolean
|
import { out } from '../utils/RefOrOut';
import {
convertToBoolean,
ReifiedEnumInfo,
RuntimeTypeInfo,
} from './ReflectionSupport';
/**
* Helper for manipulating enums
*
* @export
* @class SmEnumHelper
*/
export class SmEnumHelper {
/**
* Parses the value of an enum element for the given enum definition
*
* @static
* @param {RuntimeTypeInfo} enumDefinition
* @param {string} valueToParse
* @param {boolean} [caseInsensitive]
* @return {*} {unknown}
* @memberof SmEnumHelper
*/
static enumParse(
enumDefinition: RuntimeTypeInfo,
valueToParse: string,
caseInsensitive?: boolean
): unknown {
let outValue: any = null;
if (
SmEnumHelper.tryParse(
enumDefinition,
valueToParse,
out((v) => (outValue = v)),
caseInsensitive
)
) {
return outValue;
}
throw new Error('Invalid argument value');
}
/**
* Tries to creates an enum element with an string representation of an enum entry.
*
* @static
* @template T
* @param {*} enumDefinition
* @param {string} valueToParse
* @param {out<any>} result
* @param {boolean} [caseInsensitive]
* @return {*} {boolean}
* @memberof SmEnumHelper
*/
static tryParse<T>(
enumDefinition: any,
valueToParse: string,
result: out<any>,
caseInsensitive?: boolean
): boolean {
const enumInfo = SmEnumHelper.getEnumInfo(enumDefinition);
if (
this.trySpecialEnumParse(enumInfo, valueToParse, result, caseInsensitive)
) {
return true;
}
const innerEnumDefinition = enumInfo.innerEnumDefinition;
if (caseInsensitive === true) {
const tmp = valueToParse.toLowerCase();
for (const key of Object.keys(innerEnumDefinition)) {
if (key.toLowerCase() === tmp) {
valueToParse = key;
break;
}
}
}
let lookupResult = innerEnumDefinition[valueToParse];
if (
typeof lookupResult === 'string' &&
lookupResult in innerEnumDefinition
) {
lookupResult = innerEnumDefinition[lookupResult];
}
if (typeof lookupResult !== 'undefined') {
result.value = lookupResult;
return true;
} else {
return false;
}
}
/**
* Gets the name of the given enum entry
*
* @static
* @param {*} enumDefinition
* @param {*} enumValue
* @return {*} {string}
* @memberof SmEnumHelper
*/
static getName(enumDefinition: any, enumValue: any): string {
if (enumDefinition instanceof ReifiedEnumInfo) {
enumDefinition = enumDefinition.innerEnumDefinition;
} else if (
enumDefinition instanceof RuntimeTypeInfo &&
enumDefinition.JSType instanceof ReifiedEnumInfo
) {
enumDefinition = enumDefinition.JSType.innerEnumDefinition;
}
return enumDefinition[enumValue] ?? '';
}
/**
* Verifies if the given element on an enum is defined
*
* @static
* @param {RuntimeTypeInfo} enumDefinition
* @param {unknown} value
* @return {*} {unknown}
* @memberof SmEnumHelper
*/
static isEnumItemDefined(
enumDefinition: RuntimeTypeInfo,
value: unknown
): boolean {
if (enumDefinition instanceof ReifiedEnumInfo) {
enumDefinition = enumDefinition.innerEnumDefinition;
return <any>value in enumDefinition;
} else if (
enumDefinition instanceof RuntimeTypeInfo &&
enumDefinition.JSType instanceof ReifiedEnumInfo
) {
enumDefinition = enumDefinition.JSType.innerEnumDefinition;
return <any>value in enumDefinition;
}
return false;
}
/**
* Gets the values of the given enum definition
*
* @static
* @param {unknown} enumType
* @return {*} {unknown[]}
* @memberof SmEnumHelper
*/
static getValues(enumType: unknown): unknown[] {
if (enumType instanceof ReifiedEnumInfo) {
enumType = enumType.innerEnumDefinition;
} else if (
enumType instanceof RuntimeTypeInfo &&
enumType.JSType instanceof ReifiedEnumInfo
) {
enumType = enumType.JSType.innerEnumDefinition;
}
const x = (prop) => enumType[prop];
return SmEnumHelper.getNames(enumType).map(x);
}
/**
* Gets the names of the elements of the given enum definition
*
* @static
* @param {unknown} enumType
* @return {*} {string[]}
* @memberof SmEnumHelper
*/
static getNames(enumType: unknown): string[] {
if (enumType instanceof ReifiedEnumInfo) {
enumType = enumType.innerEnumDefinition;
} else if (
enumType instanceof RuntimeTypeInfo &&
enumType.JSType instanceof ReifiedEnumInfo
) {
enumType = enumType.JSType.innerEnumDefinition;
}
const funcTemp = (x) => /^[A-Z_]/i.test(x);
return Object.keys(enumType).filter(funcTemp);
}
/**
* Gets the enum info from the enum type
*
* @private
* @static
* @param {*} enumType
* @returns {ReifiedEnumInfo}
* @memberof SmEnumHelper
*/
private static getEnumInfo(enumType: any): ReifiedEnumInfo {
if (
enumType instanceof RuntimeTypeInfo &&
enumType.JSType instanceof ReifiedEnumInfo
) {
return enumType.JSType;
} else {
return enumType;
}
}
/**
* Try to apply an special conversion for a enum value
*
* @private
* @static
* @param {ReifiedEnumInfo} enumType
* @param {string} value
* @param {out<any>} result
* @param {boolean} [caseInsensitive]
* @returns {boolean}
* @memberof SmEnumHelper
*/
private static trySpecialEnumParse(
enumType: ReifiedEnumInfo,
value: string,
result: out<any>,
caseInsensitive?: boolean
): boolean {
const valueToProcess = caseInsensitive ? value.toLowerCase() : value;
if (enumType.fullName == 'System.Windows.Visibility') {
if (isNaN(parseInt(value, 10))) {
result.value = convertToBoolean(valueToProcess);
} else {
result.value = parseInt(value, 10) === 0;
}
return true;
}
return false;
}
}