-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathconsistValidation.ts
78 lines (63 loc) · 1.78 KB
/
consistValidation.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
import { kgToT, msToKmh } from 'utils/physics';
export const CONSIST_TOTAL_MASS_MAX = 10000; // ton
export const CONSIST_TOTAL_LENGTH_MAX = 750; // m
export const CONSIST_MAX_SPEED_MIN = 30; // km/h
export const validateTotalMass = ({
tractionEngineMass = 0,
towedMass = 0,
totalMass,
}: {
tractionEngineMass?: number;
towedMass?: number;
totalMass?: number;
}) => {
if (!totalMass) {
return undefined;
}
if (totalMass <= 0) {
return 'consist.errors.totalMass.negative';
}
const tractionMassInTons = kgToT(tractionEngineMass);
const consistMassInTons = kgToT(tractionEngineMass + towedMass);
const massLimit = towedMass ? consistMassInTons : tractionMassInTons;
if (totalMass < massLimit || totalMass >= CONSIST_TOTAL_MASS_MAX) {
return 'consist.errors.totalMass.range';
}
return undefined;
};
export const validateTotalLength = ({
tractionEngineLength = 0,
towedLength = 0,
totalLength,
}: {
tractionEngineLength?: number;
towedLength?: number;
totalLength?: number;
}) => {
if (!totalLength) {
return undefined;
}
if (totalLength <= 0) {
return 'consist.errors.totalLength.negative';
}
const consistLength = Math.floor(tractionEngineLength + towedLength);
if (totalLength < consistLength || totalLength >= CONSIST_TOTAL_LENGTH_MAX) {
return 'consist.errors.totalLength.range';
}
return undefined;
};
export const validateMaxSpeed = (maxSpeed?: number, tractionEngineMaxSpeed?: number) => {
if (!maxSpeed) {
return undefined;
}
if (maxSpeed <= 0) {
return 'consist.errors.maxSpeed.negative';
}
if (
maxSpeed < CONSIST_MAX_SPEED_MIN ||
(tractionEngineMaxSpeed && maxSpeed > Math.floor(msToKmh(tractionEngineMaxSpeed)))
) {
return 'consist.errors.maxSpeed.range';
}
return undefined;
};