-
Notifications
You must be signed in to change notification settings - Fork 95
/
converter.js
47 lines (43 loc) · 1.21 KB
/
converter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use strict';
var converter = {
valueToPosition: function (value, valuesArray, sliderLength) {
var arrLength;
var index = valuesArray.indexOf(value);
if (index === -1) {
console.log('Invalid value, array does not contain: ', value)
return null;
} else {
arrLength = valuesArray.length - 1;
return sliderLength * index / arrLength;
}
},
positionToValue: function (position, valuesArray, sliderLength) {
var arrLength;
var index;
if ( position < 0 || sliderLength < position ) {
console.log('invalid position: ', position);
return null;
} else {
arrLength = valuesArray.length - 1;
index = arrLength * position / sliderLength;
return valuesArray[Math.round(index)];
}
},
createArray: function (start, end, step) {
var i;
var length;
var direction = start - end > 0 ? -1 : 1;
var result = [];
if (!step) {
console.log('invalid step: ', step);
return result;
} else {
length = Math.abs((start - end)/step) + 1;
for (i=0 ; i<length ; i++){
result.push(start + i * Math.abs(step)*direction);
}
return result;
}
}
};
module.exports = converter;