-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.ts
More file actions
73 lines (62 loc) · 2.05 KB
/
parser.ts
File metadata and controls
73 lines (62 loc) · 2.05 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
export const parseReadingsFromGoogleText = (input: string): { ok: boolean, value: number } => {
if (!input) {
return {ok: false, value: 0}
}
const resultStrategy1 = strategy1(input);
if (resultStrategy1.ok) {
return resultStrategy1;
}
const resultStrategy2 = strategy2(input);
return resultStrategy2;
}
/**
* Reading is on the same line as 'M15' string
*/
const strategy1 = (input: string): { ok: boolean, value: number } => {
const splitted = input.split('\n')
const index = splitted.findIndex(s => s === 'M15') + 1 // newline after 'M15' contains reading on my photo's
const line = splitted[index]
return parseRawReading(line);
}
/**
* Reading is on the next line after 'M15' string
*/
const strategy2 = (input: string): { ok: boolean, value: number } => {
const splitted = input.split('\n')
const index = splitted.findIndex(s => s.startsWith('M15')) // newline after 'M15' contains reading on my photo's
const line = splitted[index]
return parseRawReading(line);
}
/**
* Examples:
* - "0 0 2 7:3:3"
* - "0 0 2 7:3"
* - "0 0.2 7:3"
*
* Or sometimes, it prepends the other numbers on the same line,
* So make sure we only start parsing from the first zero
* - "1383 0.0.2.7.3"
*
* Sometimes, the 'M15' string is put as prefix:
* - "M15 00274770"
*/
const parseRawReading = (line: string): { ok: boolean, value: number } => {
if (!line) {
return {ok: false, value: 0}
}
const numbers = line.match(/\d+/g)
if (!numbers || numbers.length === 0) {
return {ok: false, value: 0}
}
const numberString = numbers.join('')
// first remove all numbers BEFORE the first 0.
const zeroIndex = numberString.indexOf('0')
const newNumberString = numberString.substr(zeroIndex)
if (newNumberString.length < 5) { // uncertain response, return false with result
return {ok: false, value: Number(newNumberString)}
}
if (numberString.length === 5) {
return {ok: true, value: Number(newNumberString)}
}
return {ok: true, value: Number(newNumberString.substr(0, 5))} // don't care about values after the comma
}