Skip to content

Commit 6313b68

Browse files
committed
Extract out coercion helpers
1 parent 386eaff commit 6313b68

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

src/common/provider.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,19 @@ export interface IProblem {
5353
size: number;
5454
};
5555
}
56+
57+
export function coerceToObject(data: unknown): object {
58+
if (typeof data === "object" && data !== null) {
59+
return data;
60+
}
61+
return {};
62+
}
63+
export function coerceToArray(data: unknown): object[] {
64+
const arr = [];
65+
if (Array.isArray(data)) {
66+
for (const obj of data) {
67+
arr.push(coerceToObject(obj));
68+
}
69+
}
70+
return arr;
71+
}

src/views/judge/provider/JudgeViewProvider.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import * as path from "node:path";
22
import * as vscode from "vscode";
33

44
import { type ITestcase, Status, Stdio } from "~common/common";
5-
import type { ILanguageSettings, IProblem, ITest } from "~common/provider";
5+
import {
6+
coerceToArray,
7+
coerceToObject,
8+
type ILanguageSettings,
9+
type IProblem,
10+
type ITest,
11+
} from "~common/provider";
612
import BaseViewProvider from "~utils/BaseViewProvider";
713
import { compile, Runnable } from "~utils/runtime";
814
import {
@@ -55,22 +61,6 @@ function setTestcaseStats(state: IState, timeLimit: number) {
5561
}
5662
}
5763

58-
function coerceToObject(data: unknown): unknown {
59-
if (typeof data === "object" && data !== null) {
60-
return data;
61-
}
62-
return {};
63-
}
64-
function coerceToArray(data: unknown): unknown[] {
65-
const arr = [];
66-
if (Array.isArray(data)) {
67-
for (const obj of data) {
68-
arr.push(coerceToObject(obj));
69-
}
70-
}
71-
return arr;
72-
}
73-
7464
export default class extends BaseViewProvider<ProviderMessage, WebviewMessage> {
7565
private _state: Map<number, IState> = new Map(); // Map also remembers insertion order :D
7666
private _timeLimit = 0;

src/views/stress/provider/StressViewProvider.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import * as path from "node:path";
22
import * as vscode from "vscode";
33

44
import { Status } from "~common/common";
5-
import type { ILanguageSettings } from "~common/provider";
5+
import {
6+
coerceToArray,
7+
coerceToObject,
8+
type ILanguageSettings,
9+
} from "~common/provider";
610
import BaseViewProvider from "~utils/BaseViewProvider";
711
import { compile, Runnable } from "~utils/runtime";
812
import {
@@ -107,12 +111,9 @@ export default class extends BaseViewProvider<ProviderMessage, WebviewMessage> {
107111
super._postMessage({ type: WebviewMessageType.SHOW, visible: true });
108112

109113
const fileData = super.readStorage()[file];
110-
const state = fileData && Array.isArray(fileData) ? fileData : [];
114+
const state = coerceToArray(fileData);
111115
for (let id = 0; id < state.length; id++) {
112-
const testcase =
113-
state[id] !== "null" && typeof state[id] === "object"
114-
? (state[id] as Partial<IData>)
115-
: {};
116+
const testcase = coerceToObject(state[id]) as Partial<IData>;
116117

117118
this._state[id].data.write(testcase.data ?? "", true);
118119
this._state[id].status = testcase.status ?? Status.NA;

0 commit comments

Comments
 (0)