src/lib/HistoryManager.ts   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 1.75

Size

Lines of Code 41
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 7
mnd 3
bc 3
fnc 4
bpm 0.75
cpm 1.75
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A HistoryManager.load 0 11 3
A HistoryManager.for 0 3 1
A HistoryManager.save 0 7 2
A HistoryManager.createEntry 0 5 1
1
import { BoostHistory, BoostHistoryItem } from '@/types/BoostHistory';
2
import { existsSync, readFileSync, writeFileSync } from 'fs';
3
4
export class HistoryManager {
5
    public data: BoostHistory = [];
6
7
    constructor(public filename: string) {
8
        this.load();
9
    }
10
11
    public for(boostName: string): BoostHistory {
12
        return this.data.filter(item => item.boost === boostName);
13
    }
14
15
    public createEntry(item: BoostHistoryItem): BoostHistoryItem {
16
        this.data.push(item);
17
18
        return new Proxy(this.data[this.data.length - 1], {} as any);
19
    }
20
21
    public save() {
22
        if (this.filename.length === 0) {
23
            return;
24
        }
25
26
        writeFileSync(this.filename, JSON.stringify(this.data), { encoding: 'utf8' });
27
    }
28
29
    public load() {
30
        if (this.filename.length === 0) {
31
            return;
32
        }
33
34
        if (!existsSync(this.filename)) {
35
            this.save();
36
        }
37
38
        this.data = JSON.parse(readFileSync(this.filename, { encoding: 'utf8' }));
39
    }
40
}
41