Initial commit
This commit is contained in:
66
lib/assets/javascripts/builder/value-objects/analysis-node-ids.js
Executable file
66
lib/assets/javascripts/builder/value-objects/analysis-node-ids.js
Executable file
@@ -0,0 +1,66 @@
|
||||
var _ = require('underscore');
|
||||
|
||||
var SOURCE_ID_REGEX = /^([a-z]+)(\d*)$/; // matches a string with one or more letters + numbers, e.g. 'b2'
|
||||
|
||||
var newId = function (id, seqChange) {
|
||||
if (!id || !id.match) throw new Error('invalid id');
|
||||
|
||||
var matches = id.match(SOURCE_ID_REGEX);
|
||||
matches[2] = matches[2] || -1;
|
||||
|
||||
if (matches && matches.length === 3) {
|
||||
// letter + next number
|
||||
var letter = matches[1];
|
||||
var seq = parseInt(matches[2], 10) + seqChange;
|
||||
return letter + Math.max(seq, 0);
|
||||
} else {
|
||||
throw new Error('invalid id');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Encapsulates logic related to the nodes' IDs.
|
||||
*/
|
||||
module.exports = {
|
||||
|
||||
/**
|
||||
* Get next id in sequence of given source id.
|
||||
* @param {String} id, e.g. 'c2'
|
||||
* @return {String} e.g. 'c3'
|
||||
*/
|
||||
next: function (id) {
|
||||
return newId(id, 1);
|
||||
},
|
||||
|
||||
prev: function (id) {
|
||||
return newId(id, -1);
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the letter representation.
|
||||
* @param {String, Object} sourceId or backbone model that have id. e.g. 'c2'
|
||||
* @return {String} e.g. 'c' or an empty string if there is no letter in the given id
|
||||
*/
|
||||
letter: function (sourceId) {
|
||||
if (!sourceId || !_.isString(sourceId)) return '';
|
||||
var match = sourceId.match(/^([a-z]+)/);
|
||||
return _.isArray(match) && match[0] || '';
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the sequence representation.
|
||||
* @param {String, Object} sourceId or backbone model that have id. e.g. 'c2'
|
||||
* @return {Number} e.g. 2 or undefined if there is no sequence
|
||||
*/
|
||||
sequence: function (sourceId) {
|
||||
if (!sourceId || !_.isString(sourceId)) return;
|
||||
var match = sourceId.match(/(\d+$)/);
|
||||
return _.isArray(match) && match[0] || undefined;
|
||||
},
|
||||
|
||||
changeLetter: function (id, newLetter) {
|
||||
var seq = id.match(/(\d+)$/)[0];
|
||||
return newLetter + seq;
|
||||
}
|
||||
|
||||
};
|
||||
48
lib/assets/javascripts/builder/value-objects/distance-converter.js
Executable file
48
lib/assets/javascripts/builder/value-objects/distance-converter.js
Executable file
@@ -0,0 +1,48 @@
|
||||
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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user