@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
64 lines • 2.88 kB
JavaScript
/***
* d888b d88888b d888888b .d8888. d888888b d88888D d88888b db .d8b. d8888b. d88888b db
* 88' Y8b 88' `~~88~~' 88' YP `88' YP d8' 88' 88 d8' `8b 88 `8D 88' 88
* 88 88ooooo 88 `8bo. 88 d8' 88ooooo 88 88ooo88 88oooY' 88ooooo 88
* 88 ooo 88~~~~~ 88 `Y8b. 88 d8' 88~~~~~ 88 88~~~88 88~~~b. 88~~~~~ 88
* 88. ~8~ 88. 88 db 8D .88. d8' db 88. 88booo. 88 88 88 8D 88. 88booo.
* Y888P Y88888P YP `8888Y' Y888888P d88888P Y88888P Y88888P YP YP Y8888P' Y88888P Y88888P
*
* import { getSizeLabel } from '@mikezimm/npmfunctions/dist/Services/Strings/stringServices';
*/
/**
* Converts a number into a label, used in eXTreme Storage to show 100 MB and 1.23 GB sizes
* @param size
* @param decimal
* @returns
*/
const oneKB = 1024;
const oneMB = 1024 * oneKB;
const oneGB = 1024 * oneMB;
/**
* NOTE: If you pass a string, it will first parseInt it so BE SURE TO VERIFY you are not passing in
* something like 1.24MB or it will return as 1.24B
*
* @param size
* @param decimal
* @returns
*/
export function getSizeLabel(size, decimal = 1) {
if (size === null || size === undefined) {
return '';
}
// 2024-12-04: Found in PivotTiles that the FileSizeDisplay is a string like '12341234'.
// Not sure if I should just parseInt the string or check for other chars like GB etc first.
if (typeof size === 'string')
size = parseInt(size);
return size > oneGB ? `${(size / oneGB).toFixed(decimal)} GB` : size > oneMB ? `${(size / oneMB).toFixed(decimal)} MB` : size > oneKB ? `${(size / oneKB).toFixed(decimal)} KB` : `${(size).toFixed(decimal)} B`;
}
/**
* Converts a number into a label, used in eXTreme Storage to show 1.6M and 2.3G sizes
* @param size
* @param decimal
* @returns
*/
export function getCountLabel(count, decimal = 1) {
if (count === null || count === undefined) {
return '';
}
// https://github.com/mikezimm/drilldown7/issues/355 changed final .toFixed( decimal ) to .toFixed ( count % 1 === 0 ? 0 : decimal )
// This logic will return an integer if the value does not have any decimals reducing unneccessary decimals and noise
return count > 1e9 ? `${(count / 1e9).toFixed(decimal)} G` :
count > 1e6 ? `${(count / 1e6).toFixed(decimal)} M` :
count > 1e3 ? `${(count / 1e3).toFixed(decimal)} k` :
`${(count).toFixed(count % 1 === 0 ? 0 : decimal)}`;
}
/**
* Returns string with 1000's separated format.
* For US: 1,000,000
* For EU: 1.000.000
* @param numb
*/
export function getCommaSepLabel(numb) {
return new Intl.NumberFormat().format(numb);
}
//# sourceMappingURL=labels.js.map