From 0f3989a1701e15e8a3dfbda2b41f91631d0b1bcf Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Sun, 16 Feb 2020 11:09:33 +0100 Subject: [PATCH] Increase rounding precision, especially speed --- js/src/config.js | 12 ++++++------ js/src/lang.js | 2 +- js/src/position.js | 2 +- js/src/utils.js | 2 +- js/test/config.test.js | 10 +++++----- js/test/lang.test.js | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/js/src/config.js b/js/src/config.js index d1cdf49..0c00701 100644 --- a/js/src/config.js +++ b/js/src/config.js @@ -69,21 +69,21 @@ export default class uConfig { initUnits() { if (this.units === 'imperial') { - this.factorSpeed = 0.62; // to mph + this.factorSpeed = 2.237; // m/s to mph this.unitSpeed = 'unitmph'; - this.factorDistance = 3.28; // to feet + this.factorDistance = 3.28; // m to feet this.unitDistance = 'unitft'; - this.factorDistanceMajor = 0.62; // to miles + this.factorDistanceMajor = 0.621; // km to miles this.unitDistanceMajor = 'unitmi'; } else if (this.units === 'nautical') { - this.factorSpeed = 0.54; // to knots + this.factorSpeed = 1.944; // m/s to kt this.unitSpeed = 'unitkt'; this.factorDistance = 1; // meters this.unitDistance = 'unitm'; - this.factorDistanceMajor = 0.54; // to nautical miles + this.factorDistanceMajor = 0.54; // km to nautical miles this.unitDistanceMajor = 'unitnm'; } else { - this.factorSpeed = 1; + this.factorSpeed = 3.6; // m/s to km/h this.unitSpeed = 'unitkmh'; this.factorDistance = 1; this.unitDistance = 'unitm'; diff --git a/js/src/lang.js b/js/src/lang.js index f1b0e4f..420faba 100644 --- a/js/src/lang.js +++ b/js/src/lang.js @@ -71,7 +71,7 @@ export default class uLang { * @return {(number|string)} String when with unit */ getLocaleSpeed(ms, withUnit) { - const value = Math.round(ms * this.config.factorSpeed * 360) / 100; + const value = Math.round(ms * this.config.factorSpeed * 100) / 100; if (withUnit) { return `${value} ${this.unit('unitSpeed')}`; } diff --git a/js/src/position.js b/js/src/position.js index ce467c0..8371c30 100644 --- a/js/src/position.js +++ b/js/src/position.js @@ -54,7 +54,7 @@ export default class uPosition { position.latitude = uUtils.getFloat(pos.latitude); position.longitude = uUtils.getFloat(pos.longitude); position.altitude = uUtils.getInteger(pos.altitude, true); // may be null - position.speed = uUtils.getInteger(pos.speed, true); // may be null + position.speed = uUtils.getFloat(pos.speed, true); // may be null position.bearing = uUtils.getInteger(pos.bearing, true); // may be null position.accuracy = uUtils.getInteger(pos.accuracy, true); // may be null position.provider = uUtils.getString(pos.provider, true); // may be null diff --git a/js/src/utils.js b/js/src/utils.js index cf6b497..b6ade2d 100644 --- a/js/src/utils.js +++ b/js/src/utils.js @@ -256,7 +256,7 @@ export default class uUtils { output = parseFloat(input); break; case 'int': - output = parseInt(input); + output = Math.round(parseFloat(input)); break; case 'string': output = String(input); diff --git a/js/test/config.test.js b/js/test/config.test.js index 6896a52..f76a546 100644 --- a/js/test/config.test.js +++ b/js/test/config.test.js @@ -60,11 +60,11 @@ describe('Config tests', () => { config.units = 'imperial'; config.initUnits(); // then - expect(config.factorSpeed).toBe(0.62); // to mph + expect(config.factorSpeed).toBe(2.237); // to mph expect(config.unitSpeed).toBe('unitmph'); expect(config.factorDistance).toBe(3.28); // to feet expect(config.unitDistance).toBe('unitft'); - expect(config.factorDistanceMajor).toBe(0.62); // to miles + expect(config.factorDistanceMajor).toBe(0.621); // to miles expect(config.unitDistanceMajor).toBe('unitmi'); }); @@ -73,7 +73,7 @@ describe('Config tests', () => { config.units = 'nautical'; config.initUnits(); // then - expect(config.factorSpeed).toBe(0.54); // to knots + expect(config.factorSpeed).toBe(1.944); // to knots expect(config.unitSpeed).toBe('unitkt'); expect(config.factorDistance).toBe(1); // meters expect(config.unitDistance).toBe('unitm'); @@ -86,7 +86,7 @@ describe('Config tests', () => { config.units = 'metric'; config.initUnits(); // then - expect(config.factorSpeed).toBe(1); + expect(config.factorSpeed).toBe(3.6); expect(config.unitSpeed).toBe('unitkmh'); expect(config.factorDistance).toBe(1); expect(config.unitDistance).toBe('unitm'); @@ -125,7 +125,7 @@ describe('Config tests', () => { // when config.load(data); // then - expect(config.factorSpeed).toBe(0.62); + expect(config.factorSpeed).toBe(2.237); }); it('should parse regex if present in data', () => { diff --git a/js/test/lang.test.js b/js/test/lang.test.js index 7d1838a..129632d 100644 --- a/js/test/lang.test.js +++ b/js/test/lang.test.js @@ -94,14 +94,14 @@ describe('Lang tests', () => { // when lang.init(mockConfig, mockStrings); // then - expect(lang.getLocaleSpeed(value, false)).toBe(1188); + expect(lang.getLocaleSpeed(value, false)).toBe(330); }); it('should return localized speed value with unit', () => { // when lang.init(mockConfig, mockStrings); // then - expect(lang.getLocaleSpeed(value, true)).toBe(`1188 ${mockStrings.units}`); + expect(lang.getLocaleSpeed(value, true)).toBe(`330 ${mockStrings.units}`); }); it('should return localized distance major value', () => {