Skip to content

Instantly share code, notes, and snippets.

@nergal
Created December 13, 2023 14:34
Show Gist options
  • Save nergal/7220778a916d371631952b23d578125f to your computer and use it in GitHub Desktop.
Save nergal/7220778a916d371631952b23d578125f to your computer and use it in GitHub Desktop.
@types/numfmt - numfmt package typescript definitions
declare module 'numfmt' {
type LocaleTag = string;
type LocaleData = {
group: string;
decimal: string;
positive: string;
negative: string;
percent: string;
exponent: string;
nan: string;
infinity: string;
ampm: [string, string];
mmmm: string[];
mmm: string[];
dddd: string[];
ddd: string[];
mmmm6: string[];
mmm6: string[];
};
type Options = Partial<{
locale: string;
throws: boolean;
invalid: string;
nbsp: boolean;
leap1900: boolean;
dateErrorThrows: boolean;
dateErrorNumber: boolean;
overflow: string;
dateSpanLarge: boolean;
ignoreTimezone: boolean;
nativeDate: boolean;
}>;
type DateDescriptor = [number, number, number, number, number, number];
type ParsedFormat<T = unknown> = {
v: T;
z: string;
};
type FormatInfo = {
type:
| 'currency'
| 'date'
| 'datetime'
| 'error'
| 'fraction'
| 'general'
| 'grouped'
| 'number'
| 'percent'
| 'scientific'
| 'text'
| 'time';
isDate: boolean;
isText: boolean;
isPercent: boolean;
maxDecimals: number;
color: number;
parentheses: number;
grouped: number;
code: string;
level: number;
scale: number;
};
type FormatDateInfo = {
year: boolean;
month: boolean;
day: boolean;
hours: boolean;
minutes: boolean;
seconds: boolean;
clockType: 24 | 12;
};
type FormatterCallable = (
value: number | string,
options?: Options,
) => string;
type Formatter = FormatterCallable & {
info: FormatInfo;
dateInfo: FormatDateInfo;
isDate: (format?: string) => boolean;
isPercent: (format?: string) => boolean;
isText: (format?: string) => boolean;
color: (value: number) => string;
format: (
pattern: string,
value: number | string,
options?: Options,
) => string;
round: (value: number, places?: number) => string;
getInfo: (format: string) => FormatInfo;
parseValue: (value: string, options?: Options) => ParsedFormat<unknown>;
parseNumber: (value: string, options?: Options) => ParsedFormat<number>;
parseDate: (value: string, options?: Options) => ParsedFormat<Date>;
parseTime: (value: string, options?: Options) => ParsedFormat<Date>;
parseBool: (
value: string,
options?: Options,
) => Omit<ParsedFormat<boolean>, 'z'>;
dateToSerial: (value: Date | DateDescriptor, options?: Options) => number;
dateFromSerial: (value: number, options?: Options) => DateDescriptor | Date;
addLocale: (data: LocaleData, tag: LocaleTag) => void;
getLocale: (tag: LocaleTag) => Options;
options: (options: Options) => void;
};
export default function numfmt(pattern: string, options?: Options): Formatter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment