-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptimize.ts
89 lines (80 loc) · 2.76 KB
/
optimize.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { IOptimizeOptionsDifficulty, IOptimizeOptionsInfo } from './types/bsmap/optimize.ts';
import logger from './logger.ts';
import {
cleanDifficulty as cleanV1Difficulty,
cleanInfo as cleanV1Info,
} from './beatmap/v1/clean.ts';
import {
cleanDifficulty as cleanV2Difficulty,
cleanInfo as cleanV2Info,
} from './beatmap/v2/clean.ts';
import { cleanDifficulty as cleanV3Difficulty } from './beatmap/v3/clean.ts';
import { ICleanOptions } from './types/beatmap/shared/clean.ts';
function tag(name: string): string[] {
return ['optimize', name];
}
const optionsInfo: Required<IOptimizeOptionsInfo> = {
enabled: true,
floatTrim: 4,
stringTrim: true,
purgeZeros: true,
throwError: true,
};
const optionsDifficulty: Required<IOptimizeOptionsDifficulty> = {
enabled: true,
floatTrim: 4,
stringTrim: true,
purgeZeros: true,
throwError: true,
};
/** Set default option value for optimize function. */
export const defaultOptions = {
info: optionsInfo,
difficulty: optionsDifficulty,
};
export function info(
// deno-lint-ignore no-explicit-any
info: Record<string, any>,
version: number,
options: IOptimizeOptionsInfo = {},
) {
const opt: Required<IOptimizeOptionsInfo> = {
enabled: options.enabled ?? defaultOptions.info.enabled,
floatTrim: options.floatTrim ?? defaultOptions.info.floatTrim,
stringTrim: options.stringTrim ?? defaultOptions.info.stringTrim,
purgeZeros: options.purgeZeros ?? defaultOptions.info.purgeZeros,
throwError: options.throwError ?? defaultOptions.info.throwError,
};
logger.tInfo(tag('info'), `Optimising info data`);
// deno-lint-ignore no-explicit-any
const clean: (data: any, opt: ICleanOptions) => void = version === 2
? cleanV2Info
: version === 1
? cleanV1Info
: () => {};
clean(info, opt);
}
export function difficulty(
// deno-lint-ignore no-explicit-any
difficulty: Record<string, any>,
version: number,
options: IOptimizeOptionsDifficulty = {},
) {
const opt: Required<IOptimizeOptionsDifficulty> = {
enabled: options.enabled ?? defaultOptions.difficulty.enabled,
floatTrim: options.floatTrim ?? defaultOptions.difficulty.floatTrim,
stringTrim: options.stringTrim ?? defaultOptions.difficulty.stringTrim,
purgeZeros: options.purgeZeros ?? defaultOptions.difficulty.purgeZeros,
throwError: options.throwError ?? defaultOptions.difficulty.throwError,
};
logger.tInfo(tag('difficulty'), `Optimising difficulty data`);
// deno-lint-ignore no-explicit-any
const clean: (data: any, opt: ICleanOptions) => void = version === 3
? cleanV3Difficulty
: version === 2
? cleanV2Difficulty
: version === 1
? cleanV1Difficulty
: () => {};
clean(difficulty, opt);
}