[ML] Creating number utils package for roundToDecimalPlace function (#154910)

`roundToDecimalPlace` is used in ML and data visualiser packages and
soon aiops, so this PR moves it to a shared package.
This commit is contained in:
James Gowdy 2023-04-13 18:53:37 +01:00 committed by GitHub
parent 27d7aa226b
commit c34dae2a0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 178 additions and 165 deletions

View file

@ -0,0 +1,3 @@
# @kbn/ml-number-utils
Utility functions for operations on numbers

View file

@ -0,0 +1,8 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
export { roundToDecimalPlace } from './src/round_to_decimal_place';

View file

@ -0,0 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
module.exports = {
preset: '@kbn/test',
rootDir: '../../../..',
roots: ['<rootDir>/x-pack/packages/ml/number_utils'],
};

View file

@ -0,0 +1,5 @@
{
"type": "shared-common",
"id": "@kbn/ml-number-utils",
"owner": "@elastic/ml-ui"
}

View file

@ -0,0 +1,6 @@
{
"name": "@kbn/ml-number-utils",
"private": true,
"version": "1.0.0",
"license": "Elastic License 2.0"
}

View file

@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { roundToDecimalPlace } from './round_to_decimal_place';
describe('roundToDecimalPlace formatter', () => {
it('returns the correct format using default decimal place', () => {
expect(roundToDecimalPlace(12)).toBe(12);
expect(roundToDecimalPlace(12.3)).toBe(12.3);
expect(roundToDecimalPlace(12.34)).toBe(12.34);
expect(roundToDecimalPlace(12.345)).toBe(12.35);
expect(roundToDecimalPlace(12.045)).toBe(12.05);
expect(roundToDecimalPlace(12.005)).toBe(12.01);
expect(roundToDecimalPlace(12.0005)).toBe(12);
expect(roundToDecimalPlace(0.05)).toBe(0.05);
expect(roundToDecimalPlace(0.005)).toBe('5.00e-3');
expect(roundToDecimalPlace(0.0005)).toBe('5.00e-4');
expect(roundToDecimalPlace(-0.0005)).toBe('-5.00e-4');
expect(roundToDecimalPlace(-12.045)).toBe(-12.04);
expect(roundToDecimalPlace(0)).toBe(0);
});
it('returns the correct format using specified decimal place', () => {
expect(roundToDecimalPlace(12, 4)).toBe(12);
expect(roundToDecimalPlace(12.3, 4)).toBe(12.3);
expect(roundToDecimalPlace(12.3456, 4)).toBe(12.3456);
expect(roundToDecimalPlace(12.345678, 4)).toBe(12.3457);
expect(roundToDecimalPlace(0.05, 4)).toBe(0.05);
expect(roundToDecimalPlace(0.0005, 4)).toBe(0.0005);
expect(roundToDecimalPlace(0.00005, 4)).toBe('5.00e-5');
expect(roundToDecimalPlace(-0.00005, 4)).toBe('-5.00e-5');
expect(roundToDecimalPlace(0, 4)).toBe(0);
});
});

View file

@ -0,0 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
/**
* Rounds a number to a specified decimal place.
* If the specified number is undefined, an empty string is returned.
*
* @param num - number to round
* @param dp - decimal place, default value is 2
* @returns rounded number
*/
export function roundToDecimalPlace(num: number | undefined, dp: number = 2): number | string {
if (num === undefined) return '';
if (num % 1 === 0) {
// no decimal place
return num;
}
if (Math.abs(num) < Math.pow(10, -dp)) {
return Number.parseFloat(String(num)).toExponential(2);
}
const m = Math.pow(10, dp);
return Math.round(num * m) / m;
}

View file

@ -0,0 +1,19 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types",
"types": [
"jest",
"node",
"react"
]
},
"include": [
"**/*.ts",
"**/*.tsx",
],
"exclude": [
"target/**/*"
],
"kbn_references": []
}