Added assertions utility for error handling and validation
This commit is contained in:
2
index.js
2
index.js
@@ -12,6 +12,7 @@ const outputUtils = require('./src/helper/outputUtils.js');
|
||||
const logger = require('./src/helper/logger.js');
|
||||
const validation = require('./src/helper/validationUtils.js');
|
||||
const configUtils = require('./src/helper/configUtils.js');
|
||||
const assertions = require('./src/helper/assertionUtils.js')
|
||||
|
||||
// Domain-specific modules
|
||||
const { MeasurementContainer } = require('./src/measurements/index.js');
|
||||
@@ -34,6 +35,7 @@ module.exports = {
|
||||
configUtils,
|
||||
logger,
|
||||
validation,
|
||||
assertions,
|
||||
MeasurementContainer,
|
||||
nrmse,
|
||||
state,
|
||||
|
||||
29
src/helper/assertionUtils.js
Normal file
29
src/helper/assertionUtils.js
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* @file assertionUtils.js
|
||||
*
|
||||
* Utility functions for assertions and throwing errors in EVOLV.
|
||||
*
|
||||
* @description This module provides functions to assert conditions and throw errors when those conditions are not met.
|
||||
* @exports ValidationUtils
|
||||
*/
|
||||
|
||||
class Assertions {
|
||||
/**
|
||||
* Assert that no NaN values are present in an array.
|
||||
* @param {Array} arr - The array to check for NaN values.
|
||||
* @param {string} label - Array label to indicate where the error occurs.
|
||||
*/
|
||||
assertNoNaN(arr, label = "array") {
|
||||
if (Array.isArray(arr)) {
|
||||
for (const el of arr) {
|
||||
assertNoNaN(el, label);
|
||||
}
|
||||
} else {
|
||||
if (Number.isNaN(arr)) {
|
||||
throw new Error(`NaN detected in ${label}!`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Assertions;
|
||||
Reference in New Issue
Block a user