I have the following function that takes out the rotation values from a css string and returns them as an array.
var extractRotationValues = function(code) {
var getAllIndexes = function(arr, val) {
var indexes = [],
i = -1;
//To understand the second parameter of indexOf function
//have a look at this : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
while ((i = arr.indexOf(val, i + 1)) != -1) {
indexes.push(i);
}
return indexes;
};
var indexes = [];
var closingIndexes = getAllIndexes(code, ")")
var values = [];
var rindexes = [];
rindexes.push(code.indexOf("X("), code.indexOf("Y("), code.indexOf("Z("));
//No value for rotateX was found so consider it 0
if (rindexes[0] == -1)
values.unshift(0);
else {
//We have a value for rotateX
//Where does rotateX start from => rindexes[0]
//Where does it end? =>closingIndexes[0]
var value = code.substr(rindexes[0], closingIndexes[0]).split("(")[1].split("deg)")[0];
values.push(value)
}
//No value for rotateY
if (rindexes[1] == -1)
values.splice(1, 0, 0);
else {
//We have a value for rotateY
//Where does rotateY start from => rindexes[1]
//Where does it end? =>closingIndexes[1]
values.push(code.substr(rindexes[1], closingIndexes[1]).split("(")[1].split("deg)")[0])
}
//No value for rotateZ
if (rindexes[2] == -1)
values.splice(2, 0, 0);
else {
//We have a value for rotateZ
//Where does rotateZ start from => rindexes[2]
//Where does it end? =>closingIndexes[2]
var zvalue = code.substr(rindexes[2], closingIndexes[2]).split("(")[1].split("deg)")[0]
values.push(zvalue);
}
return values;
};
console.log(extractRotationValues("rotateX(90deg) rotateY(87deg) rotateZ(22deg)"))
I was wondering.Is there anything that can be done in a better way here or something to improve?