UNPKG

4.33 kBTypeScriptView Raw
1// Type definitions for Numeral.js 2.0
2// Project: https://github.com/adamwdraper/Numeral-js
3// Definitions by: Vincent Bortone <https://github.com/vbortone>
4// Kenneth Luján <https://github.com/klujanrosas>
5// Carlos Quiroga <https://github.com/KarlosQ>
6// Piotr Błażejewicz <https://github.com/peterblazejewicz>
7// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8export as namespace numeral;
9
10/**
11 * A javascript library for formatting and manipulating numbers.
12 */
13declare function numeral(input?: any): numeral.Numeral;
14
15type VERSION = '2.0.6';
16
17declare namespace numeral {
18 /** compare numeral object */
19 function isNumeral(value: any): boolean;
20
21 const version: VERSION;
22 const options: NumeralJSOptions;
23 /**
24 * Object with all loaded locales
25 */
26 const locales: NumeralJSLocales;
27
28 /**
29 * Object with all loaded formats
30 */
31 const formats: NumeralJSFormats;
32
33 /**
34 * Object with utility functions
35 */
36 const _: NumeralJSUtils;
37
38 /**
39 * This function sets the current locale. If no arguments are passed in,
40 * it will simply return the current global locale key.
41 */
42 function locale(key?: string): string;
43
44 /**
45 * This function provides access to the loaded locale data. If
46 * no arguments are passed in, it will simply return the current
47 * global locale object.
48 *
49 * @param key Locale key, e.g 'es' for a spanish locale definition
50 */
51 function localeData(key?: string): NumeralJSLocale;
52
53 /**
54 * This function resets the configuration to all the defaults
55 */
56 function reset(): void;
57
58 function zeroFormat(format: string): void;
59 function nullFormat(format: string): void;
60 function defaultFormat(format: string): void;
61 /**
62 * Registers a language definition or a custom format definition.
63 *
64 * @param what Allowed values are: either 'format' or 'locale'
65 * @param key The key of the registerd type, e.g. 'de' for a german locale definition
66 * @param value The locale definition or the format definitiion
67 */
68 function register(
69 what: RegisterType,
70 key: string,
71 value: NumeralJSLocale | NumeralJSFormat,
72 ): NumeralJSLocale | NumeralJSFormat;
73
74 function validate(value: any, culture: any): boolean;
75
76 const fn: Numeral['prototype'];
77
78 // http://numeraljs.com/#use-it
79 class Numeral {
80 constructor(input: any, value: number);
81 prototype: Numeral;
82 clone(): Numeral;
83 format(inputString?: string, roundingFunction?: RoundingFunction): string;
84 value(): number | null;
85 input(): any;
86 set(value: any): Numeral;
87 add(value: any): Numeral;
88 subtract(value: any): Numeral;
89 multiply(value: any): Numeral;
90 divide(value: any): Numeral;
91 difference(value: any): number;
92 }
93
94 // http://numeraljs.com/#locales
95 interface NumeralJSLocale {
96 delimiters: {
97 thousands: string;
98 decimal: string;
99 };
100 abbreviations: {
101 thousand: string;
102 million: string;
103 billion: string;
104 trillion: string;
105 };
106 ordinal(num: number): string;
107 currency: {
108 symbol: string;
109 };
110 }
111
112 interface NumeralJSLocales {
113 [id: string]: NumeralJSLocale;
114 }
115
116 interface NumeralJSOptions {
117 currentLocale: string;
118 zeroFormat: string;
119 nullFormat: string;
120 defaultFormat: string;
121 scalePercentBy100: boolean;
122 }
123
124 type RoundingFunction = (value: number) => number;
125
126 // http://numeraljs.com/#custom-formats
127 interface NumeralJSFormat {
128 regexps: {
129 format: RegExp;
130 unformat: RegExp;
131 };
132 format: (value: any, format: string, roundingFunction: RoundingFunction) => string;
133 unformat: (value: string) => number;
134 }
135
136 interface NumeralJSFormats {
137 [id: string]: NumeralJSFormat;
138 }
139
140 interface NumeralJSUtils {
141 numberToFormat: (value: number, format: string, roundingFunction?: RoundingFunction) => string;
142 stringToNumber: (string: string) => number;
143 }
144
145 type RegisterType = 'format' | 'locale';
146}
147
148export = numeral;