Use locale dependent number formatting

This commit is contained in:
Bartek Fabiszewski 2020-02-16 15:22:59 +01:00
parent 5447a9a5fc
commit 102227c3a3
3 changed files with 51 additions and 12 deletions

View File

@ -242,7 +242,7 @@ module.exports = {
"no-useless-constructor": "error",
"no-useless-rename": "error",
"no-useless-return": "error",
"no-var": "off",
"no-var": "error",
"no-void": "error",
"no-warning-comments": "warn",
"no-whitespace-before-property": "error",

View File

@ -73,7 +73,7 @@ export default class uLang {
getLocaleSpeed(ms, withUnit) {
const value = Math.round(ms * this.config.factorSpeed * 100) / 100;
if (withUnit) {
return `${value} ${this.unit('unitSpeed')}`;
return `${value.toLocaleString(this.config.lang)} ${this.unit('unitSpeed')}`;
}
return value;
}
@ -87,7 +87,7 @@ export default class uLang {
getLocaleDistanceMajor(m, withUnit) {
const value = Math.round(m * this.config.factorDistanceMajor / 10) / 100;
if (withUnit) {
return `${value} ${this.unit('unitDistanceMajor')}`
return `${value.toLocaleString(this.config.lang)} ${this.unit('unitDistanceMajor')}`
}
return value;
}
@ -100,7 +100,7 @@ export default class uLang {
getLocaleDistance(m, withUnit) {
const value = Math.round(m * this.config.factorDistance * 100) / 100;
if (withUnit) {
return `${value} ${this.unit('unitDistance')}`;
return `${value.toLocaleString(this.config.lang)} ${this.unit('unitDistance')}`;
}
return value;
}
@ -141,7 +141,7 @@ export default class uLang {
* @return {string}
*/
getLocaleCoordinates(pos) {
return `${uLang.coordStr(pos.longitude, true)} ${uLang.coordStr(pos.latitude, false)}`;
return `${this.coordStr(pos.longitude, true)} ${this.coordStr(pos.latitude, false)}`;
}
/**
@ -149,9 +149,9 @@ export default class uLang {
* @param {boolean} isLon
* @return {string}
*/
static coordStr(pos, isLon) {
const ipos = Math.floor(pos);
const dec = ((pos - ipos) * 60).toFixed(1);
coordStr(pos, isLon) {
const ipos = Math.trunc(pos);
const dec = Math.abs((pos - ipos) * 60);
let dir;
if (isLon) {
@ -159,6 +159,6 @@ export default class uLang {
} else {
dir = pos < 0 ? 'S' : 'N';
}
return `${ipos}° ${dec}'${dir}`;
return `${Math.abs(ipos).toLocaleString(this.config.lang)}°${dec.toLocaleString(this.config.lang, { maximumFractionDigits: 2 })}'${dir}`;
}
}

View File

@ -17,6 +17,7 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
import TrackFactory from './helpers/trackfactory.js';
import uLang from '../src/lang.js';
describe('Lang tests', () => {
@ -29,6 +30,7 @@ describe('Lang tests', () => {
beforeEach(() => {
lang = new uLang();
mockConfig = {
lang: 'en',
factorSpeed: 0.33,
unitSpeed: 'units',
factorDistance: 1.3,
@ -129,7 +131,7 @@ describe('Lang tests', () => {
// when
lang.init(mockConfig, mockStrings);
// then
expect(lang.getLocaleDistance(value, true)).toBe(`1300 ${mockStrings.unitd}`);
expect(lang.getLocaleDistance(value, true)).toBe(`1,300 ${mockStrings.unitd}`);
});
it('should return localized altitude value', () => {
@ -143,7 +145,7 @@ describe('Lang tests', () => {
// when
lang.init(mockConfig, mockStrings);
// then
expect(lang.getLocaleDistance(value, true)).toBe(`1300 ${mockStrings.unitd}`);
expect(lang.getLocaleDistance(value, true)).toBe(`1,300 ${mockStrings.unitd}`);
});
it('should return localized accuracy value', () => {
@ -157,7 +159,7 @@ describe('Lang tests', () => {
// when
lang.init(mockConfig, mockStrings);
// then
expect(lang.getLocaleDistance(value, true)).toBe(`1300 ${mockStrings.unitd}`);
expect(lang.getLocaleDistance(value, true)).toBe(`1,300 ${mockStrings.unitd}`);
});
it('should return localized time duration', () => {
@ -174,4 +176,41 @@ describe('Lang tests', () => {
expect(lang.getLocaleDuration(123456789)).toBe(`1428 ${mockStrings.unitday} 21:33:09`);
});
it('should return localized coordinates', () => {
// when
lang.init(mockConfig, mockStrings);
const testCoord = 'coord';
spyOn(lang, 'coordStr').and.returnValue(testCoord);
// then
expect(lang.getLocaleCoordinates(TrackFactory.getPosition())).toBe(`${testCoord} ${testCoord}`);
});
it('should return localized coordinate for locale "en"', () => {
// when
lang.init(mockConfig, mockStrings);
// then
expect(lang.coordStr(1.111, true)).toBe('1°6.66\'E');
expect(lang.coordStr(1.111, false)).toBe('1°6.66\'N');
expect(lang.coordStr(171.11, true)).toBe('171°6.6\'E');
expect(lang.coordStr(81.11, false)).toBe('81°6.6\'N');
expect(lang.coordStr(-1.111, true)).toBe('1°6.66\'W');
expect(lang.coordStr(-1.111, false)).toBe('1°6.66\'S');
expect(lang.coordStr(0, true)).toBe('0°0\'E');
expect(lang.coordStr(0, false)).toBe('0°0\'N');
expect(lang.coordStr(-0.01, true)).toBe('0°0.6\'W');
expect(lang.coordStr(-0.01, false)).toBe('0°0.6\'S');
});
it('should return localized coordinate for locale "pl"', () => {
// when
mockConfig.lang = 'pl';
lang.init(mockConfig, mockStrings);
// then
expect(lang.coordStr(1.111, true)).toBe('1°6,66\'E');
expect(lang.coordStr(1.111, false)).toBe('1°6,66\'N');
});
});