Skip to content

Commit 553cca1

Browse files
Refactor Util.stringToSeconds function (web-scrobbler#2523)
Refactor Util.stringToSeconds by using two simple regex matchers. There is also no longer a need to trim the string, since the expressions only match on the negative sign or groups of two digits.
1 parent 9917a9f commit 553cca1

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

src/core/content/util.js

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,20 @@ const Util = {
2828
* @return {Number} Seconds
2929
*/
3030
stringToSeconds(str) {
31-
if (!str) {
31+
const timeFormatExpression = /^\s*-?((\d{1,2}:\d\d:\d\d)|(\d{1,2}:\d\d)|(\d{1,2}))\s*$/g;
32+
if (!timeFormatExpression.test(str)) {
3233
return 0;
3334
}
3435

35-
let s = str.toString().trim();
36-
let val = 0;
37-
let seconds = 0;
36+
const negativeExpression = /-/g;
37+
const digitsExpression = /\d{1,2}/g;
3838

39-
const isNegative = s.startsWith('-');
40-
if (isNegative) {
41-
s = s.substr(1);
42-
}
43-
44-
for (let i = 0; i < 3; i++) {
45-
const idx = s.lastIndexOf(':');
46-
if (idx > -1) {
47-
val = parseInt(s.substr(idx + 1), 10);
48-
seconds += val * Math.pow(60, i);
49-
s = s.substr(0, idx);
50-
} else {
51-
val = parseInt(s, 10);
52-
seconds += val * Math.pow(60, i);
53-
break;
54-
}
55-
}
56-
57-
if (isNegative) {
58-
seconds = -seconds;
59-
}
39+
const seconds = str.match(digitsExpression)
40+
.reverse()
41+
.map((current) => parseInt(current, 10))
42+
.reduce((total, current, i) => total + current * Math.pow(60, i));
6043

61-
return seconds;
44+
return (negativeExpression.test(str)) ? -seconds : seconds;
6245
},
6346

6447
/**

tests/content/util.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,41 @@ const ESCAPE_BAD_TIME_VALUES_DATA = [{
119119
* @type {Array}
120120
*/
121121
const STRING_TO_SECONDS_DATA = [{
122-
description: 'should trim string and parse time',
123-
args: ['01:10:30 '],
122+
description: 'should parse time that contains leading and trailing whitespace',
123+
args: [' 01:10:30 '],
124124
expected: 4230,
125125
}, {
126126
description: 'should parse time in hh:mm:ss format',
127127
args: ['01:10:30'],
128128
expected: 4230,
129+
}, {
130+
description: 'should parse time in h:mm:ss format',
131+
args: ['5:20:00'],
132+
expected: 19200,
129133
}, {
130134
description: 'should parse negative time',
131135
args: ['-01:10'],
132136
expected: -70,
137+
}, {
138+
description: 'should parse negative time that contains leading and trailing whitespace',
139+
args: [' -01:10 '],
140+
expected: -70,
133141
}, {
134142
description: 'should parse time in mm:ss format',
135143
args: ['05:20'],
136144
expected: 320,
145+
}, {
146+
description: 'should parse time in m:ss format',
147+
args: ['5:20'],
148+
expected: 320,
137149
}, {
138150
description: 'should parse time in ss format',
139151
args: ['20'],
140152
expected: 20,
153+
}, {
154+
description: 'should parse time in s format',
155+
args: ['2'],
156+
expected: 2,
141157
}, {
142158
description: 'should not parse empty string',
143159
args: [''],
@@ -150,6 +166,22 @@ const STRING_TO_SECONDS_DATA = [{
150166
description: 'should not parse malformed format',
151167
args: [NaN],
152168
expected: 0,
169+
}, {
170+
description: 'should not parse a format without colons',
171+
args: ['01 10 30'],
172+
expected: 0,
173+
}, {
174+
description: 'should not parse a format with days',
175+
args: ['01:00:00:00'],
176+
expected: 0,
177+
}, {
178+
description: 'should not parse mm:s format',
179+
args: ['12:4'],
180+
expected: 0,
181+
}, {
182+
description: 'should not parse hh:m:s format',
183+
args: ['12:3:4'],
184+
expected: 0,
153185
}];
154186

155187
/**

0 commit comments

Comments
 (0)