regex improvement for DurationMatcher
Created by: zutto
So, browsing the code, I came across this small problem with regex results. https://github.com/Dash-Industry-Forum/dash.js/blob/master/src/dash/parser/matchers/DurationMatcher.js#L69
All of the parseFloats are ran on a strings matched in the regex, instead of the actual floats. Now the parseFloat deals with these just fine, but this probably should be improved.
Demo of the issue, and how parseFloat currently as of this day deals with this just fine:
re=/^([-])?P(([\d.]*)Y)?(([\d.]*)M)?(([\d.]*)D)?T?(([\d.]*)H)?(([\d.]*)M)?(([\d.]*)S)?/;
matched = re.exec("P10Y10M10DT10H10M10.1S");
console.log("matched[2]:", matched[2], "parseFloat:", parseFloat(matched[2]));
console.log("matched[3]:", matched[3], "parseFloat:", parseFloat(matched[3]));
Diff patch for someone who interested to run tests & contribute this to the codebase.
--- a/src/dash/parser/matchers/DurationMatcher.js
+++ b/src/dash/parser/matchers/DurationMatcher.js
@@ -66,12 +66,12 @@ class DurationMatcher extends BaseMatcher {
str => {
//str = "P10Y10M10DT10H10M10.1S";
const match = durationRegex.exec(str);
- let result = (parseFloat(match[2] || 0) * SECONDS_IN_YEAR +
- parseFloat(match[4] || 0) * SECONDS_IN_MONTH +
- parseFloat(match[6] || 0) * SECONDS_IN_DAY +
- parseFloat(match[8] || 0) * SECONDS_IN_HOUR +
- parseFloat(match[10] || 0) * SECONDS_IN_MIN +
- parseFloat(match[12] || 0));
+ let result = (parseFloat(match[3] || 0) * SECONDS_IN_YEAR +
+ parseFloat(match[5] || 0) * SECONDS_IN_MONTH +
+ parseFloat(match[7] || 0) * SECONDS_IN_DAY +
+ parseFloat(match[9] || 0) * SECONDS_IN_HOUR +
+ parseFloat(match[11] || 0) * SECONDS_IN_MIN +
+ parseFloat(match[13] || 0));
if (match[1] !== undefined) {
result = -result;