num-beauty
Version:
An ultra lightweight module for formatting numbers into human-friendly strings
96 lines (95 loc) • 3.5 kB
TypeScript
import { type SupportedLocale } from './locales/index.js';
import { type RoundingMode, round } from './services/rounding.service.js';
import { formatNumber } from './services/formatting.service.js';
import { formatCurrency } from './services/currency.service.js';
import { formatBytes } from './services/bytes.service.js';
import { formatPercentage } from './services/percentage.service.js';
import { applyMask, getMask } from './services/mask.service.js';
import { abbreviateNumber } from './services/abbreviation.service.js';
import { toAccessibleString } from './services/accessibility.service.js';
import { unbeautify } from './services/parsing.service.js';
import { beautifyToParts, type NumberPart } from './services/parts.service.js';
declare const pluginServices: {
readonly round: typeof round;
readonly formatNumber: typeof formatNumber;
readonly formatCurrency: typeof formatCurrency;
readonly formatBytes: typeof formatBytes;
readonly formatPercentage: typeof formatPercentage;
readonly applyMask: typeof applyMask;
readonly getMask: typeof getMask;
readonly abbreviateNumber: typeof abbreviateNumber;
readonly toAccessibleString: typeof toAccessibleString;
readonly unbeautify: typeof unbeautify;
readonly beautifyToParts: typeof beautifyToParts;
};
export declare class Num {
private static appliedPlugins;
private value;
private _locale;
private _decimals;
private _abbreviated;
private _stripZeros;
private _roundingMode;
private _mask?;
private _currency?;
private _showSymbol;
private _showCode;
private _bytes;
private _bytesBinary;
private _bytesLongFormat;
private _percentage;
private _percentageMultiply;
private _percentageAddSpace?;
static extend<TOptions = void>(plugin: NumPlugin<TOptions>, options?: TOptions): typeof Num;
private static createStateSnapshot;
private static applyStatePatch;
constructor(value: number);
static parse(input: string, locale?: SupportedLocale): number;
locale(locale: SupportedLocale): this;
decimals(decimals: number): this;
abbreviated(): this;
stripZeros(): this;
rounding(mode: RoundingMode): this;
mask(mask: string): this;
currency(code: string): this;
hideSymbol(): this;
showCode(): this;
bytes(binary?: boolean): this;
bytesLongFormat(): this;
percentage(multiply?: boolean): this;
percentageSpace(addSpace?: boolean): this;
format(): string;
toString(): string;
valueOf(): number;
toAccessible(): string;
toParts(): NumberPart[];
}
export declare function num(value: number): Num;
export interface NumState {
value: number;
locale: SupportedLocale;
decimals: number;
abbreviated: boolean;
stripZeros: boolean;
roundingMode: RoundingMode;
mask?: string;
currency?: string;
showSymbol: boolean;
showCode: boolean;
bytes: boolean;
bytesBinary: boolean;
bytesLongFormat: boolean;
percentage: boolean;
percentageMultiply: boolean;
percentageAddSpace?: boolean;
}
export type NumPluginServices = typeof pluginServices;
export interface NumPluginContext {
Num: typeof Num;
createInstance: (value: number) => Num;
services: NumPluginServices;
getState(instance: Num): Readonly<NumState>;
patchState(instance: Num, patch: Partial<NumState>): void;
}
export type NumPlugin<TOptions = void> = (context: NumPluginContext, options?: TOptions) => void;
export {};