From da5616d886e517647d4b93ca218a21a930295ac8 Mon Sep 17 00:00:00 2001 From: German Toro del Valle Date: Wed, 14 Sep 2016 09:17:37 +0200 Subject: [PATCH] Fixing the textRotationInterpolator accumunation and async's RangeError error --- CHANGES_NEXT_RELEASE | 2 ++ lib/interpolators/textRotationInterpolator.js | 10 ++++++---- .../fiwareDeviceSimulatorValidator.js | 20 +++++++++---------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index e69de29..6262896 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -0,0 +1,2 @@ +- [BUG] Fixing the text-rotation-interpolator probability accumulation +- [BUG] Fixing the async's "RangeError: Maximum call stack size exceeded" error diff --git a/lib/interpolators/textRotationInterpolator.js b/lib/interpolators/textRotationInterpolator.js index 3e1e743..2146c49 100644 --- a/lib/interpolators/textRotationInterpolator.js +++ b/lib/interpolators/textRotationInterpolator.js @@ -90,15 +90,17 @@ function sortTextArray(entryA, entryB) { * @return {String} The interpolated text */ function getProbabilisticText(textProbabilities) { + var accumulated = textProbabilities[0][0]; var randomNumber = Math.random() * 100; - if (randomNumber <= textProbabilities[0][0]) { + if (randomNumber <= accumulated) { return textProbabilities[0][1]; } - for (var ii = 1; ii < textProbabilities.length - 1; ii++) { - if (randomNumber > textProbabilities[ii][0] && - randomNumber <= (textProbabilities[ii][0] + textProbabilities[ii + 1][0])) { + for (var ii = 0; ii < textProbabilities.length - 1; ii++) { + if (randomNumber > accumulated && + randomNumber <= (accumulated + textProbabilities[ii + 1][0])) { return textProbabilities[ii + 1][1]; } + accumulated += textProbabilities[ii + 1][0]; } return textProbabilities[textProbabilities.length - 1][1]; } diff --git a/lib/validators/fiwareDeviceSimulatorValidator.js b/lib/validators/fiwareDeviceSimulatorValidator.js index e031cdd..ffde67b 100644 --- a/lib/validators/fiwareDeviceSimulatorValidator.js +++ b/lib/validators/fiwareDeviceSimulatorValidator.js @@ -66,7 +66,7 @@ function validateDomain(simulationConfiguration, callback) { 'information (the \'domain.subservice\' property is mandatory if \'entities\' are included)')); } } - callback(); + process.nextTick(callback); } /** @@ -103,7 +103,7 @@ function validateContextBrokerConfiguration(simulationConfiguration, callback) { 'configuration information (\'' + simulationConfiguration.contextBroker.ngsiVersion + '\') is not supported')); } } - callback(); + process.nextTick(callback); } /** @@ -134,7 +134,7 @@ function validateAuthenticationConfiguration(simulationConfiguration, callback) 'configuration information (the \'authentication.password\' property is mandatory)')); } } - callback(); + process.nextTick(callback); } /** @@ -375,7 +375,7 @@ function validateIoTAConfiguration(simulationConfiguration, callback) { } } } - callback(); + process.nextTick(callback); } /** @@ -479,7 +479,7 @@ function validateValue(value, attributeType, attributeIndex, parentType, parentI } } } - callback(); + process.nextTick(callback); } /** @@ -514,7 +514,7 @@ function validateMetadata(parentType, parentIndex, attributeStr, attributeIndex, attributeIndex + ' including a metadata entry at array index position ' + metadataIndex + ' missing the value property')); } - callback(); + process.nextTick(callback); } /** @@ -580,7 +580,7 @@ function validateAttribute(attributeType, parentType, parentIndex, attribute, at attributeIndex), callback); } } else { - callback(); + process.nextTick(callback); } } }); @@ -616,7 +616,7 @@ function validateAttributes(attributes, attributesType, parentType, parentIndex, attributeStr + ' property which is not an array')); } } else { - callback(); + process.nextTick(callback); } } @@ -664,7 +664,7 @@ function validateDevicesConfiguration(simulationConfiguration, callback) { 'information available (at least one of them is mandatory)')); } if (!simulationConfiguration.devices) { - return callback(); + return process.nextTick(callback); } if (!Array.isArray(simulationConfiguration.devices)) { return callback(new fdsErrors.SimulationConfigurationNotValid('The devices ' + @@ -714,7 +714,7 @@ function validateEntitiesConfiguration(simulationConfiguration, callback) { 'information available (at least one of them is mandatory)')); } if (!simulationConfiguration.entities) { - return callback(); + return process.nextTick(callback); } if (!Array.isArray(simulationConfiguration.entities)) { return callback(new fdsErrors.SimulationConfigurationNotValid('The entities ' +