bigbuffet-rw/app/soapbox/utils/numbers.tsx

48 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-04-24 12:28:07 -07:00
import React from 'react';
import { FormattedNumber } from 'react-intl';
/** Check if a value is REALLY a number. */
2022-04-28 09:36:45 -07:00
export const isNumber = (value: unknown): value is number => typeof value === 'number' && !isNaN(value);
2022-04-24 12:28:07 -07:00
export const secondsToDays = (seconds: number) => Math.floor(seconds / (3600 * 24));
2022-08-24 11:46:40 -07:00
const roundDown = (num: number) => {
if (num >= 100 && num < 1000) {
num = Math.floor(num);
}
const n = Number(num.toFixed(2));
return (n > num) ? n - (1 / (Math.pow(10, 2))) : n;
};
2022-04-24 12:28:07 -07:00
/** Display a number nicely for the UI, eg 1000 becomes 1K. */
export const shortNumberFormat = (number: any): React.ReactNode => {
if (!isNumber(number)) return '•';
2022-08-24 11:46:40 -07:00
let value = number;
let factor: string = '';
if (number >= 1000 && number < 1000000) {
factor = 'k';
value = roundDown(value / 1000);
} else if (number >= 1000000) {
factor = 'M';
value = roundDown(value / 1000000);
2022-04-24 12:28:07 -07:00
}
2022-08-24 11:46:40 -07:00
return (
<span>
<FormattedNumber
value={value}
maximumFractionDigits={0}
minimumFractionDigits={0}
maximumSignificantDigits={3}
style='decimal'
/>
{factor}
</span>
);
2022-04-24 12:28:07 -07:00
};
/** Check if an entity ID is an integer (eg not a FlakeId). */
export const isIntegerId = (id: string): boolean => new RegExp(/^-?[0-9]+$/g).test(id);