Use locale dependent number formatting
This commit is contained in:
parent
5447a9a5fc
commit
102227c3a3
@ -242,7 +242,7 @@ module.exports = {
|
|||||||
"no-useless-constructor": "error",
|
"no-useless-constructor": "error",
|
||||||
"no-useless-rename": "error",
|
"no-useless-rename": "error",
|
||||||
"no-useless-return": "error",
|
"no-useless-return": "error",
|
||||||
"no-var": "off",
|
"no-var": "error",
|
||||||
"no-void": "error",
|
"no-void": "error",
|
||||||
"no-warning-comments": "warn",
|
"no-warning-comments": "warn",
|
||||||
"no-whitespace-before-property": "error",
|
"no-whitespace-before-property": "error",
|
||||||
|
@ -73,7 +73,7 @@ export default class uLang {
|
|||||||
getLocaleSpeed(ms, withUnit) {
|
getLocaleSpeed(ms, withUnit) {
|
||||||
const value = Math.round(ms * this.config.factorSpeed * 100) / 100;
|
const value = Math.round(ms * this.config.factorSpeed * 100) / 100;
|
||||||
if (withUnit) {
|
if (withUnit) {
|
||||||
return `${value} ${this.unit('unitSpeed')}`;
|
return `${value.toLocaleString(this.config.lang)} ${this.unit('unitSpeed')}`;
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@ -87,7 +87,7 @@ export default class uLang {
|
|||||||
getLocaleDistanceMajor(m, withUnit) {
|
getLocaleDistanceMajor(m, withUnit) {
|
||||||
const value = Math.round(m * this.config.factorDistanceMajor / 10) / 100;
|
const value = Math.round(m * this.config.factorDistanceMajor / 10) / 100;
|
||||||
if (withUnit) {
|
if (withUnit) {
|
||||||
return `${value} ${this.unit('unitDistanceMajor')}`
|
return `${value.toLocaleString(this.config.lang)} ${this.unit('unitDistanceMajor')}`
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@ -100,7 +100,7 @@ export default class uLang {
|
|||||||
getLocaleDistance(m, withUnit) {
|
getLocaleDistance(m, withUnit) {
|
||||||
const value = Math.round(m * this.config.factorDistance * 100) / 100;
|
const value = Math.round(m * this.config.factorDistance * 100) / 100;
|
||||||
if (withUnit) {
|
if (withUnit) {
|
||||||
return `${value} ${this.unit('unitDistance')}`;
|
return `${value.toLocaleString(this.config.lang)} ${this.unit('unitDistance')}`;
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@ -141,7 +141,7 @@ export default class uLang {
|
|||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
getLocaleCoordinates(pos) {
|
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
|
* @param {boolean} isLon
|
||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
static coordStr(pos, isLon) {
|
coordStr(pos, isLon) {
|
||||||
const ipos = Math.floor(pos);
|
const ipos = Math.trunc(pos);
|
||||||
const dec = ((pos - ipos) * 60).toFixed(1);
|
const dec = Math.abs((pos - ipos) * 60);
|
||||||
let dir;
|
let dir;
|
||||||
|
|
||||||
if (isLon) {
|
if (isLon) {
|
||||||
@ -159,6 +159,6 @@ export default class uLang {
|
|||||||
} else {
|
} else {
|
||||||
dir = pos < 0 ? 'S' : 'N';
|
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}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import TrackFactory from './helpers/trackfactory.js';
|
||||||
import uLang from '../src/lang.js';
|
import uLang from '../src/lang.js';
|
||||||
|
|
||||||
describe('Lang tests', () => {
|
describe('Lang tests', () => {
|
||||||
@ -29,6 +30,7 @@ describe('Lang tests', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
lang = new uLang();
|
lang = new uLang();
|
||||||
mockConfig = {
|
mockConfig = {
|
||||||
|
lang: 'en',
|
||||||
factorSpeed: 0.33,
|
factorSpeed: 0.33,
|
||||||
unitSpeed: 'units',
|
unitSpeed: 'units',
|
||||||
factorDistance: 1.3,
|
factorDistance: 1.3,
|
||||||
@ -129,7 +131,7 @@ describe('Lang tests', () => {
|
|||||||
// when
|
// when
|
||||||
lang.init(mockConfig, mockStrings);
|
lang.init(mockConfig, mockStrings);
|
||||||
// then
|
// 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', () => {
|
it('should return localized altitude value', () => {
|
||||||
@ -143,7 +145,7 @@ describe('Lang tests', () => {
|
|||||||
// when
|
// when
|
||||||
lang.init(mockConfig, mockStrings);
|
lang.init(mockConfig, mockStrings);
|
||||||
// then
|
// 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', () => {
|
it('should return localized accuracy value', () => {
|
||||||
@ -157,7 +159,7 @@ describe('Lang tests', () => {
|
|||||||
// when
|
// when
|
||||||
lang.init(mockConfig, mockStrings);
|
lang.init(mockConfig, mockStrings);
|
||||||
// then
|
// 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', () => {
|
it('should return localized time duration', () => {
|
||||||
@ -174,4 +176,41 @@ describe('Lang tests', () => {
|
|||||||
expect(lang.getLocaleDuration(123456789)).toBe(`1428 ${mockStrings.unitday} 21:33:09`);
|
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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user