From fd5dc234aaa7c6ea536baa5596877637a3bad2bf Mon Sep 17 00:00:00 2001 From: David Herges Date: Thu, 19 Jan 2017 22:05:26 +0100 Subject: [PATCH] update type definitions for numeraljs (#14120) --- numeral/index.d.ts | 42 +++++++++++++++++++++++++++++++++--- numeral/numeral-tests.ts | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/numeral/index.d.ts b/numeral/index.d.ts index c19caef255..e48cfa6f3d 100644 --- a/numeral/index.d.ts +++ b/numeral/index.d.ts @@ -3,7 +3,9 @@ // Definitions by: Vincent Bortone // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -interface NumeralJSLanguage { + +// http://numeraljs.com/#locales +interface NumeralJSLocale { delimiters: { thousands: string; decimal: string; @@ -20,12 +22,43 @@ interface NumeralJSLanguage { }; } + +// http://numeraljs.com/#custom-formats +interface NumeralJsFormat { + regexps: { + format: RegExp, + unformat: RegExp, + }, + format: (value: any, format: string, roundingFunction: Function) => string, + unformat: (value: string) => number +} + +type RegisterType = 'format' | 'locale'; + +// http://numeraljs.com/#use-it interface Numeral { (value?: any): Numeral; version: string; isNumeral: boolean; - language(key?: string, values?: NumeralJSLanguage): Numeral | string; - zeroFormat(format: string): string; + + /** + * This function sets the current locale. If no arguments are passed in, + * it will simply return the current global locale key. + */ + locale(key?: string): string; + + /** + * Registers a language definition or a custom format definition. + * + * @param what Allowed values are: either 'format' or 'locale' + * @param key The key of the registerd type, e.g. 'de' for a german locale definition + * @param value The locale definition or the format definitiion + */ + register(what: RegisterType, key: string, value: NumeralJSLocale | NumeralJsFormat): NumeralJSLocale | NumeralJsFormat; + + zeroFormat(format: string): void; + nullFormat(format: string): void; + defaultFormat(format: string): void; clone(): Numeral; format(inputString?: string): string; formatCurrency(inputString?: string): string; @@ -42,6 +75,9 @@ interface Numeral { declare var numeral: Numeral; +/** + * Usage: import * as numeral from 'numeral' + */ declare module "numeral" { export = numeral; diff --git a/numeral/numeral-tests.ts b/numeral/numeral-tests.ts index 6202b16b0e..7be34cc8f9 100644 --- a/numeral/numeral-tests.ts +++ b/numeral/numeral-tests.ts @@ -41,3 +41,49 @@ var bVal: number = b.value(); // 1000 var cVal: number = c.add(10).value(); // 1010 + + +// Formats +numeral.register('format', 'percentage', { + regexps: { + format: /(%)/, + unformat: /(%)/ + }, + format: function(value, format, roundingFunction) { + return 'foo'; + }, + unformat: function(string) { + return 123; + } +}); + +var customFormatted = numeral().format('0%'); + + +// Locales +// load a locale +numeral.register('locale', 'fr', { + delimiters: { + thousands: ' ', + decimal: ',' + }, + abbreviations: { + thousand: 'k', + million: 'm', + billion: 'b', + trillion: 't' + }, + ordinal : function (number) { + return number === 1 ? 'er' : 'ème'; + }, + currency: { + symbol: '€' + } +}); + +// switch between locales +numeral.locale('fr'); + +// return the current locale +numeral.locale(); +// 'fr'