From 922f1ae43f3661b4ecd6e3d957c95fe9e0bd799b Mon Sep 17 00:00:00 2001 From: Christian Paul Date: Sun, 1 May 2022 15:34:57 +0200 Subject: [PATCH] utils.digits: Make it return a string in fixed-point notation --- src/utils.js | 2 +- src/utils.spec.js | 48 +++++++++++++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/utils.js b/src/utils.js index b136c7e..ff2e791 100644 --- a/src/utils.js +++ b/src/utils.js @@ -75,7 +75,7 @@ export const hex2rgb = (color) => { }; 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) => { diff --git a/src/utils.spec.js b/src/utils.spec.js index 6ddbd17..4e3bdfe 100644 --- a/src/utils.spec.js +++ b/src/utils.spec.js @@ -1,6 +1,18 @@ import * as utils from './utils.js'; 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.each([ ['#ff0000', 255, 0, 0], @@ -21,26 +33,26 @@ describe('utils', () => { expect(wrapper).toThrow('isn\'t a supported hex color'); }); }); -}); -describe('normalize', () => { - describe.each([ - [0, 0, 0, 0], - [61, 48, 61, 48], - [-61, -48, -61, -48], - [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) => { - const input = { - lon, - lat, - }; - test(`returns lon=${expected_lon} and lat=${expected_lat}`, () => { - const expected = { - lon: expected_lon, - lat: expected_lat, + describe('normalize', () => { + describe.each([ + [0, 0, 0, 0], + [61, 48, 61, 48], + [-61, -48, -61, -48], + [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) => { + const input = { + lon, + 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); + }); }); }); });