Skip to content

Commit d621d09

Browse files
committed
chore: fix style & types
1 parent 846614b commit d621d09

File tree

6 files changed

+28
-31
lines changed

6 files changed

+28
-31
lines changed

eslint.config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ export default ts({
66
'no-cond-assign': [0],
77
'no-console': [2],
88
'ts/explicit-function-return-type': [2],
9-
'import/extensions': [2, 'ignorePackages', {
10-
checkTypeImports: true,
11-
}],
129
},
1310
}, {
1411
files: ['scripts/*.ts'],

src/platform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { $Fetch, CreateFetchOptions, FetchOptions } from 'ofetch';
22
import type { Contest } from './contest.ts';
33
import type { Problem } from './problem.ts';
44
import { ofetch } from 'ofetch';
5-
import { addHeaders, UnOJError, version } from './utils.ts';
5+
import { mergeHeaders, UnOJError, version } from './utils.ts';
66

77
/** General platform constructor options. */
88
export interface PlatformOptions<Locale extends string | never = never> {
@@ -52,7 +52,7 @@ export abstract class Platform<Locale extends string | never = never> {
5252
this.ofetch = options?.ofetch ?? ofetch.create({
5353
...options?.ofetchDefaults,
5454
baseURL: this.baseURL,
55-
headers: addHeaders(headers, [['user-agent', `UnOJ/${version}`]]),
55+
headers: mergeHeaders(headers, [['user-agent', `UnOJ/${version}`]]),
5656
}, options?.ofetchCreateOptions);
5757

5858
this.locale = options?.locale ?? defaultLocale;

src/platforms/atcoder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default class AtCoder extends Platform<Locale> {
6565
.text()
6666
.trimStart()
6767
.split('\n')[0]
68-
.split(' - ')[1];
68+
?.split(' - ')[1] ?? '';
6969
const descContainer = statement.find(`span.lang-${this.locale}`);
7070
const hr = descContainer.find('hr').first();
7171
if (hr.length) {
@@ -108,8 +108,8 @@ export default class AtCoder extends Platform<Locale> {
108108

109109
description,
110110
samples,
111-
timeLimit: parseTime(tlMatch![1])!,
112-
memoryLimit: parseMemory(mlMatch![1])!,
111+
timeLimit: parseTime(tlMatch?.[1])!,
112+
memoryLimit: parseMemory(mlMatch?.[1])!,
113113

114114
difficulty: undefined,
115115
tags: undefined,

src/platforms/hydro.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function parseHydroProblem(pdoc: any, lang?: string): Problem {
4646
for (let i = 1, match: any; match = r(i, 'input').exec(content); i++) {
4747
samples.push({
4848
input: match[1].trim(),
49-
output: r(i, 'output').exec(content)?.[1].trim() || '',
49+
output: r(i, 'output').exec(content)?.[1]?.trim() || '',
5050
});
5151
}
5252

src/platforms/luogu.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { PlatformOptions } from '../platform.ts';
88
import type { Problem as BaseProblem, ProblemDescriptionObject, TagInfo } from '../problem.ts';
99
import { FetchError } from 'ofetch';
1010
import { NotFoundError, Platform } from '../platform.ts';
11-
import { addHeaders, UnOJError } from '../utils.ts';
11+
import { mergeHeaders, UnOJError } from '../utils.ts';
1212

1313
export type ProblemType = 'traditional' | 'interactive' | 'communication' | 'submission';
1414
export type Difficulty = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
@@ -74,7 +74,7 @@ export default class Luogu extends Platform<string> {
7474
...options,
7575
ofetchDefaults: {
7676
...options?.ofetchDefaults,
77-
headers: addHeaders(
77+
headers: mergeHeaders(
7878
options?.ofetchDefaults?.headers,
7979
[['x-lentille-request', 'content-only'], ['x-luogu-type', 'content-only']],
8080
),

src/utils.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { HeadersInit } from 'bun';
2+
13
export { version } from '../package.json' with { type: 'json' };
24

35
/** Get the first key of an object. */
@@ -10,7 +12,9 @@ export function getFirstKey<T extends Record<never, never>>(obj: T): keyof T {
1012
* @param s The string to parse.
1113
* @returns The time in milliseconds, or undefined if failed.
1214
*/
13-
export function parseTime(s: string): number | undefined {
15+
export function parseTime(s?: string): number | undefined {
16+
if (!s)
17+
return;
1418
const val = Number.parseInt(s);
1519
if (Number.isNaN(val))
1620
return;
@@ -36,35 +40,31 @@ const MEMORY_UNITS: Record<string, number> = {
3640
* @param s The string to parse.
3741
* @returns The memory size in bytes, or undefined if failed.
3842
*/
39-
export function parseMemory(s: string): number | undefined {
40-
const [, val, unit] = s.match(/^(\d+) ([a-z]+)$/i) ?? [];
43+
export function parseMemory(s?: string): number | undefined {
44+
const [, val, unit] = s?.match(/^(\d+) ([a-z]+)$/i) ?? [];
4145
if (!val || !unit)
4246
return;
43-
return Number(val) * MEMORY_UNITS[unit];
47+
return Number(val) * (MEMORY_UNITS[unit] ?? Number.NaN);
4448
}
4549

4650
/**
4751
* Adds key-value pairs to the provided headers object if they aren't present.
4852
*
49-
* @param h The original headers object.
53+
* @param init The original headers object.
5054
* @param add The key-value pairs to add.
5155
* @returns A **new** headers object.
5256
*/
53-
export function addHeaders(h: HeadersInit | undefined, add: Array<[string, string]>): Record<string, string> {
54-
if (h instanceof Headers)
55-
h = h.toJSON();
56-
if (Array.isArray(h))
57-
h = Object.fromEntries(h);
58-
else if (h == null)
59-
h = {};
60-
else
61-
h = structuredClone(h);
62-
63-
for (const item of add) {
64-
if (!(item[0] in h))
65-
h[item[0]] = item[1];
66-
}
67-
return h;
57+
export function mergeHeaders(init: HeadersInit | undefined, add: Array<[string, string]>): Headers {
58+
const ret = new Headers(
59+
init instanceof Headers
60+
? init
61+
: Array.isArray(init)
62+
? Object.fromEntries(init)
63+
: init,
64+
);
65+
for (const item of add)
66+
ret.append(item[0], item[1]);
67+
return ret!;
6868
}
6969

7070
/** General error class for UnOJ. */

0 commit comments

Comments
 (0)