Skip to content

Commit f179fd6

Browse files
committed
Refactor development tool scripts
- Improve code quality and type safety in utility scripts - Standardize formatting in locale, timezone, and zonename tools - Enhance maintainability of build tooling
1 parent 13e4a48 commit f179fd6

File tree

3 files changed

+49
-54
lines changed

3 files changed

+49
-54
lines changed

tools/locale.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ const getMonths = (locale: string, style: 'long' | 'short' | 'narrow') => {
1919
const months2 = [];
2020

2121
for (let i = 0; i < 12; i++) {
22-
months1.push(dtf1.formatToParts(new Date(2024, i, 1, 0)).find(p => p.type === 'month')?.value || '');
22+
months1.push(dtf1.formatToParts(new Date(2024, i, 1, 0)).find(p => p.type === 'month')?.value ?? '');
2323
}
2424
for (let i = 0; i < 12; i++) {
25-
months2.push(dtf2.formatToParts(new Date(2024, i, 1, 0)).find(p => p.type === 'month')?.value || '');
25+
months2.push(dtf2.formatToParts(new Date(2024, i, 1, 0)).find(p => p.type === 'month')?.value ?? '');
2626
}
2727
return compare(months1, months2) ? months1 : [months1, months2];
2828
};
@@ -36,10 +36,10 @@ const getWeekdays = (locale: string, style: 'long' | 'short' | 'narrow') => {
3636
const weekdays2 = [];
3737

3838
for (let i = 1; i <= 7; i++) {
39-
weekdays1.push(dtf1.formatToParts(new Date(2024, 11, i, 0)).find(p => p.type === 'weekday')?.value || '');
39+
weekdays1.push(dtf1.formatToParts(new Date(2024, 11, i, 0)).find(p => p.type === 'weekday')?.value ?? '');
4040
}
4141
for (let i = 1; i <= 7; i++) {
42-
weekdays2.push(dtf2.formatToParts(new Date(2024, 11, i, 0)).find(p => p.type === 'weekday')?.value || '');
42+
weekdays2.push(dtf2.formatToParts(new Date(2024, 11, i, 0)).find(p => p.type === 'weekday')?.value ?? '');
4343
}
4444
return compare(weekdays1, weekdays2) ? weekdays1 : [weekdays1, weekdays2];
4545
};
@@ -49,18 +49,18 @@ const getDayPeriod = (locale: string) => {
4949
const options2: Intl.DateTimeFormatOptions = { ...options1, weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
5050
const dtf1 = new Intl.DateTimeFormat(locale, options1);
5151
const dtf2 = new Intl.DateTimeFormat(locale, options2);
52-
const dayperiod1 = [];
53-
const dayperiod2 = [];
52+
const dayperiod1: string[] = [];
53+
const dayperiod2: string[] = [];
5454

5555
for (let i = 0; i < 24; i++) {
56-
const value = dtf1.formatToParts(new Date(2024, 11, 1, i)).find(p => p.type === 'dayPeriod')?.value || '';
57-
if (dayperiod1.indexOf(value) < 0) {
56+
const value = dtf1.formatToParts(new Date(2024, 11, 1, i)).find(p => p.type === 'dayPeriod')?.value ?? '';
57+
if (!dayperiod1.includes(value)) {
5858
dayperiod1.push(value);
5959
}
6060
}
6161
for (let i = 0; i < 24; i++) {
62-
const value = dtf2.formatToParts(new Date(2024, 11, 1, i)).find(p => p.type === 'dayPeriod')?.value || '';
63-
if (dayperiod2.indexOf(value) < 0) {
62+
const value = dtf2.formatToParts(new Date(2024, 11, 1, i)).find(p => p.type === 'dayPeriod')?.value ?? '';
63+
if (!dayperiod2.includes(value)) {
6464
dayperiod2.push(value);
6565
}
6666
}
@@ -82,7 +82,6 @@ const getParts = (locale: string) => {
8282
const getDate = (locale: string) => {
8383
const options: Intl.DateTimeFormatOptions = {
8484
hour12: true, weekday: 'short',
85-
// year: 'numeric', month: 'long', day: 'numeric',
8685
year: 'numeric', month: 'short', day: 'numeric',
8786
hour: 'numeric', minute: 'numeric', second: 'numeric', fractionalSecondDigits: 3,
8887
timeZone: 'Europe/Paris', timeZoneName: 'longOffset'

tools/timezone.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
import { readFile, mkdir, writeFile } from 'node:fs/promises';
99
import { join } from 'node:path';
1010
import prettier from 'prettier';
11-
import type { TimeZone } from '../src/timezone.ts';
11+
import type { TimeZone } from '@/timezone.ts';
1212

1313
const createTimeZones = (csv: string) => {
1414
const map = new Map<string, TimeZone>();
1515

1616
csv.split('\n').forEach(line => {
1717
const [zone_name, , , , gmt_offset] = line.split(',');
1818

19-
if (zone_name && !/UTC$/.test(zone_name)) {
19+
if (zone_name && !zone_name.endsWith('UTC')) {
2020
const data = map.get(zone_name);
2121

2222
if (data) {
23-
if (data.gmt_offset.indexOf(+gmt_offset) < 0) {
23+
if (!data.gmt_offset.includes(+gmt_offset)) {
2424
data.gmt_offset.push(+gmt_offset);
2525
}
2626
} else {
@@ -35,7 +35,7 @@ const getPath = (timezone: TimeZone) => {
3535
const re = /[^/]+$/;
3636
return {
3737
dir: join('src', 'timezones', timezone.zone_name.replace(re, '')),
38-
name: `${re.exec(timezone.zone_name)?.[0] || ''}.ts`
38+
name: `${re.exec(timezone.zone_name)?.[0] ?? ''}.ts`
3939
};
4040
};
4141

@@ -47,21 +47,19 @@ const format = (timezone: TimeZone) => {
4747
return prettier.format(code, { parser: 'typescript', singleQuote: true, trailingComma: 'none' });
4848
};
4949

50-
(async () => {
51-
const path = process.argv[2];
50+
const path = process.argv[2];
5251

53-
if (!path) {
54-
console.error('Please provide a CSV file path');
55-
process.exit();
56-
}
52+
if (!path) {
53+
console.error('Please provide a CSV file path');
54+
process.exit();
55+
}
5756

58-
const csv = await readFile(path, 'utf8');
59-
const map = createTimeZones(csv);
57+
const csv = await readFile(path, 'utf8');
58+
const map = createTimeZones(csv);
6059

61-
map.forEach(async timezone => {
62-
const { dir, name } = getPath(timezone);
60+
for (const timezone of map.values()) {
61+
const { dir, name } = getPath(timezone);
6362

64-
await mkdir(dir, { recursive: true });
65-
await writeFile(join(dir, name), await format(timezone));
66-
});
67-
})();
63+
await mkdir(dir, { recursive: true });
64+
await writeFile(join(dir, name), await format(timezone));
65+
}

tools/zonename.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { readFile } from 'node:fs/promises';
99
import prettier from 'prettier';
10-
import zonenames from '../src/zonenames.ts';
10+
import zonenames from '@/zonenames.ts';
1111

1212
const getTimezone = async (path: string) => {
1313
try {
@@ -30,7 +30,7 @@ const getTimezone = async (path: string) => {
3030
};
3131

3232
const getTimezoneName = (dtf: Intl.DateTimeFormat, time: number) => {
33-
return dtf.formatToParts(time).find(part => part.type === 'timeZoneName')?.value.replace(/^GMT([+-].+)?$/, '') || '';
33+
return dtf.formatToParts(time).find(part => part.type === 'timeZoneName')?.value.replace(/^GMT([+-].+)?$/, '') ?? '';
3434
};
3535

3636
const getTimezoneNames = (timeZone: string, names: Record<string, string>) => {
@@ -68,31 +68,29 @@ const format = (code: Map<string, string>) => {
6868
};`, { parser: 'typescript', singleQuote: true, trailingComma: 'none' });
6969
};
7070

71-
(async () => {
72-
const path = process.argv[2];
71+
const path = process.argv[2];
7372

74-
if (!path) {
75-
console.error('Please provide a CSV file path');
76-
process.exit();
77-
}
73+
if (!path) {
74+
console.error('Please provide a CSV file path');
75+
process.exit();
76+
}
7877

79-
const timezones = await getTimezone(path);
80-
const timeZoneNames = new Map<string, string>();
81-
const errorNames = new Set<string>();
78+
const timezones = await getTimezone(path);
79+
const timeZoneNames = new Map<string, string>();
80+
const errorNames = new Set<string>();
8281

83-
timezones.forEach(timeZone => {
84-
const { names, errors } = getTimezoneNames(timeZone, zonenames);
82+
timezones.forEach(timeZone => {
83+
const { names, errors } = getTimezoneNames(timeZone, zonenames);
8584

86-
for (const [key, value] of names.entries()) {
87-
timeZoneNames.set(key, value);
88-
}
89-
errors.forEach(err => errorNames.add(err));
90-
});
91-
92-
if (errorNames.size) {
93-
console.error('Not Found');
94-
console.error(Array.from(errorNames));
95-
} else {
96-
process.stdout.write(await format(sort(timeZoneNames)));
85+
for (const [key, value] of names.entries()) {
86+
timeZoneNames.set(key, value);
9787
}
98-
})();
88+
errors.forEach(err => errorNames.add(err));
89+
});
90+
91+
if (errorNames.size) {
92+
console.error('Not Found');
93+
console.error(Array.from(errorNames));
94+
} else {
95+
process.stdout.write(await format(sort(timeZoneNames)));
96+
}

0 commit comments

Comments
 (0)