mirror of
https://github.com/rastapasta/mapscii.git
synced 2025-03-20 00:46:38 +01:00
utils.digits: Make it return a string in fixed-point notation
This commit is contained in:
parent
7b80ac883a
commit
922f1ae43f
@ -75,7 +75,7 @@ export const hex2rgb = (color) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const digits = (number, digits) => {
|
export const digits = (number, digits) => {
|
||||||
return Math.floor(number*Math.pow(10, digits))/Math.pow(10, digits);
|
return (Math.floor(number*Math.pow(10, digits))/Math.pow(10, digits)).toFixed(digits);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const normalize = (ll) => {
|
export const normalize = (ll) => {
|
||||||
|
@ -1,6 +1,18 @@
|
|||||||
import * as utils from './utils.js';
|
import * as utils from './utils.js';
|
||||||
|
|
||||||
describe('utils', () => {
|
describe('utils', () => {
|
||||||
|
describe('digits', () => {
|
||||||
|
describe.each([
|
||||||
|
[1, 0, '1'],
|
||||||
|
[1, 1, '1.0'],
|
||||||
|
[3.1415, 3, '3.141'],
|
||||||
|
])('when given value=%f and digits=%f', (value, digits, expected_value) => {
|
||||||
|
test(`returns ${expected_value}`, () => {
|
||||||
|
expect(utils.digits(value, digits)).toEqual(expected_value);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('hex2rgb', () => {
|
describe('hex2rgb', () => {
|
||||||
describe.each([
|
describe.each([
|
||||||
['#ff0000', 255, 0, 0],
|
['#ff0000', 255, 0, 0],
|
||||||
@ -21,26 +33,26 @@ describe('utils', () => {
|
|||||||
expect(wrapper).toThrow('isn\'t a supported hex color');
|
expect(wrapper).toThrow('isn\'t a supported hex color');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
describe('normalize', () => {
|
describe('normalize', () => {
|
||||||
describe.each([
|
describe.each([
|
||||||
[0, 0, 0, 0],
|
[0, 0, 0, 0],
|
||||||
[61, 48, 61, 48],
|
[61, 48, 61, 48],
|
||||||
[-61, -48, -61, -48],
|
[-61, -48, -61, -48],
|
||||||
[181, 85.06, -179, 85.0511],
|
[181, 85.06, -179, 85.0511],
|
||||||
[-181, -85.06, 179, -85.0511],
|
[-181, -85.06, 179, -85.0511],
|
||||||
])('when given lon=%f and lat=%f', (lon, lat, expected_lon, expected_lat) => {
|
])('when given lon=%f and lat=%f', (lon, lat, expected_lon, expected_lat) => {
|
||||||
const input = {
|
const input = {
|
||||||
lon,
|
lon,
|
||||||
lat,
|
lat,
|
||||||
};
|
|
||||||
test(`returns lon=${expected_lon} and lat=${expected_lat}`, () => {
|
|
||||||
const expected = {
|
|
||||||
lon: expected_lon,
|
|
||||||
lat: expected_lat,
|
|
||||||
};
|
};
|
||||||
expect(utils.normalize(input)).toEqual(expected);
|
test(`returns lon=${expected_lon} and lat=${expected_lat}`, () => {
|
||||||
|
const expected = {
|
||||||
|
lon: expected_lon,
|
||||||
|
lat: expected_lat,
|
||||||
|
};
|
||||||
|
expect(utils.normalize(input)).toEqual(expected);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user