Feat/change event api (#553)

* feat: change attribute for given event 

---------

Co-authored-by: Ary <arylmoraesn@gmail.com>
Co-authored-by: arc-alex <ac@omnivox.dk>
This commit is contained in:
Alex Christoffer Rasmussen
2023-10-31 19:24:22 +01:00
committed by GitHub
parent 78d5d442cf
commit a01046b479
8 changed files with 173 additions and 9 deletions
+42
View File
@@ -0,0 +1,42 @@
/**
* @description Converts a value to a number if possible, throws otherwise
* @param {unknown} value - Value to be converted to a string.
* @returns {string} - The converted value as a string.
* @throws {Error} Throws an error if the value is null or undefined.
*/
export function coerceString(value: unknown): string {
if (value == null) {
throw new Error('Invalid value received');
}
return String(value);
}
/**
* @description Converts a value to a number if possible, throws otherwise
* @param {unknown} value - Value to be converted to a boolean.
* @returns {boolean} - The converted value as a boolean.
* @throws {Error} Throws an error if the value is null or undefined.
*/
export function coerceBoolean(value: unknown): boolean {
if (value == null) {
throw new Error('Invalid value received');
}
return Boolean(value);
}
/**
* @description Converts a value to a number if possible, throws otherwise
* @param {unknown} value - Value to be converted to a number.
* @returns {number} - The converted value as a number.
* @throws {Error} Throws an error if the value is null, undefined or not a valid number.
*/
export function coerceNumber(value: unknown): number {
if (value == null) {
throw new Error('Invalid value received');
}
const parsedValue = Number(value);
if (isNaN(parsedValue)) {
throw new Error('Invalid value received');
}
return parsedValue;
}