Skip to content

Commit 738e03e

Browse files
committed
add type annotations to datetime
1 parent a9351dc commit 738e03e

2 files changed

Lines changed: 67 additions & 12 deletions

File tree

src/components/mediainfo/usePrimaryMediaInfo.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ function addproductionYearWithEndDate(
288288
{ useGrouping: false }
289289
);
290290
/* At this point, text will contain only the start year */
291+
// @ts-expect-error: this appears to be an error. Since, toLocaleString() returns a `string` it can never equal itemProductionYear since it's a number.
292+
// This is either a logic error or the typing on BaseItemDto is incorrect.
293+
// eslint-disable-next-line sonarjs/different-types-comparison
291294
if (endYear !== itemProductionYear) {
292295
productionYear += `-${endYear}`;
293296
}

src/scripts/datetime.js

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
// @ts-nocheck
21
import globalize from 'lib/globalize';
32

3+
/**
4+
* @param {string | null | undefined } s
5+
* @param {boolean} [toLocal]: If not set, assumed true
6+
* @returns {Date}
7+
*/
48
export function parseISO8601Date(s, toLocal) {
59
// parenthese matches:
610
// year month day hours minutes seconds
711
// dotmilliseconds
812
// tzstring plusminus hours minutes
913
const re = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.\d+)?(Z|([+-])(\d{2}):(\d{2}))?/;
1014

15+
/** @type {(string | number)[] | null} */
16+
// @ts-expect-error: Some functions call this with potentially undefined `s`s and so we get a type error here
1117
const d = s.match(re);
1218

