Repository navigation
Expand file tree
/
Copy pathsettings.ts
More file actions
51 lines (46 loc) · 1.37 KB
/
Copy pathsettings.ts
File metadata and controls
51 lines (46 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import type { Settings } from './types';
export const COMMENT_MIN = 1;
export const COMMENT_MAX = 30;
export const DEFAULT_SETTINGS: Settings = {
title: true,
tags: false,
statement: true,
examples: true,
constraints: true,
hints: true,
codeTemplate: true,
currentCode: true,
failingTest: true,
comments: false,
commentCount: 10,
prompt: '',
};
export const clampCommentCount = (n: unknown): number => {
const v = Math.round(Number(n));
return Number.isFinite(v) ? Math.min(COMMENT_MAX, Math.max(COMMENT_MIN, v)) : DEFAULT_SETTINGS.commentCount;
};
/** Turn whatever is in storage (object, JSON string, garbage) into a valid Settings object. */
export function sanitizeSettings(raw: unknown): Settings {
let src: unknown = raw;
if (typeof src === 'string') {
try {
src = JSON.parse(src);
} catch {
src = null;
}
}
const out: Settings = { ...DEFAULT_SETTINGS };
if (typeof src !== 'object' || src === null) return out;
const rec = src as Record<string, unknown>;
for (const key of Object.keys(DEFAULT_SETTINGS) as (keyof Settings)[]) {
const v = rec[key];
if (key === 'prompt') {
if (typeof v === 'string') out.prompt = v;
} else if (key === 'commentCount') {
if (v !== undefined) out.commentCount = clampCommentCount(v);
} else if (typeof v === 'boolean') {
out[key] = v;
}
}
return out;
}