From 950ca2b6b4e91b37479aee90bff74b02c16f130e Mon Sep 17 00:00:00 2001 From: "p.vanderwilt" Date: Tue, 8 Jul 2025 12:57:22 +0200 Subject: [PATCH] Added assertions utility for error handling and validation --- index.js | 2 ++ src/helper/assertionUtils.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/helper/assertionUtils.js diff --git a/index.js b/index.js index dded213..54bc8de 100644 --- a/index.js +++ b/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, diff --git a/src/helper/assertionUtils.js b/src/helper/assertionUtils.js new file mode 100644 index 0000000..2ec2645 --- /dev/null +++ b/src/helper/assertionUtils.js @@ -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; \ No newline at end of file