mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[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:
parent
27d7aa226b
commit
c34dae2a0e
25 changed files with 178 additions and 165 deletions
3
x-pack/packages/ml/number_utils/README.md
Normal file
3
x-pack/packages/ml/number_utils/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# @kbn/ml-number-utils
|
||||
|
||||
Utility functions for operations on numbers
|
8
x-pack/packages/ml/number_utils/index.ts
Normal file
8
x-pack/packages/ml/number_utils/index.ts
Normal 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';
|
12
x-pack/packages/ml/number_utils/jest.config.js
Normal file
12
x-pack/packages/ml/number_utils/jest.config.js
Normal 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'],
|
||||
};
|
5
x-pack/packages/ml/number_utils/kibana.jsonc
Normal file
5
x-pack/packages/ml/number_utils/kibana.jsonc
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"type": "shared-common",
|
||||
"id": "@kbn/ml-number-utils",
|
||||
"owner": "@elastic/ml-ui"
|
||||
}
|
6
x-pack/packages/ml/number_utils/package.json
Normal file
6
x-pack/packages/ml/number_utils/package.json
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"name": "@kbn/ml-number-utils",
|
||||
"private": true,
|
||||
"version": "1.0.0",
|
||||
"license": "Elastic License 2.0"
|
||||
}
|
|
@ -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);
|
||||
});
|
||||
});
|
|
@ -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;
|
||||
}
|
19
x-pack/packages/ml/number_utils/tsconfig.json
Normal file
19
x-pack/packages/ml/number_utils/tsconfig.json
Normal file
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"extends": "../../../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "target/types",
|
||||
"types": [
|
||||
"jest",
|
||||
"node",
|
||||
"react"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"**/*.ts",
|
||||
"**/*.tsx",
|
||||
],
|
||||
"exclude": [
|
||||
"target/**/*"
|
||||
],
|
||||
"kbn_references": []
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue