-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataHandler.js
98 lines (73 loc) · 2.5 KB
/
DataHandler.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
/* DataHandler.js: handles the formatting of data
* Project: "clock-for-steelseries-oled"
*/
class DataHandler {
constructor() { }
/* Remove all occurences of a character from string
In: string, string | RegExp
Out: string */
removeCharsFromString(oldString, charToRemove) {
if(typeof oldString !== 'string') {
console.log("Input is not a string!");
return "";
}
const replacer = function () {
return "";
}
// make regexp
const re = new RegExp(charToRemove, 'g');
// replace matches
const newString = oldString.replace(re, replacer);
return newString;
}
/* In: any value
Out: JSON string or null if JSON throws error */
stringToJSON(value) {
let jsonData;
try {
jsonData = JSON.stringify(value);
}
catch (e) {
console.error(e);
return null;
}
return jsonData;
}
/* Convert string to positive integer
and change its value from scale 0...oldMax to 0...newMax
In: number as (string), current maximum (number), new maximum (number)
Out: integer (number) */
normalizeNumberFromString(numberString, oldMax, newMax) {
if(oldMax === 0) {
return 0;
}
let newNumber = Math.round(Number.parseInt(numberString) / oldMax * newMax);
if (Number.isNaN(newNumber)) {
return 0;
}
return newNumber;
}
/* Form JSON from data string: locale time string with format hh-mm-ss, where
hh: 0...9 | 10...23,
- : any character but 0...9,
mm: 00...59,
ss: 00...59
In: string, ClockData messageData property
Out: JSON formatted string */
formatJSONtimeString(data, message) {
let numberDataValue = 0;
if (typeof data !== 'string') {
data = data.toString();
}
if (message.data !== undefined) {
// fill progress bar with integer 0...100
let delimiter = data[data.search(/[^0-9]/)];
numberDataValue = this.normalizeNumberFromString(
data.slice(data.lastIndexOf(delimiter) + 1), 60, 100);
message.data["value"] = numberDataValue;
// time data: hour & min as text
message.data["frame"]["textvalue"] = data.slice(0, data.lastIndexOf(delimiter));
}
return this.stringToJSON(message);
}
} export default DataHandler;