cartodb-4.42/lib/assets/javascripts/builder/value-objects/distance-converter.js
2024-04-06 05:25:13 +00:00

49 lines
1.3 KiB
JavaScript
Executable File

var _ = require('underscore');
/**
* Value object to calcualte distances
*/
module.exports = {
OPTIONS: [
{
distance: 'meters',
metersPerDistance: 1
}, {
distance: 'kilometers',
metersPerDistance: 1000
}, {
distance: 'miles',
metersPerDistance: 1609.34
}
],
/**
* @param {Number} val - e.g. 3
* @param {String} distance - e.g. 'kilometers'
* @param {Float} e.g. 3000
*/
toMeters: function (val, distance) {
val = parseFloat(val);
if (isNaN(val)) throw new Error('val (1st arg) is required, expected a parseable number or float');
var option = _.find(this.OPTIONS, function (d) {
return d.distance === distance;
});
if (!option) throw new Error(distance + ' (2nd arg) is not a valid distance value, expects one of ' + _.pluck(this.OPTIONS, 'distance').join(', '));
return val * option.metersPerDistance;
},
toDistance: function (meters, distance) {
meters = parseFloat(meters);
if (isNaN(meters)) throw new Error('meters (1st arg) is required');
var option = _.find(this.OPTIONS, function (d) {
return d.distance === distance;
});
if (!option) throw new Error(distance + ' (2nd arg) is not a valid distance value, expects one of ' + _.pluck(this.OPTIONS, 'distance').join(', '));
return meters / option.metersPerDistance;
}
};