utils.digits: Make it return a string in fixed-point notation

This commit is contained in:
Christian Paul 2022-05-01 15:34:57 +02:00
parent 7b80ac883a
commit 922f1ae43f
2 changed files with 31 additions and 19 deletions

View File

@ -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) => {

View File

@ -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,9 +33,8 @@ 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],
@ -43,4 +54,5 @@ describe('normalize', () => {
expect(utils.normalize(input)).toEqual(expected); expect(utils.normalize(input)).toEqual(expected);
}); });
}); });
});
}); });