projects/wms-framework/src/lib/baseframework/typeconversion.ts
Conversion class
Methods |
|
Static ChangeType | |||||||||
ChangeType(obj: unknown, type: RuntimeTypeInfo)
|
|||||||||
Changes the type of an element
Parameters :
Returns :
unknown
{unknown} |
Private Static GetBuiltinConverters |
GetBuiltinConverters()
|
Built-in types registered converters
Returns :
any
|
Static isDBNull | ||||||
isDBNull(dbnull: any)
|
||||||
Validates if the object is DBNull type
Parameters :
Returns :
boolean
True if the object is DBNull type. Otherwise false. |
Static toDecimal | ||||||
toDecimal(value: any)
|
||||||
Converts a string value to a number.
Parameters :
Returns :
number
Returns the number value. If the value cannot be converted, NaN is returned. |
Static toSingle | ||||||
toSingle(value: any)
|
||||||
Converts a string value to a number single precision.
Parameters :
Returns :
number
Returns the number value. If the value cannot be converted, NaN is returned. |
import { stringy } from '../utils/supportFunctions';
import { DBNull } from './DBNull';
import { ReflectionHelper, RuntimeTypeInfo } from './ReflectionSupport';
/**
* Conversion class
*
* @export
* @class Convert
*/
export class Convert {
/**
* Built-in types registered converters
*
* @private
* @static
* @memberof Convert
*/
private static GetBuiltinConverters() {
return new Map<string, any>([['System.Int32', toInt32]]);
}
/**
* Changes the type of an element
*
* @static
* @param {unknown} obj
* @param {RuntimeTypeInfo} type
* @return {*} {unknown}
* @memberof Convert
*/
public static ChangeType(obj: unknown, type: RuntimeTypeInfo): unknown {
if (obj === null) {
// This may not be true for non nullable types
return obj;
}
if (type.JSType === String) {
return stringy(obj);
}
const objType = ReflectionHelper.getTypeInfo(obj);
if (objType === type) {
return obj;
} else if (objType.JSType === String && type.JSType === Number) {
const parsed = parseFloat(obj as string);
if (Number.isNaN(parsed)) {
throw new Error('Cannot convert value');
} else {
return parsed;
}
} else if (objType.JSType === Boolean && type.JSType === Number) {
return obj ? 1 : 0;
} else if (typeof type.Name === 'string') {
const converter = Convert.GetBuiltinConverters().get(type.Name);
return converter?.(obj, objType) ?? obj;
}
return obj;
}
/**
* Validates if the object is DBNull type
* @param obj
* @returns True if the object is DBNull type. Otherwise false.
*/
public static isDBNull(dbnull: any): boolean {
return dbnull === DBNull.value;
}
/**
* Converts a string value to a number.
* @param value
* @returns Returns the number value. If the value cannot be converted, NaN is returned.
*/
public static toDecimal(value: any): number {
return parseFloat(value);
}
/**
* Converts a string value to a number single precision.
* @param value
* @returns Returns the number value. If the value cannot be converted, NaN is returned.
*/
public static toSingle(value: any): number {
return parseFloat(value);
}
}
/**
* Converts a string value to a number with built-in support or Int32.
* @param value
* @param objectType
* @returns Returns the number value. If the value cannot be converted, an exception is throw.
*/
export function toInt32(value: any, objectType: any): number {
if (objectType.JSType === Boolean) {
return value ? 1 : 0;
} else {
const parsed = parseFloat(value as string);
if (
Number.isNaN(parsed) ||
!(-2147483648 <= parsed && parsed <= 2147483647)
) {
throw new Error(
'Cannot convert value to int32, not valid value between Int32.MinValue and Int32.MaxValue'
);
} else {
return parsed;
}
}
}
/**
* Converts a string to a number with `Byte` limits.
*
* @export
* @param {string} value
* @param {number} base
* @return {*}
*/
export function toByte(value: string, base: number) {
/* istanbul ignore else */
if (base != 16) {
throw new Error('Only base 16 is implemented');
}
/* istanbul ignore else */
if (value === '') {
throw new Error('Argument out of range');
}
/* istanbul ignore else */
if (!/^(0x|0X)?[\da-fA-F]+$/g.test(value)) {
throw new Error(
`Format error: '${value}' format is incorrect for a base 16 number.`
);
}
const byteValue = parseInt(value, base);
/* istanbul ignore else */
if (byteValue > 255) {
throw new Error(
`Overflow: '${value}' can not be converted to a valid byte between 0 and 255.`
);
}
return byteValue;
}