mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-12 02:43:50 +00:00
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:
committed by
GitHub
parent
78d5d442cf
commit
a01046b479
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user