Skip to content

Commit a8e5a28

Browse files
committed
Improve source code formatting and type definitions
- Enhance type safety across core modules - Standardize code formatting and style consistency - Refine locale definitions for better maintainability - Improve plugin implementations - Update numeral system handlers
1 parent 9fd0ce8 commit a8e5a28

65 files changed

Lines changed: 155 additions & 158 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/addDays.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { toParts, fromParts } from './datetime.ts';
2-
import { isTimeZone, isUTC, getTimezoneOffset } from './timezone.ts';
2+
import { isTimeZone, isUTC, createTimezoneDate } from './timezone.ts';
33
import type { TimeZone } from './timezone.ts';
44

55
/**
@@ -17,10 +17,7 @@ export function addDays(dateObj: Date, days: number, timeZone?: TimeZone | 'UTC'
1717
parts.day += days;
1818
parts.timezoneOffset = 0;
1919

20-
const utcTime = fromParts(parts);
21-
const offset = getTimezoneOffset(utcTime, timeZone);
22-
23-
return new Date(utcTime - offset * 1000);
20+
return createTimezoneDate(fromParts(parts), timeZone);
2421
}
2522

2623
const d = new Date(dateObj.getTime());

src/addMonths.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { toParts, fromParts } from './datetime.ts';
2-
import { isTimeZone, isUTC, getTimezoneOffset } from './timezone.ts';
2+
import { isTimeZone, isUTC, createTimezoneDate } from './timezone.ts';
33
import type { TimeZone } from './timezone.ts';
44

55
/**
@@ -23,15 +23,15 @@ export function addMonths(dateObj: Date, months: number, timeZone?: TimeZone | '
2323
d.setUTCDate(0);
2424
}
2525

26-
const utcTime = fromParts({
27-
...parts,
28-
year: d.getUTCFullYear(),
29-
month: d.getUTCMonth() + 1,
30-
day: d.getUTCDate()
31-
});
32-
const offset = getTimezoneOffset(utcTime, timeZone);
33-
34-
return new Date(utcTime - offset * 1000);
26+
return createTimezoneDate(
27+
fromParts({
28+
...parts,
29+
year: d.getUTCFullYear(),
30+
month: d.getUTCMonth() + 1,
31+
day: d.getUTCDate()
32+
}),
33+
timeZone
34+
);
3535
}
3636

3737
const d = new Date(dateObj.getTime());

src/compile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ const REGEXP = /\[(?:[^[\]]|\[[^[\]]*])*]|([A-Za-z])\1*|\.{3}|./g;
2020
* followed by tokenized format components
2121
*/
2222
export const compile = (formatString: string): CompiledObject => {
23-
const array = formatString.replace(LBRACKET, '\uE000').replace(RBRACKET, '\uE001').match(REGEXP) || [];
23+
const array = formatString.replace(LBRACKET, '\uE000').replace(RBRACKET, '\uE001').match(REGEXP) ?? [];
2424
return [formatString, ...array.map(token => token.replace(PUA_LBRACKET, '[').replace(PUA_RBRACKET, ']'))];
2525
};

src/datetime.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface DateTimeParts {
1313
}
1414

