-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataController.js
100 lines (77 loc) · 2.12 KB
/
DataController.js
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
90
91
92
93
94
95
96
97
98
99
100
//cspell:disable
const pify = require("pify");
const fs = require("fs");
class DataController{
constructor() {
this._assignmentBlocks = new Map(); //store assignment pairs
}
getCount(){
return this._assignmentBlocks.size;
}
contains(ass_obj){
return this._assignmentBlocks.has(ass_obj.sourceText);
}
add(ass_obj){ //of type assignment
if (this.contains(ass_obj)){
console.warn("textBlock is already present.Skip");
return;
}
this._assignmentBlocks.set(ass_obj.sourceText,ass_obj);
}
get(key){ //type string
if (!this._assignmentBlocks.has(key)){
throw new Error("no textblock found that matches sourceText");
}
return this._assignmentBlocks.get(key);
}
update(ass_obj){
if (!this.contains(ass_obj)){
throw new Error("textBlock not found.Skip");
}
this._assignmentBlocks.set(ass_obj.sourceText,ass_obj);
}
getFinished(){
//TODO implement filter
}
remove(ass_obj){
return this._assignmentBlocks.delete(ass_obj.sourceText);
}
//setTextBlock
async saveAssignments(filepath){
let jsonstring = this._mapToJson(this._assignmentBlocks);
try {
await pify(fs.write(filepath, Buffer.from(jsonstring, "utf-8")));
}
catch(e){
console.error(e);
}
}
async loadAssignments(filepath){
try {
let istring = await pify(fs.read(filepath,"utf-8"));
this._assignmentBlocks = this._jsonToMap(istring);
}
catch(e){
console.error(e);
}
}
_mapToJson(map) {
return JSON.stringify([...map]);
}
_jsonToMap(jsonStr) {
return new Map(JSON.parse(jsonStr));
}
}
class Assignment {
constructor(sourceText) {
this.sourceText = sourceText;
this.targetText = null;
}
hasSourceText(){
return !!this.sourceText;
}
}
module.exports = {DataController,Assignment};
//TODO:save to json
//TODO:load from json
//TODO:export2Clipboard