MapComplete/src/UI/InputElement/Validators/SlopeValidator.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

44 lines
1.4 KiB
TypeScript
Raw Normal View History

import FloatValidator from "./FloatValidator"
2023-12-14 15:41:34 +01:00
export default class SlopeValidator extends FloatValidator {
2023-12-14 15:41:34 +01:00
constructor() {
super(
"slope",
"Validates that the slope is a valid number." +
"The accompanying input element uses the gyroscope and the compass to determine the correct incline. The sign of the incline will be set automatically. The bearing of the way is compared to the bearing of the compass, as such, the device knows if it is measuring in the forward or backward direction."
)
2023-12-14 15:41:34 +01:00
}
2023-12-14 15:41:34 +01:00
isValid(str: string): boolean {
if (str.endsWith("%") || str.endsWith("°")) {
str = str.substring(0, str.length - 1)
}
return super.isValid(str)
}
/**
*
* const v = new SlopeValidator()
* v.reformat("5%") // => "5%"
* v.reformat("5 %") // => "5%"
* v.reformat(" 5 %") // => "5%"
* v.reformat(" 5.0 %") // => "5%"
* v.reformat(" 5.0 % ") // => "5%"
* v.reformat(" -5.0 % ") // => "-5%"
* v.reformat("5") // => "5"
* v.reformat("5°") // => "5°"
*
*
*
*/
reformat(str: string): string {
str = str.trim()
let lastChar = ""
if (str.endsWith("%") || str.endsWith("°")) {
lastChar = str.substring(str.length - 1)
str = str.substring(0, str.length - 1)
}
return super.reformat(str) + lastChar
}
2023-12-14 15:41:34 +01:00
}