1515
export const toParts = (dateObj: Date, zoneName: string): DateTimeParts => {
16-
if (zoneName?.toUpperCase() === 'UTC') {
16+
if (zoneName.toUpperCase() === 'UTC') {
1717
return {
1818
weekday: dateObj.getUTCDay(),
1919
year: dateObj.getUTCFullYear(),
@@ -76,43 +76,43 @@ export interface DateLike {
7676
/**
7777
* Returns the year of the date.
7878
*/
79-
getFullYear(): number,
79+
getFullYear(): number;
8080
/**
8181
* Returns the month of the date (0-11).
8282
*/
83-
getMonth(): number,
83+
getMonth(): number;
8484
/**
8585
* Returns the day of the month (1-31).
8686
*/
87-
getDate(): number,
87+
getDate(): number;
8888
/**
8989
* Returns the hours of the date (0-23).
9090
*/
91-
getHours(): number,
91+
getHours(): number;
9292
/**
9393
* Returns the minutes of the date (0-59).
9494
*/
95-
getMinutes(): number,
95+
getMinutes(): number;
9696
/**
9797
* Returns the seconds of the date (0-59).
9898
*/
99-
getSeconds(): number,
99+
getSeconds(): number;
100100
/**
101101
* Returns the milliseconds of the date (0-999).
102102
*/
103-
getMilliseconds(): number,
103+
getMilliseconds(): number;
104104
/**
105-
* Returns the day of the week (0-6, where 0 is Sunday).
105+
* Returns the day of the week (0-6; where 0 is Sunday).
106106
*/
107-
getDay(): number,
107+
getDay(): number;
108108
/**
109-
* Returns the time value in milliseconds since the Unix epoch (January 1, 1970).
109+
* Returns the time value in milliseconds since the Unix epoch (January 1; 1970).
110110
*/
111-
getTime(): number,
111+
getTime(): number;
112112
/**
113113
* Returns the timezone offset in minutes from UTC.
114114
*/
115-
getTimezoneOffset(): number
115+
getTimezoneOffset(): number;
116116
}
117117

118118
export class DateTime implements DateLike {

src/dtf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const cache = new Map<string, Intl.DateTimeFormat>();
22

33
export const getDateTimeFormat = (zoneName: string): Intl.DateTimeFormat => {
4-
return cache.get(zoneName) || (() => {
4+
return cache.get(zoneName) ?? (() => {
55
const dtf = new Intl.DateTimeFormat('en-US', {
66
hour12: false, weekday: 'short', year: 'numeric', month: 'numeric', day: 'numeric',
77
hour: 'numeric', minute: 'numeric', second: 'numeric', fractionalSecondDigits: 3,

src/duration.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ interface DurationFormatter {
1414
F?: number;
1515
}
1616

17-
const nanosecondsPart = (parts: DurationFormatter, time: number | DOMHighResTimeStamp) => {
17+
const nanosecondsPart = (parts: DurationFormatter, time: DOMHighResTimeStamp) => {
1818
parts.F = Math.trunc(time * 1000000);
1919
return parts;
2020
};
2121

22-
const microsecondsPart = (parts: DurationFormatter, time: number | DOMHighResTimeStamp) => {
22+
const microsecondsPart = (parts: DurationFormatter, time: DOMHighResTimeStamp) => {
2323
parts.f = Math.trunc(time * 1000);
2424
return nanosecondsPart(parts, Math.abs(time) * 1000 % 1 / 1000);
2525
};
2626

27-
const millisecondsPart = (parts: DurationFormatter, time: number | DOMHighResTimeStamp) => {
27+
const millisecondsPart = (parts: DurationFormatter, time: DOMHighResTimeStamp) => {
2828
parts.S = Math.trunc(time);
2929
return microsecondsPart(parts, Math.abs(time) % 1);
3030
};
@@ -58,7 +58,7 @@ const format = (times: DurationFormatter, formatString: string, numeral: Numeral
5858
const resolveToken = (token: string) => {
5959
if (token[0] in times) {
6060
const time = times[token[0] as keyof DurationFormatter] ?? 0;
61-
return numeral.encode(`${sign(time)}${`${Math.abs(time)}`.padStart(token.length, '0')}`);
61+
return numeral.encode(`${sign(time)}${String(Math.abs(time)).padStart(token.length, '0')}`);
6262
}
6363
return comment.test(token) ? token.replace(comment, '$1') : token;
6464
};
@@ -116,9 +116,9 @@ export interface DurationDescriptor<T> {
116116
}
117117

118118
export class Duration {
119-
private readonly time: number | DOMHighResTimeStamp;
119+
private readonly time: DOMHighResTimeStamp;
120120

121-
constructor (time: number | DOMHighResTimeStamp) {
121+
constructor (time: DOMHighResTimeStamp) {
122122
this.time = time;
123123
}
124124

src/format.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ const comment = /^\[(.*)\]$/;
1919
export function format(dateObj: Date, arg: string | CompiledObject, options?: FormatterOptions) {
2020
const pattern = (typeof arg === 'string' ? compile(arg) : arg).slice(1);
2121
const timeZone = isTimeZone(options?.timeZone) || isUTC(options?.timeZone) ? options.timeZone : undefined;
22-
const zoneName = typeof timeZone === 'string' ? timeZone : timeZone?.zone_name || '';
22+
const zoneName = typeof timeZone === 'string' ? timeZone : timeZone?.zone_name ?? '';
2323
const dateTime = zoneName ? new DateTime(dateObj, zoneName) : dateObj;
2424
const formatterOptions = {
25-
hour12: options?.hour12 || 'h12',
26-
hour24: options?.hour24 || 'h23',
27-
numeral: options?.numeral || latn,
28-
calendar: options?.calendar || 'gregory',
25+
hour12: options?.hour12 ?? 'h12',
26+
hour24: options?.hour24 ?? 'h23',
27+
numeral: options?.numeral ?? latn,
28+
calendar: options?.calendar ?? 'gregory',
2929
timeZone,
30-
locale: options?.locale || en
30+
locale: options?.locale ?? en
3131
};
32-
const formatters = [...options?.plugins || [], defaultFormatter];
32+
const formatters = [...options?.plugins ?? [], defaultFormatter];
3333
const encode = formatterOptions.numeral.encode;
3434
const resolveToken = (token: string, compiledObj: CompiledObject) => {
3535
for (const formatter of formatters) {

src/formatter.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface FormatterPluginOptions {
4646
}
4747

4848
export abstract class FormatterPlugin {
49-
[key: string]: (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) => string;
49+
[key: string]: ((d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) => string) | undefined;
5050
}
5151

5252
export interface FormatterOptions extends Partial<FormatterPluginOptions> {
@@ -59,15 +59,15 @@ const getFullYear = (d: DateLike, calendar: 'buddhist' | 'gregory') => {
5959

6060
class DefaultFormatter extends FormatterPlugin {
6161
YYYY (d: DateLike, options: FormatterPluginOptions) {
62-
return `000${getFullYear(d, options.calendar)}`.slice(-4);
62+
return `000${String(getFullYear(d, options.calendar))}`.slice(-4);
6363
}
6464

6565
YY (d: DateLike, options: FormatterPluginOptions) {
66-
return `0${getFullYear(d, options.calendar)}`.slice(-2);
66+
return `0${String(getFullYear(d, options.calendar))}`.slice(-2);
6767
}
6868

6969
Y (d: DateLike, options: FormatterPluginOptions) {
70-
return `${getFullYear(d, options.calendar)}`;
70+
return String(getFullYear(d, options.calendar));
7171
}
7272

7373
MMMM (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) {
@@ -81,27 +81,27 @@ class DefaultFormatter extends FormatterPlugin {
8181
}
8282

8383
MM (d: DateLike) {
84-
return `0${d.getMonth() + 1}`.slice(-2);
84+
return `0${String(d.getMonth() + 1)}`.slice(-2);
8585
}
8686

8787
M (d: DateLike) {
88-
return `${d.getMonth() + 1}`;
88+
return String(d.getMonth() + 1);
8989
}
9090

9191
DD (d: DateLike) {
92-
return `0${d.getDate()}`.slice(-2);
92+
return `0${String(d.getDate())}`.slice(-2);
9393
}
9494

9595
D (d: DateLike) {
96-
return `${d.getDate()}`;
96+
return String(d.getDate());
9797
}
9898

9999
HH (d: DateLike, options: FormatterPluginOptions) {
100-
return `0${d.getHours() || (options.hour24 === 'h24' ? 24 : 0)}`.slice(-2);
100+
return `0${String(d.getHours() || (options.hour24 === 'h24' ? 24 : 0))}`.slice(-2);
101101
}
102102

103103
H (d: DateLike, options: FormatterPluginOptions) {
104-
return `${d.getHours() || (options.hour24 === 'h24' ? 24 : 0)}`;
104+
return String(d.getHours() || (options.hour24 === 'h24' ? 24 : 0));
105105
}
106106

107107
AA (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) {
@@ -125,39 +125,39 @@ class DefaultFormatter extends FormatterPlugin {
125125
}
126126

127127
hh (d: DateLike, options: FormatterPluginOptions) {
128-
return `0${d.getHours() % 12 || (options.hour12 === 'h12' ? 12 : 0)}`.slice(-2);
128+
return `0${String(d.getHours() % 12 || (options.hour12 === 'h12' ? 12 : 0))}`.slice(-2);
129129
}
130130

131131
h (d: DateLike, options: FormatterPluginOptions) {
132-
return `${d.getHours() % 12 || (options.hour12 === 'h12' ? 12 : 0)}`;
132+
return String(d.getHours() % 12 || (options.hour12 === 'h12' ? 12 : 0));
133133
}
134134

135135
mm (d: DateLike) {
136-
return `0${d.getMinutes()}`.slice(-2);
136+
return `0${String(d.getMinutes())}`.slice(-2);
137137
}
138138

139139
m (d: DateLike) {
140-
return `${d.getMinutes()}`;
140+
return String(d.getMinutes());
141141
}
142142

143143
ss (d: DateLike) {
144-
return `0${d.getSeconds()}`.slice(-2);
144+
return `0${String(d.getSeconds())}`.slice(-2);
145145
}
146146

147147
s (d: DateLike) {
148-
return `${d.getSeconds()}`;
148+
return String(d.getSeconds());
149149
}
150150

151151
SSS (d: DateLike) {
152-
return `00${d.getMilliseconds()}`.slice(-3);
152+
return `00${String(d.getMilliseconds())}`.slice(-3);
153153
}
154154

155155
SS (d: DateLike) {
156-
return `00${d.getMilliseconds()}`.slice(-3, -1);
156+
return `00${String(d.getMilliseconds())}`.slice(-3, -1);
157157
}
158158

159159
S (d: DateLike) {
160-
return `00${d.getMilliseconds()}`.slice(-3, -2);
160+
return `00${String(d.getMilliseconds())}`.slice(-3, -2);
161161
}
162162

163163
dddd (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) {
@@ -178,13 +178,13 @@ class DefaultFormatter extends FormatterPlugin {
178178
Z (d: DateLike) {
179179
const offset = d.getTimezoneOffset();
180180
const absOffset = Math.abs(offset);
181-
return `${offset > 0 ? '-' : '+'}${`0${absOffset / 60 | 0}`.slice(-2)}${`0${absOffset % 60}`.slice(-2)}`;
181+
return `${offset > 0 ? '-' : '+'}${`0${String(absOffset / 60 | 0)}`.slice(-2)}${`0${String(absOffset % 60)}`.slice(-2)}`;
182182
}
183183

184184
ZZ (d: DateLike) {
185185
const offset = d.getTimezoneOffset();
186186
const absOffset = Math.abs(offset);
187-
return `${offset > 0 ? '-' : '+'}${`0${absOffset / 60 | 0}`.slice(-2)}:${`0${absOffset % 60}`.slice(-2)}`;
187+
return `${offset > 0 ? '-' : '+'}${`0${String(absOffset / 60 | 0)}`.slice(-2)}:${`0${String(absOffset % 60)}`.slice(-2)}`;
188188
}
189189
}
190190

src/isValid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function validatePreparseResult(pr: PreparseResult, options?: ParserOptio
2525
&& pr._match > 0
2626
&& range(y, 1, 9999)
2727
&& range(pr.M, 1, 12)
28-
&& range(pr.D, 1, getLastDayOfMonth(y, pr.M || 1))
28+
&& range(pr.D, 1, getLastDayOfMonth(y, pr.M ?? 1))
2929
&& range(pr.H, min24, max24)
3030
&& range(pr.A, 0, 1)
3131
&& range(pr.h, min12, max12)

src/locale.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { CompiledObject } from './compile.ts';
33
export interface LocaleOptions {
44
compiledObj: CompiledObject;
55
style: 'long' | 'short' | 'narrow';
6-
case?: 'uppercase' | 'lowercase'
6+
case?: 'uppercase' | 'lowercase';
77
}
88

99
export interface Locale {

0 commit comments

Comments
 (0)