From bb08cc5a845ae96734a373e64507c46fc47393f0 Mon Sep 17 00:00:00 2001 From: German Toro del Valle Date: Tue, 27 Sep 2016 19:25:03 +0200 Subject: [PATCH] Interporlator cache mixing values if same interpolator and distinct spec --- CHANGES_NEXT_RELEASE | 1 + .../dateIncrementInterpolator.js | 30 ++-- .../multilinePositionInterpolator.js | 115 ++++++++-------- lib/interpolators/stepAfterInterpolator.js | 36 ++--- lib/interpolators/stepBeforeInterpolator.js | 36 ++--- lib/interpolators/textRotationInterpolator.js | 128 +++++++++--------- 6 files changed, 174 insertions(+), 172 deletions(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index e69de29..13aae45 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -0,0 +1 @@ +- [BUG] Caching of interpolators is mixing values (#96) diff --git a/lib/interpolators/dateIncrementInterpolator.js b/lib/interpolators/dateIncrementInterpolator.js index dbdbaed..ec518ae 100644 --- a/lib/interpolators/dateIncrementInterpolator.js +++ b/lib/interpolators/dateIncrementInterpolator.js @@ -26,8 +26,6 @@ var ROOT_PATH = require('app-root-path'); var fdsErrors = require(ROOT_PATH + '/lib/errors/fdsErrors'); -var interpolationObject; - /** * Checks if the provided interpolation object is a valid once * @param {Object} interpolationObject The interpolation object @@ -39,21 +37,23 @@ function isValidInterpolationObject(interpolationObject) { ((interpolationObject.increment !== undefined) && (typeof interpolationObject.increment === 'number'))); } -/** - * The date increment interpolator function - * @return {[type]} [description] - */ -function dateIncrementInterpolator() { - var originDate; - if (interpolationObject.origin === 'now') { - originDate = new Date(); - } else { - originDate = new Date(interpolationObject.origin); +module.exports = function(interpolationObjectOrSpec) { + var interpolationObject; + + /** + * The date increment interpolator function + * @return {[type]} [description] + */ + function dateIncrementInterpolator() { + var originDate; + if (interpolationObject.origin === 'now') { + originDate = new Date(); + } else { + originDate = new Date(interpolationObject.origin); + } + return new Date(originDate.getTime() + (interpolationObject.increment * 1000)).toISOString(); } - return new Date(originDate.getTime() + (interpolationObject.increment * 1000)).toISOString(); -} -module.exports = function(interpolationObjectOrSpec) { if ((typeof interpolationObjectOrSpec === 'object')) { if (isValidInterpolationObject(interpolationObjectOrSpec)) { interpolationObject = interpolationObjectOrSpec; diff --git a/lib/interpolators/multilinePositionInterpolator.js b/lib/interpolators/multilinePositionInterpolator.js index 19cfca4..7f3b178 100644 --- a/lib/interpolators/multilinePositionInterpolator.js +++ b/lib/interpolators/multilinePositionInterpolator.js @@ -29,72 +29,73 @@ var turfLineDistance = require('turf-line-distance'); var turfAlong = require('turf-along'); var fdsErrors = require(ROOT_PATH + '/lib/errors/fdsErrors'); -var interpolationObject; -var distanceUnits; -var line; -var lineDistance; +module.exports = function(interpolationObjectOrSpec){ + var interpolationObject, + distanceUnits, + line, + lineDistance; -/** - * Validates the coordinates property value - * @param {Array} coordinates The coordinates array - * @return {Boolean} True if the coordinates array is valid, false otherwise - */ -function isValidCoordinates(coordinates) { - if (!Array.isArray(coordinates)) { - return false; - } - for (var ii = 0; ii < coordinates.length; ii++) { - if (!Array.isArray(coordinates[ii] || (coordinates[ii].length !== 2) || (typeof coordinates[ii][0] !== 'number') || - (typeof coordinates[ii][1]))) { + /** + * Validates the coordinates property value + * @param {Array} coordinates The coordinates array + * @return {Boolean} True if the coordinates array is valid, false otherwise + */ + function isValidCoordinates(coordinates) { + if (!Array.isArray(coordinates)) { return false; } + for (var ii = 0; ii < coordinates.length; ii++) { + if (!Array.isArray(coordinates[ii] || (coordinates[ii].length !== 2) || + (typeof coordinates[ii][0] !== 'number') || (typeof coordinates[ii][1]))) { + return false; + } + } + line = turfLineString(coordinates); + lineDistance = turfLineDistance(line, distanceUnits); + return true; } - line = turfLineString(coordinates); - lineDistance = turfLineDistance(line, distanceUnits); - return true; -} -/** - * Validates the interpolation object - * @param {Object} interpolationObject The interpolation object to validate - * @return {Boolean} True if the interpolation object is valid, false otherwise - */ -function isValidInterpolationObject(interpolationObject) { - if (!interpolationObject.coordinates || !isValidCoordinates(interpolationObject.coordinates)) { - return false; - } - if (!interpolationObject.speed || (typeof interpolationObject.speed !== 'object') || - (typeof interpolationObject.speed.value !== 'number') || - ((interpolationObject.speed.units !== 'km/h') && (interpolationObject.speed.units !== 'mi/h'))) { - return false; - } - distanceUnits = (interpolationObject.speed.units === 'km/h' ? 'kilometers' : 'miles'); - if (!interpolationObject.time || (typeof interpolationObject.time.from !== 'number') || - (typeof interpolationObject.time.to !== 'number')) { - return false; + /** + * Validates the interpolation object + * @param {Object} interpolationObject The interpolation object to validate + * @return {Boolean} True if the interpolation object is valid, false otherwise + */ + function isValidInterpolationObject(interpolationObject) { + if (!interpolationObject.coordinates || !isValidCoordinates(interpolationObject.coordinates)) { + return false; + } + if (!interpolationObject.speed || (typeof interpolationObject.speed !== 'object') || + (typeof interpolationObject.speed.value !== 'number') || + ((interpolationObject.speed.units !== 'km/h') && (interpolationObject.speed.units !== 'mi/h'))) { + return false; + } + distanceUnits = (interpolationObject.speed.units === 'km/h' ? 'kilometers' : 'miles'); + if (!interpolationObject.time || (typeof interpolationObject.time.from !== 'number') || + (typeof interpolationObject.time.to !== 'number')) { + return false; + } + return true; } - return true; -} -/** - * Returns the new interpolated position for the passed decimal hours - * @param {Number} decimalHours The decimal hours - * @return {Object} The new interpolated position - */ -function multilinePositionInterpolator(decimalHours) { - var traveledDistance, - traveledDistanceModulus; - if (decimalHours < interpolationObject.time.from) { - return turfAlong(line, 0, distanceUnits).geometry; - } else if (decimalHours > interpolationObject.time.to) { - traveledDistance = interpolationObject.speed.value * (interpolationObject.time.to - interpolationObject.time.from); + /** + * Returns the new interpolated position for the passed decimal hours + * @param {Number} decimalHours The decimal hours + * @return {Object} The new interpolated position + */ + function multilinePositionInterpolator(decimalHours) { + var traveledDistance, + traveledDistanceModulus; + if (decimalHours < interpolationObject.time.from) { + return turfAlong(line, 0, distanceUnits).geometry; + } else if (decimalHours > interpolationObject.time.to) { + traveledDistance = interpolationObject.speed.value * + (interpolationObject.time.to - interpolationObject.time.from); + } + traveledDistance = interpolationObject.speed.value * (decimalHours - interpolationObject.time.from); + traveledDistanceModulus = traveledDistance % lineDistance; + return turfAlong(line, traveledDistanceModulus, distanceUnits).geometry; } - traveledDistance = interpolationObject.speed.value * (decimalHours - interpolationObject.time.from); - traveledDistanceModulus = traveledDistance % lineDistance; - return turfAlong(line, traveledDistanceModulus, distanceUnits).geometry; -} -module.exports = function(interpolationObjectOrSpec){ if (typeof interpolationObjectOrSpec === 'object') { interpolationObject = interpolationObjectOrSpec; if (!isValidInterpolationObject(interpolationObject)) { diff --git a/lib/interpolators/stepAfterInterpolator.js b/lib/interpolators/stepAfterInterpolator.js index c4f4abb..1ce6b67 100644 --- a/lib/interpolators/stepAfterInterpolator.js +++ b/lib/interpolators/stepAfterInterpolator.js @@ -26,8 +26,6 @@ var ROOT_PATH = require('app-root-path'); var fdsErrors = require(ROOT_PATH + '/lib/errors/fdsErrors'); -var interpolationArray; - /** * Validates the entries of an interpolation array * @param {Array} entry The entry to Validates @@ -58,26 +56,28 @@ function sortInterpolationArray(entryA, entryB) { return entryA[0] - entryB[0]; } -/** - * Step-after interpolation function - * @param {Number} input The input to return its associated interporlated value - * @return {Number} The interpolated value associated to the provided input - */ -function stepAfterInterpolator(input) { - if (input >= interpolationArray[interpolationArray.length - 1][0]) { - return interpolationArray[interpolationArray.length - 1][1]; - } else if (input <= interpolationArray[0][0]) { - return interpolationArray[0][1]; - } else { - for (var ii = 0; ii < interpolationArray.length - 1; ii++) { - if (interpolationArray[ii][0] < input && interpolationArray[ii + 1][0] > input) { - return interpolationArray[ii][1]; +module.exports = function(interpolationArrayOrSpec) { + var interpolationArray; + + /** + * Step-after interpolation function + * @param {Number} input The input to return its associated interporlated value + * @return {Number} The interpolated value associated to the provided input + */ + function stepAfterInterpolator(input) { + if (input >= interpolationArray[interpolationArray.length - 1][0]) { + return interpolationArray[interpolationArray.length - 1][1]; + } else if (input <= interpolationArray[0][0]) { + return interpolationArray[0][1]; + } else { + for (var ii = 0; ii < interpolationArray.length - 1; ii++) { + if (interpolationArray[ii][0] < input && interpolationArray[ii + 1][0] > input) { + return interpolationArray[ii][1]; + } } } } -} -module.exports = function(interpolationArrayOrSpec) { if (Array.isArray(interpolationArrayOrSpec) && isValidInterpolationArray(interpolationArrayOrSpec)) { interpolationArray = interpolationArrayOrSpec; } else { diff --git a/lib/interpolators/stepBeforeInterpolator.js b/lib/interpolators/stepBeforeInterpolator.js index 25fc4f1..52362b3 100644 --- a/lib/interpolators/stepBeforeInterpolator.js +++ b/lib/interpolators/stepBeforeInterpolator.js @@ -26,8 +26,6 @@ var ROOT_PATH = require('app-root-path'); var fdsErrors = require(ROOT_PATH + '/lib/errors/fdsErrors'); -var interpolationArray; - /** * Validates the entries of an interpolation array * @param {Array} entry The entry to Validates @@ -58,26 +56,28 @@ function sortInterpolationArray(entryA, entryB) { return entryA[0] - entryB[0]; } -/** - * Step-before interpolation function - * @param {Number} input The input to return its associated interporlated value - * @return {Number} The interpolated value associated to the provided input - */ -function stepBeforeInterpolator(input) { - if (input >= interpolationArray[interpolationArray.length - 1][0]) { - return interpolationArray[interpolationArray.length - 1][1]; - } else if (input <= interpolationArray[0][0]) { - return interpolationArray[0][1]; - } else { - for (var ii = 0; ii < interpolationArray.length - 1; ii++) { - if (interpolationArray[ii][0] < input && interpolationArray[ii + 1][0] > input) { - return interpolationArray[ii + 1][1]; +module.exports = function(interpolationArrayOrSpec) { + var interpolationArray; + + /** + * Step-before interpolation function + * @param {Number} input The input to return its associated interporlated value + * @return {Number} The interpolated value associated to the provided input + */ + function stepBeforeInterpolator(input) { + if (input >= interpolationArray[interpolationArray.length - 1][0]) { + return interpolationArray[interpolationArray.length - 1][1]; + } else if (input <= interpolationArray[0][0]) { + return interpolationArray[0][1]; + } else { + for (var ii = 0; ii < interpolationArray.length - 1; ii++) { + if (interpolationArray[ii][0] < input && interpolationArray[ii + 1][0] > input) { + return interpolationArray[ii + 1][1]; + } } } } -} -module.exports = function(interpolationArrayOrSpec) { if (Array.isArray(interpolationArrayOrSpec) && isValidInterpolationArray(interpolationArrayOrSpec)) { interpolationArray = interpolationArrayOrSpec; } else { diff --git a/lib/interpolators/textRotationInterpolator.js b/lib/interpolators/textRotationInterpolator.js index 2146c49..570bc58 100644 --- a/lib/interpolators/textRotationInterpolator.js +++ b/lib/interpolators/textRotationInterpolator.js @@ -26,8 +26,6 @@ var ROOT_PATH = require('app-root-path'); var fdsErrors = require(ROOT_PATH + '/lib/errors/fdsErrors'); -var interpolationObject; - /** * Checkes if the text probability array is valid * @param {Array} textProbabilityArray The text probability array @@ -84,77 +82,79 @@ function sortTextArray(entryA, entryB) { return entryA[0] - entryB[0]; } -/** - * Returns the interpolated text from an text probabilities array - * @param {Array} textProbabilities The text probabilities array - * @return {String} The interpolated text - */ -function getProbabilisticText(textProbabilities) { - var accumulated = textProbabilities[0][0]; - var randomNumber = Math.random() * 100; - if (randomNumber <= accumulated) { - return textProbabilities[0][1]; - } - for (var ii = 0; ii < textProbabilities.length - 1; ii++) { - if (randomNumber > accumulated && - randomNumber <= (accumulated + textProbabilities[ii + 1][0])) { - return textProbabilities[ii + 1][1]; +module.exports = function(interpolationObjectOrSpec){ + var interpolationObject; + + /** + * Returns the interpolated text from an text probabilities array + * @param {Array} textProbabilities The text probabilities array + * @return {String} The interpolated text + */ + function getProbabilisticText(textProbabilities) { + var accumulated = textProbabilities[0][0]; + var randomNumber = Math.random() * 100; + if (randomNumber <= accumulated) { + return textProbabilities[0][1]; } - accumulated += 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]; } - return textProbabilities[textProbabilities.length - 1][1]; -} -/** - * Returns the interpolated value for certain entry in the selected units - * @param {Number} entry The entry to interpolated - * @return {String} The interpolated text - */ -function getInterpolatedText(entry) { - if (entry < interpolationObject.text[0][0]) { - return null; - } - for (var ii = 0; ii < interpolationObject.text.length - 1; ii++) { - if (entry >= interpolationObject.text[ii][0] && entry < interpolationObject.text[ii + 1][0]) { - if (typeof interpolationObject.text[ii][1] === 'string') { - return interpolationObject.text[ii][1]; - } else { - return getProbabilisticText(interpolationObject.text[ii][1]); + /** + * Returns the interpolated value for certain entry in the selected units + * @param {Number} entry The entry to interpolated + * @return {String} The interpolated text + */ + function getInterpolatedText(entry) { + if (entry < interpolationObject.text[0][0]) { + return null; + } + for (var ii = 0; ii < interpolationObject.text.length - 1; ii++) { + if (entry >= interpolationObject.text[ii][0] && entry < interpolationObject.text[ii + 1][0]) { + if (typeof interpolationObject.text[ii][1] === 'string') { + return interpolationObject.text[ii][1]; + } else { + return getProbabilisticText(interpolationObject.text[ii][1]); + } } } + if (typeof interpolationObject.text[interpolationObject.text.length - 1][1] === 'string') { + return interpolationObject.text[interpolationObject.text.length - 1][1]; + } else { + return getProbabilisticText(interpolationObject.text[interpolationObject.text.length - 1][1]); + } } - if (typeof interpolationObject.text[interpolationObject.text.length - 1][1] === 'string') { - return interpolationObject.text[interpolationObject.text.length - 1][1]; - } else { - return getProbabilisticText(interpolationObject.text[interpolationObject.text.length - 1][1]); - } -} -/** - * Returns the new interpolated position for the passed decimal hours - * @param {Number} decimalHours The decimal hours - * @return {String} The new interpolated text - */ -function textRotationInterpolator(date) { - switch (interpolationObject.units) { - case 'seconds': - return getInterpolatedText(date.getUTCSeconds()); - case 'minutes': - return getInterpolatedText(date.getUTCMinutes()); - case 'hours': - return getInterpolatedText(date.getUTCHours()); - case 'days': - return getInterpolatedText(date.getUTCDay()); - case 'dates': - return getInterpolatedText(date.getUTCDate()); - case 'months': - return getInterpolatedText(date.getUTCMonth()); - case 'years': - return getInterpolatedText(date.getUTCFullYear()); + /** + * Returns the new interpolated position for the passed decimal hours + * @param {Number} decimalHours The decimal hours + * @return {String} The new interpolated text + */ + function textRotationInterpolator(date) { + switch (interpolationObject.units) { + case 'seconds': + return getInterpolatedText(date.getUTCSeconds()); + case 'minutes': + return getInterpolatedText(date.getUTCMinutes()); + case 'hours': + return getInterpolatedText(date.getUTCHours()); + case 'days': + return getInterpolatedText(date.getUTCDay()); + case 'dates': + return getInterpolatedText(date.getUTCDate()); + case 'months': + return getInterpolatedText(date.getUTCMonth()); + case 'years': + return getInterpolatedText(date.getUTCFullYear()); + } } -} -module.exports = function(interpolationObjectOrSpec){ if (typeof interpolationObjectOrSpec === 'object') { interpolationObject = interpolationObjectOrSpec; if (!isValidInterpolationObject(interpolationObject)) {