1319
// "2010-12-07T11:00:00.000-09:00" parses to:
@@ -24,13 +30,14 @@ export function parseISO8601Date(s, toLocal) {
2430
// parse strings, leading zeros into proper ints
2531
const a = [1, 2, 3, 4, 5, 6, 10, 11];
2632
for (const i in a) {
27-
d[a[i]] = parseInt(d[a[i]], 10);
33+
d[a[i]] = parseInt(/** @type {string} */ (d[a[i]]), 10);
2834
}
29-
d[7] = parseFloat(d[7]);
35+
d[7] = parseFloat(/** @type {string} */ (d[7]));
3036

3137
// Date.UTC(year, month[, date[, hrs[, min[, sec[, ms]]]]])
3238
// note that month is 0-11, not 1-12
3339
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/UTC
40+
// @ts-expect-error: d is being used to hold strings and numbers and so the type checker can't tell that we know at this point that they are numbers
3441
let ms = Date.UTC(d[1], d[2] - 1, d[3], d[4], d[5], d[6]);
3542

3643
// if there are milliseconds, add them
@@ -40,9 +47,9 @@ export function parseISO8601Date(s, toLocal) {
4047

4148
// if there's a timezone, calculate it
4249
if (d[8] !== 'Z' && d[10]) {
43-
let offset = d[10] * 60 * 60 * 1000;
50+
let offset = /** @type {number} */ (d[10]) * 60 * 60 * 1000;
4451
if (d[11]) {
45-
offset += d[11] * 60 * 1000;
52+
offset += /** @type {number} */ (d[11]) * 60 * 1000;
4653
}
4754
if (d[9] === '-') {
4855
ms -= offset;
@@ -57,9 +64,10 @@ export function parseISO8601Date(s, toLocal) {
5764
}
5865

5966
/**
60-
* Return a string in '{}h {}m' format for duration.
61-
* @param {number} ticks - Duration in ticks.
62-
*/
67+
* Return a string in '{}h {}m' format for duration.
68+
* @param {number} ticks - Duration in ticks.
69+
* @returns {string}
70+
*/
6371
export function getDisplayDuration(ticks) {
6472
const totalMinutes = Math.round(ticks / 600000000) || 1;
6573
const totalHours = Math.floor(totalMinutes / 60);
@@ -72,6 +80,10 @@ export function getDisplayDuration(ticks) {
7280
return result.join(' ');
7381
}
7482

83+
/**
84+
* @param {number} ticks
85+
* @returns {string}
86+
*/
7587
export function getDisplayRunningTime(ticks) {
7688
const ticksPerHour = 36000000000;
7789
const ticksPerMinute = 600000000;
@@ -94,8 +106,10 @@ export function getDisplayRunningTime(ticks) {
94106
ticks -= (minutes * ticksPerMinute);
95107

96108
if (minutes < 10 && hours) {
109+
// @ts-expect-error: Assigning string to the same variable that was a number
97110
minutes = (0).toLocaleString(globalize.getCurrentDateTimeLocale()) + minutes.toLocaleString(globalize.getCurrentDateTimeLocale());
98111
} else {
112+
// @ts-expect-error: Assigning string to the same variable that was a number
99113
minutes = minutes.toLocaleString(globalize.getCurrentDateTimeLocale());
100114
}
101115
parts.push(minutes);
@@ -104,8 +118,10 @@ export function getDisplayRunningTime(ticks) {
104118
seconds = Math.floor(seconds);
105119

106120
if (seconds < 10) {
121+
// @ts-expect-error: Assigning string to the same variable that was a number
107122
seconds = (0).toLocaleString(globalize.getCurrentDateTimeLocale()) + seconds.toLocaleString(globalize.getCurrentDateTimeLocale());
108123
} else {
124+
// @ts-expect-error: Assigning string to the same variable that was a number
109125
seconds = seconds.toLocaleString(globalize.getCurrentDateTimeLocale());
110126
}
111127
parts.push(seconds);
@@ -118,11 +134,17 @@ const toLocaleTimeStringSupportsLocales = function () {
118134
// eslint-disable-next-line sonarjs/no-ignored-return
119135
new Date().toLocaleTimeString('i');
120136
} catch (e) {
137+
// @ts-expect-error: Type of `e` is unknown
121138
return e.name === 'RangeError';
122139
}
123140
return false;
124141
}();
125142

143+
/**
144+
* @template T
145+
* @param {{ [key: string]: T }} options
146+
* @returns {{ name: string, value: T }[]}
147+
*/
126148
function getOptionList(options) {
127149
const list = [];
128150

@@ -136,6 +158,11 @@ function getOptionList(options) {
136158
return list;
137159
}
138160

161+
/**
162+
* @param { Date | number } date
163+
* @param {Object} [options]
164+
* @returns {string}
165+
*/
139166
export function toLocaleString(date, options) {
140167
if (!date) {
141168
throw new Error('date cannot be null');
@@ -154,6 +181,11 @@ export function toLocaleString(date, options) {
154181
return date.toLocaleString();
155182
}
156183

184+
/**
185+
* @param {Date} date
186+
* @param {Object} [options]
187+
* @returns {string}
188+
*/
157189
export function toLocaleDateString(date, options) {
158190
if (!date) {
159191
throw new Error('date cannot be null');
@@ -186,6 +218,11 @@ export function toLocaleDateString(date, options) {
186218
return date.toLocaleDateString();
187219
}
188220

221+
/**
222+
* @param {Date} date
223+
* @param {Object} [options]
224+
* @returns {string}
225+
*/
189226
export function toLocaleTimeString(date, options) {
190227
if (!date) {
191228
throw new Error('date cannot be null');
@@ -204,6 +241,10 @@ export function toLocaleTimeString(date, options) {
204241
return date.toLocaleTimeString();
205242
}
206243

244+
/**
245+
* @param {Date | string} date
246+
* @returns {string}
247+
*/
207248
export function getDisplayDateTime(date) {
208249
if (!date) {
209250
throw new Error('date cannot be null');
@@ -213,13 +254,18 @@ export function getDisplayDateTime(date) {
213254
try {
214255
date = parseISO8601Date(date, true);
215256
} catch {
216-
return date;
257+
// Since parseISO8601Date failed, date is still a string
258+
return /** @type {string} */ (date);
217259
}
218260
}
219261

220262
return toLocaleString(date);
221263
}
222264

265+
/**
266+
* @param { Date | string | null | undefined } date
267+
* @returns {string}
268+
*/
223269
export function getDisplayTime(date) {
224270
if (!date) {
225271
throw new Error('date cannot be null');
@@ -229,7 +275,8 @@ export function getDisplayTime(date) {
229275
try {
230276
date = parseISO8601Date(date, true);
231277
} catch {
232-
return date;
278+
// Since parseISO8601Date failed, date is still a string
279+
return /** @type {string} */ (date);
233280
}
234281
}
235282

@@ -252,9 +299,9 @@ export function getDisplayTime(date) {
252299
if (!hour) {
253300
hour = 12;
254301
}
255-
let minutes = date.getMinutes();
302+
let minutes = /** @type {string | number} */ (date.getMinutes());
256303

257-
if (minutes < 10) {
304+
if (/** @type {number} */ (minutes) < 10) {
258305
minutes = '0' + minutes;
259306
}
260307

@@ -274,6 +321,11 @@ export function getDisplayTime(date) {
274321
return time;
275322
}
276323

324+
/**
325+
* @param {Date} date
326+
* @param {number} offsetInDays
327+
* @returns {boolean}
328+
*/
277329
export function isRelativeDay(date, offsetInDays) {
278330
if (!date) {
279331
throw new Error('date cannot be null');

0 commit comments

Comments
 (0)