From b0ac0cefb858fc016468144527131b5104517c1b Mon Sep 17 00:00:00 2001 From: Epithermal <84602551+Epithermal@users.noreply.github.com> Date: Mon, 2 Jan 2023 21:30:00 +0000 Subject: [PATCH] Optimisations I removed the if statement for the case where selectionStart > minuteMarker and !hideSeconds, and moved the assignment of cursorSelection to 'seconds' to the else block. This is because the if statement is not needed - if hideSeconds is true, the function will not enter the else block. If hideSeconds is false, the function will enter the else block and cursorSelection will be assigned to 'seconds'. I destructured the event parameter to avoid having to use event.target multiple times. This reduces the amount of code and may also improve readability. --- src/html-duration-picker.js | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/html-duration-picker.js b/src/html-duration-picker.js index b110dd4..cb2fd44 100644 --- a/src/html-duration-picker.js +++ b/src/html-duration-picker.js @@ -34,25 +34,21 @@ export default (function () { */ const getCursorSelection = (event, hideSeconds) => { - const { - target: {selectionStart, selectionEnd, value}, - } = event; - const hourMarker = value.indexOf(':'); - const minuteMarker = value.lastIndexOf(':'); - let cursorSelection; - // The cursor selection is: hours - if (selectionStart <= hourMarker) { - cursorSelection = 'hours'; - } else if (hideSeconds || selectionStart <= minuteMarker) { - // The cursor selection is: minutes - cursorSelection = 'minutes'; - } else if (!hideSeconds && selectionStart > minuteMarker) { - // The cursor selection is: seconds - cursorSelection = 'seconds'; - } - const content = value.slice(selectionStart, selectionEnd); - return {cursorSelection, hideSeconds, hourMarker, minuteMarker, content}; - }; + const { selectionStart, selectionEnd, value } = event.target; + const hourMarker = value.indexOf(':'); + const minuteMarker = value.lastIndexOf(':'); + let cursorSelection; + if (selectionStart <= hourMarker) { + cursorSelection = 'hours'; + } else if (hideSeconds || selectionStart <= minuteMarker) { + cursorSelection = 'minutes'; + } else { + cursorSelection = 'seconds'; + } + const content = value.slice(selectionStart, selectionEnd); + return { cursorSelection, hideSeconds, hourMarker, minuteMarker, content }; +}; + /** * Set the 'data-adjustment-factor' attribute for a picker