add cli flag to limit position precision to x decimal places for v2.4 firmware and older

This commit is contained in:
liamcottle
2024-10-02 01:52:03 +13:00
parent 759567917a
commit 9bebc870bd
5 changed files with 3514 additions and 9 deletions

View File

@ -0,0 +1,44 @@
class PositionUtil {
/**
* Truncates the provided latitude or longitude to a maximum of x decimal places
* e.g: 12.3456789 with 2 decimal places would be 12.34
* @param latitudeOrLongitudeString e.g: 12.3456789
* @param numberOfDecimalPlaces how many decimal places to allow in the result
* @returns {*|string|null}
*/
static truncateDecimalPlaces(latitudeOrLongitudeString, numberOfDecimalPlaces) {
// ensure value not null
if(latitudeOrLongitudeString == null){
return null;
}
// split into left and right side of decimal point
// e.g: -12.3456789 -> [-12, 3456789]
var [ leftOfDecimalPoint, rightOfDecimalPoint ] = latitudeOrLongitudeString.split(".");
// check if decimal places available
if(rightOfDecimalPoint != null){
// truncate decimal places to desired length
rightOfDecimalPoint = rightOfDecimalPoint.substring(0, numberOfDecimalPlaces);
// return modified position with decimal places, if available
if(rightOfDecimalPoint.length > 0){
return [ leftOfDecimalPoint, rightOfDecimalPoint ].join(".");
}
// no decimal places available anymore, return left side without dot
return leftOfDecimalPoint;
}
// decimal places not available, return position as is
return latitudeOrLongitudeString;
}
}
module.exports = PositionUtil;

View File

@ -0,0 +1,18 @@
const PositionUtil = require("./position_util");
test('can truncate string position to provided decimal places', () => {
expect(PositionUtil.truncateDecimalPlaces("12.3456789", 0)).toBe("12");
expect(PositionUtil.truncateDecimalPlaces("12.3456789", 1)).toBe("12.3");
expect(PositionUtil.truncateDecimalPlaces("12.3456789", 2)).toBe("12.34");
expect(PositionUtil.truncateDecimalPlaces("12.3456789", 3)).toBe("12.345");
expect(PositionUtil.truncateDecimalPlaces("12.3456789", 4)).toBe("12.3456");
expect(PositionUtil.truncateDecimalPlaces("12.3456789", 5)).toBe("12.34567");
expect(PositionUtil.truncateDecimalPlaces("12.3456789", 6)).toBe("12.345678");
expect(PositionUtil.truncateDecimalPlaces("12.3456789", 7)).toBe("12.3456789");
expect(PositionUtil.truncateDecimalPlaces("12.3", 7)).toBe("12.3");
expect(PositionUtil.truncateDecimalPlaces(null, 2)).toBe(null);
expect(PositionUtil.truncateDecimalPlaces("", 2)).toBe("");
expect(PositionUtil.truncateDecimalPlaces("12", 2)).toBe("12");
expect(PositionUtil.truncateDecimalPlaces("123", 2)).toBe("123");
expect(PositionUtil.truncateDecimalPlaces("1234.", 2)).toBe("1234");
});