Skip to content

Commit

Permalink
Deduplicate regex handling
Browse files Browse the repository at this point in the history
Avoid repeating the regex for extracting components from a time, and
make this part of the code more concise in general.
  • Loading branch information
psvenk committed Apr 10, 2021
1 parent c5b6c59 commit 2571fc5
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions public/js/clock.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ function schedulesCallback(response) {
// Convert all start and end times to JavaScript Date objects
for (const [, schedule] of Object.entries(schedules)) {
for (entry of schedule) {
const regexp = /(\d+):(\d+):(\d+)\.(\d+)/;
// Convert each entry to a JavaScript Date object
let [, hours, minutes, seconds, ms]
= /(\d+):(\d+):(\d+)\.(\d+)/.exec(entry.start);
entry.start = new Date(2000, 0, 1, hours, minutes, seconds, ms);
[, hours, minutes, seconds, ms]
= /(\d+):(\d+):(\d+)\.(\d+)/.exec(entry.end);
entry.end = new Date(2000, 0, 1, hours, minutes, seconds, ms);
// (get hours, minutes, seconds, milliseconds)
entry.start = new Date(2000, 0, 1,
...regexp.exec(entry.start).slice(1));
entry.end = new Date(2000, 0, 1,
...regexp.exec(entry.end).slice(1));
}
}

Expand Down

0 comments on commit 2571fc5

Please sign in to comment.