Skip to content

Commit

Permalink
Merge pull request #97 from gtorodelvalle/bug/interpolator-cache-mixi…
Browse files Browse the repository at this point in the history
…ng-values

Interporlator cache mixing values if same interpolator and distinct spec
  • Loading branch information
gtorodelvalle authored Sep 27, 2016
2 parents eccf13c + bb08cc5 commit 06c8154
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 172 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [BUG] Caching of interpolators is mixing values (#96)
30 changes: 15 additions & 15 deletions lib/interpolators/dateIncrementInterpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
115 changes: 58 additions & 57 deletions lib/interpolators/multilinePositionInterpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
36 changes: 18 additions & 18 deletions lib/interpolators/stepAfterInterpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
36 changes: 18 additions & 18 deletions lib/interpolators/stepBeforeInterpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 06c8154

Please sign in to comment.