From 3f4afd8128f5896fdeabebd618a44a2ee46244b3 Mon Sep 17 00:00:00 2001 From: Imran Imtiaz Date: Sun, 7 Jul 2024 18:50:45 +0400 Subject: [PATCH] Update script.js the changes and improvements made to the `parseISOString` function: 1. **Variable Naming**: - Changed `s` to `isoString` to make the purpose of the variable clear. Descriptive variable names improve readability and maintainability. 2. **Use of `const`**: - Used `const` instead of `var` for `dateParts`. This is because `const` is block-scoped and indicates that the variable's value won't change, which is a modern JavaScript best practice. 3. **Array Destructuring**: - Used array destructuring to directly assign each part of the date to a corresponding variable (`year`, `month`, `day`, `hour`, `minute`, `second`, `millisecond`). This makes the code more readable by showing explicitly what each part of the split string represents. 4. **Explicit Conversion**: - Added `.map(Number)` to convert each element of the `dateParts` array from a string to a number. This ensures the elements are properly treated as numbers in the `Date.UTC` function. 5. **Single Responsibility Principle**: - The function is clear and focused on a single task: parsing an ISO string and returning a Date object. This makes it easier to understand and maintain. --- static/js/script.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/static/js/script.js b/static/js/script.js index f409b7b4f..25bc05941 100644 --- a/static/js/script.js +++ b/static/js/script.js @@ -1,4 +1,6 @@ -window.parseISOString = function parseISOString(s) { - var b = s.split(/\D+/); - return new Date(Date.UTC(b[0], --b[1], b[2], b[3], b[4], b[5], b[6])); -}; +function parseISOString(isoString) { + const dateParts = isoString.split(/\D+/).map(Number); + const [year, month, day, hour, minute, second, millisecond] = dateParts; + + return new Date(Date.UTC(year, month - 1, day, hour, minute, second, millisecond)); +}