mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 04:27:59 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
import type { BN } from '../bn/bn.js';
|
||||
import type { SiDef, ToBn } from '../types.js';
|
||||
interface Defaults {
|
||||
decimals: number;
|
||||
unit: string;
|
||||
}
|
||||
interface SetDefaults {
|
||||
decimals?: number[] | number;
|
||||
unit?: string[] | string;
|
||||
}
|
||||
interface Options {
|
||||
/**
|
||||
* @description The number of decimals
|
||||
*/
|
||||
decimals?: number;
|
||||
/**
|
||||
* @description Format the number with this specific unit
|
||||
*/
|
||||
forceUnit?: string;
|
||||
/**
|
||||
* @description Returns value using all available decimals
|
||||
*/
|
||||
withAll?: boolean;
|
||||
/**
|
||||
* @description Format with SI, i.e. m/M/etc. (default = true)
|
||||
*/
|
||||
withSi?: boolean;
|
||||
/**
|
||||
* @description Format with full SI, i.e. mili/Mega/etc.
|
||||
*/
|
||||
withSiFull?: boolean;
|
||||
/**
|
||||
* @description Add the unit (useful in Balance formats)
|
||||
*/
|
||||
withUnit?: boolean | string;
|
||||
/**
|
||||
* @description Returns all trailing zeros, otherwise removes (default = true)
|
||||
*/
|
||||
withZero?: boolean;
|
||||
/**
|
||||
* @description The locale to use
|
||||
*/
|
||||
locale?: string;
|
||||
}
|
||||
interface BalanceFormatter {
|
||||
<ExtToBn extends ToBn>(input?: number | string | BN | bigint | ExtToBn, options?: Options): string;
|
||||
calcSi(text: string, decimals?: number): SiDef;
|
||||
findSi(type: string): SiDef;
|
||||
getDefaults(): Defaults;
|
||||
getOptions(decimals?: number): SiDef[];
|
||||
setDefaults(defaults: SetDefaults): void;
|
||||
}
|
||||
export declare const formatBalance: BalanceFormatter;
|
||||
export {};
|
||||
@@ -0,0 +1,85 @@
|
||||
import { bnToBn } from '../bn/toBn.js';
|
||||
import { isBoolean } from '../is/boolean.js';
|
||||
import { formatDecimal } from './formatDecimal.js';
|
||||
import { getSeparator } from './getSeparator.js';
|
||||
import { calcSi, findSi, SI, SI_MID } from './si.js';
|
||||
const DEFAULT_DECIMALS = 0;
|
||||
const DEFAULT_UNIT = SI[SI_MID].text;
|
||||
let defaultDecimals = DEFAULT_DECIMALS;
|
||||
let defaultUnit = DEFAULT_UNIT;
|
||||
function _formatBalance(input, { decimals = defaultDecimals, forceUnit, locale = 'en', withAll = false, withSi = true, withSiFull = false, withUnit = true, withZero = true } = {}) {
|
||||
// we only work with string inputs here - convert anything
|
||||
// into the string-only value
|
||||
let text = bnToBn(input).toString();
|
||||
if (text.length === 0 || text === '0') {
|
||||
return '0';
|
||||
}
|
||||
// strip the negative sign so we can work with clean groupings, re-add this in the
|
||||
// end when we return the result (from here on we work with positive numbers)
|
||||
let sign = '';
|
||||
if (text[0].startsWith('-')) {
|
||||
sign = '-';
|
||||
text = text.substring(1);
|
||||
}
|
||||
// We start at midpoint (8) minus 1 - this means that values display as
|
||||
// 123.4567 instead of 0.1234 k (so we always have the most relevant).
|
||||
const si = calcSi(text, decimals, forceUnit);
|
||||
const mid = text.length - (decimals + si.power);
|
||||
const pre = mid <= 0 ? '0' : text.substring(0, mid);
|
||||
// get the post from the midpoint onward and then first add max decimals
|
||||
// before trimming to the correct (calculated) amount of decimals again
|
||||
let post = text
|
||||
.padStart(mid < 0 ? decimals : 1, '0')
|
||||
.substring(mid < 0 ? 0 : mid)
|
||||
.padEnd(withAll ? Math.max(decimals, 4) : 4, '0')
|
||||
.substring(0, withAll ? Math.max(4, decimals + si.power) : 4);
|
||||
// remove all trailing 0's (if required via flag)
|
||||
if (!withZero) {
|
||||
let end = post.length - 1;
|
||||
// This looks inefficient, however it is better to do the checks and
|
||||
// only make one final slice than it is to do it in multiples
|
||||
do {
|
||||
if (post[end] === '0') {
|
||||
end--;
|
||||
}
|
||||
} while (post[end] === '0');
|
||||
post = post.substring(0, end + 1);
|
||||
}
|
||||
// the display unit
|
||||
const unit = isBoolean(withUnit)
|
||||
? SI[SI_MID].text
|
||||
: withUnit;
|
||||
// format the units for display based on the flags
|
||||
const units = withSi || withSiFull
|
||||
? si.value === '-'
|
||||
? withUnit
|
||||
? ` ${unit}`
|
||||
: ''
|
||||
: ` ${withSiFull ? `${si.text}${withUnit ? ' ' : ''}` : si.value}${withUnit ? unit : ''}`
|
||||
: '';
|
||||
const { decimal, thousand } = getSeparator(locale);
|
||||
return `${sign}${formatDecimal(pre, thousand)}${post && `${decimal}${post}`}${units}`;
|
||||
}
|
||||
export const formatBalance = _formatBalance;
|
||||
formatBalance.calcSi = (text, decimals = defaultDecimals) => calcSi(text, decimals);
|
||||
formatBalance.findSi = findSi;
|
||||
formatBalance.getDefaults = () => {
|
||||
return {
|
||||
decimals: defaultDecimals,
|
||||
unit: defaultUnit
|
||||
};
|
||||
};
|
||||
formatBalance.getOptions = (decimals = defaultDecimals) => {
|
||||
return SI.filter(({ power }) => power < 0
|
||||
? (decimals + power) >= 0
|
||||
: true);
|
||||
};
|
||||
formatBalance.setDefaults = ({ decimals, unit }) => {
|
||||
defaultDecimals = (Array.isArray(decimals)
|
||||
? decimals[0]
|
||||
: decimals) ?? defaultDecimals;
|
||||
defaultUnit = (Array.isArray(unit)
|
||||
? unit[0]
|
||||
: unit) ?? defaultUnit;
|
||||
SI[SI_MID].text = defaultUnit;
|
||||
};
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* @name formatDate
|
||||
* @description Formats a date in CCYY-MM-DD HH:MM:SS format
|
||||
*/
|
||||
export declare function formatDate(date: Date): string;
|
||||
@@ -0,0 +1,17 @@
|
||||
/** @internal */
|
||||
function zeroPad(value) {
|
||||
return value.toString().padStart(2, '0');
|
||||
}
|
||||
/**
|
||||
* @name formatDate
|
||||
* @description Formats a date in CCYY-MM-DD HH:MM:SS format
|
||||
*/
|
||||
export function formatDate(date) {
|
||||
const year = date.getFullYear().toString();
|
||||
const month = zeroPad((date.getMonth() + 1));
|
||||
const day = zeroPad(date.getDate());
|
||||
const hour = zeroPad(date.getHours());
|
||||
const minute = zeroPad(date.getMinutes());
|
||||
const second = zeroPad(date.getSeconds());
|
||||
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* @name formatDecimal
|
||||
* @description Formats a number into string format with thousand separators
|
||||
*/
|
||||
export declare function formatDecimal(value: string, separator?: string): string;
|
||||
@@ -0,0 +1,16 @@
|
||||
const NUMBER_REGEX = new RegExp('(\\d+?)(?=(\\d{3})+(?!\\d)|$)', 'g');
|
||||
/**
|
||||
* @name formatDecimal
|
||||
* @description Formats a number into string format with thousand separators
|
||||
*/
|
||||
export function formatDecimal(value, separator = ',') {
|
||||
// We can do this by adjusting the regx, however for the sake of clarity
|
||||
// we rather strip and re-add the negative sign in the output
|
||||
const isNegative = value[0].startsWith('-');
|
||||
const matched = isNegative
|
||||
? value.substring(1).match(NUMBER_REGEX)
|
||||
: value.match(NUMBER_REGEX);
|
||||
return matched
|
||||
? `${isNegative ? '-' : ''}${matched.join(separator)}`
|
||||
: value;
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import type { BN } from '../bn/bn.js';
|
||||
import type { ToBn } from '../types.js';
|
||||
/**
|
||||
* @name formatElapsed
|
||||
* @description Formats an elapsed value into s, m, h or day segments
|
||||
*/
|
||||
export declare function formatElapsed<ExtToBn extends ToBn>(now?: Date | null, value?: bigint | BN | ExtToBn | Date | number | null): string;
|
||||
@@ -0,0 +1,27 @@
|
||||
import { bnToBn } from '../bn/toBn.js';
|
||||
/** @internal */
|
||||
function formatValue(elapsed) {
|
||||
if (elapsed < 15) {
|
||||
return `${elapsed.toFixed(1)}s`;
|
||||
}
|
||||
else if (elapsed < 60) {
|
||||
return `${elapsed | 0}s`;
|
||||
}
|
||||
else if (elapsed < 3600) {
|
||||
return `${elapsed / 60 | 0}m`;
|
||||
}
|
||||
return `${elapsed / 3600 | 0}h`;
|
||||
}
|
||||
/**
|
||||
* @name formatElapsed
|
||||
* @description Formats an elapsed value into s, m, h or day segments
|
||||
*/
|
||||
export function formatElapsed(now, value) {
|
||||
const tsNow = now?.getTime() || 0;
|
||||
const tsValue = value instanceof Date
|
||||
? value.getTime()
|
||||
: bnToBn(value).toNumber();
|
||||
return (tsNow && tsValue)
|
||||
? formatValue(Math.max(Math.abs(tsNow - tsValue), 0) / 1000)
|
||||
: '0.0s';
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
import type { BN } from '../bn/bn.js';
|
||||
import type { ToBn } from '../types.js';
|
||||
interface Options {
|
||||
/**
|
||||
* @description The locale to use
|
||||
*/
|
||||
locale?: string;
|
||||
}
|
||||
/**
|
||||
* @name formatNumber
|
||||
* @description Formats a number into string format with thousand separators
|
||||
*/
|
||||
export declare function formatNumber<ExtToBn extends ToBn>(value?: ExtToBn | BN | bigint | number | null, { locale }?: Options): string;
|
||||
export {};
|
||||
@@ -0,0 +1,11 @@
|
||||
import { bnToBn } from '../bn/toBn.js';
|
||||
import { formatDecimal } from './formatDecimal.js';
|
||||
import { getSeparator } from './getSeparator.js';
|
||||
/**
|
||||
* @name formatNumber
|
||||
* @description Formats a number into string format with thousand separators
|
||||
*/
|
||||
export function formatNumber(value, { locale = 'en' } = {}) {
|
||||
const { thousand } = getSeparator(locale);
|
||||
return formatDecimal(bnToBn(value).toString(), thousand);
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Get the decimal and thousand separator of a locale
|
||||
* @param locale
|
||||
* @returns {decimal: string, thousand: string}
|
||||
*/
|
||||
export declare function getSeparator(locale?: string): {
|
||||
thousand: string;
|
||||
decimal: string;
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Get the decimal and thousand separator of a locale
|
||||
* @param locale
|
||||
* @returns {decimal: string, thousand: string}
|
||||
*/
|
||||
export function getSeparator(locale) {
|
||||
return {
|
||||
decimal: (0.1).toLocaleString(locale, { useGrouping: false }).charAt(1),
|
||||
thousand: (1000).toLocaleString(locale, { useGrouping: true }).replace(/\d/g, '').charAt(0)
|
||||
};
|
||||
}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
export { formatBalance } from './formatBalance.js';
|
||||
export { formatDate } from './formatDate.js';
|
||||
export { formatDecimal } from './formatDecimal.js';
|
||||
export { formatElapsed } from './formatElapsed.js';
|
||||
export { formatNumber } from './formatNumber.js';
|
||||
export { calcSi, findSi } from './si.js';
|
||||
@@ -0,0 +1,6 @@
|
||||
export { formatBalance } from './formatBalance.js';
|
||||
export { formatDate } from './formatDate.js';
|
||||
export { formatDecimal } from './formatDecimal.js';
|
||||
export { formatElapsed } from './formatElapsed.js';
|
||||
export { formatNumber } from './formatNumber.js';
|
||||
export { calcSi, findSi } from './si.js';
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
import type { SiDef } from '../types.js';
|
||||
/** @internal */
|
||||
export declare const SI_MID = 8;
|
||||
/** @internal */
|
||||
export declare const SI: SiDef[];
|
||||
/** @internal */
|
||||
export declare function findSi(type: string): SiDef;
|
||||
/** @internal */
|
||||
export declare function calcSi(text: string, decimals: number, forceUnit?: string): SiDef;
|
||||
@@ -0,0 +1,40 @@
|
||||
/** @internal */
|
||||
export const SI_MID = 8;
|
||||
/** @internal */
|
||||
export const SI = [
|
||||
{ power: -24, text: 'yocto', value: 'y' },
|
||||
{ power: -21, text: 'zepto', value: 'z' },
|
||||
{ power: -18, text: 'atto', value: 'a' },
|
||||
{ power: -15, text: 'femto', value: 'f' },
|
||||
{ power: -12, text: 'pico', value: 'p' },
|
||||
{ power: -9, text: 'nano', value: 'n' },
|
||||
{ power: -6, text: 'micro', value: 'µ' },
|
||||
{ power: -3, text: 'milli', value: 'm' },
|
||||
{ power: 0, text: 'Unit', value: '-' }, // position 8
|
||||
{ power: 3, text: 'Kilo', value: 'k' },
|
||||
{ power: 6, text: 'Mill', value: 'M' }, // Mega, M
|
||||
{ power: 9, text: 'Bill', value: 'B' }, // Giga, G
|
||||
{ power: 12, text: 'Tril', value: 'T' }, // Tera, T
|
||||
{ power: 15, text: 'Peta', value: 'P' },
|
||||
{ power: 18, text: 'Exa', value: 'E' },
|
||||
{ power: 21, text: 'Zeta', value: 'Z' },
|
||||
{ power: 24, text: 'Yotta', value: 'Y' }
|
||||
];
|
||||
/** @internal */
|
||||
export function findSi(type) {
|
||||
// use a loop here, better RN support (which doesn't have [].find)
|
||||
for (let i = 0, count = SI.length; i < count; i++) {
|
||||
if (SI[i].value === type) {
|
||||
return SI[i];
|
||||
}
|
||||
}
|
||||
return SI[SI_MID];
|
||||
}
|
||||
/** @internal */
|
||||
export function calcSi(text, decimals, forceUnit) {
|
||||
if (forceUnit) {
|
||||
return findSi(forceUnit);
|
||||
}
|
||||
const siDefIndex = (SI_MID - 1) + Math.ceil((text.length - decimals) / 3);
|
||||
return SI[siDefIndex] || SI[siDefIndex < 0 ? 0 : SI.length - 1];
|
||||
}
|
||||
Reference in New Issue
Block a user