From 60f833ff5cab0e48218958ec14e4476410d640af Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Thu, 1 Jun 2017 15:08:49 -0400 Subject: [PATCH] Update type definitions for moment-duration-format. (#16777) --- types/moment-duration-format/index.d.ts | 48 +++++++++++-- .../moment-duration-format-tests.ts | 72 +++++++++++++++++++ types/moment-duration-format/tsconfig.json | 7 +- 3 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 types/moment-duration-format/moment-duration-format-tests.ts diff --git a/types/moment-duration-format/index.d.ts b/types/moment-duration-format/index.d.ts index 76eb839cbd..402db8a75b 100644 --- a/types/moment-duration-format/index.d.ts +++ b/types/moment-duration-format/index.d.ts @@ -1,21 +1,55 @@ // Type definitions for moment-duration-format 1.3 // Project: https://github.com/jsmreese/moment-duration-format -// Definitions by: Swint De Coninck , Niklas Walter +// Definitions by: Swint De Coninck , Niklas Walter , Leonard Thieu // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped import * as moment from "moment"; declare module "moment" { + namespace duration { + const fn: Duration; + } + interface Duration { - format(template: string, precision?: number, settings?: DurationFormatSettings): string; - format(template: string, settings?: DurationFormatSettings): string; - format(settings: DurationFormatSettings): string; + format: Format; + } + + interface Format { + defaults: DurationFormatSettings; + + (template: string | TemplateFunction, precision: number, settings?: DurationFormatSettings): string; + (template: string | TemplateFunction, settings?: DurationFormatSettings): string; + (settings?: DurationFormatSettings): string; } interface DurationFormatSettings { - template?: string; + escape?: RegExp; + years?: RegExp; + months?: RegExp; + weeks?: RegExp; + days?: RegExp; + hours?: RegExp; + minutes?: RegExp; + seconds?: RegExp; + milliseconds?: RegExp; + general?: RegExp; + + types?: string; + + // "left" - template tokens are trimmed from the left until the first moment token that has a value >= 1 + // "right" - template tokens are trimmed from the right until the first moment token that has a value >= 1 + // (the final moment token is not trimmed, regardless of value) + // `false` - template tokens are not trimmed + trim?: 'left' | 'right' | false; + + // number of decimal digits to include after (to the right of) the decimal point (positive integer) + // or the number of digits to truncate to 0 before (to the left of) the decimal point (negative integer) precision?: number; - trim?: boolean | "right"; - forceLength?: boolean; + + // force first moment token with a value to render at full length even when template is trimmed and first moment token has length of 1 + forceLength?: boolean | null; + template?: string | TemplateFunction; } + + type TemplateFunction = ((this: DurationFormatSettings) => string); } diff --git a/types/moment-duration-format/moment-duration-format-tests.ts b/types/moment-duration-format/moment-duration-format-tests.ts new file mode 100644 index 0000000000..52f313b9b4 --- /dev/null +++ b/types/moment-duration-format/moment-duration-format-tests.ts @@ -0,0 +1,72 @@ +import * as moment from "moment"; +import 'moment-duration-format'; + +moment.duration(123, "minutes").format(); +// "2:03:00" + +moment.duration(123, "months").format(); +// "10y 3m" + +moment.duration(123, "minutes").format("h:mm"); +// "2:03" + +moment.duration(123, "minutes").format("h [hrs], m [min]"); +// "2 hrs, 3 min" + +moment.duration(123, "minutes").format("h [hrs]"); +// "2 hrs" + +moment.duration(123, "minutes").format("h [hrs]", 2); +// "2.04 hrs" + +moment.duration(123, "minutes").format("m [min]", -1); +// "120 min" + +moment.duration(123, "minutes").format({ template: "h [hrs]", precision: 2 }); +// "2.04 hrs" + +moment.duration(123, "minutes").format("d[d] h:mm:ss"); +// "2:03:00" + +moment.duration(123, "minutes").format("d[d] h:mm:ss", { trim: false }); +// "0d 2:03:00" + +moment.duration(123, "minutes").format("[seconds:] s -- [minutes:] m -- [hours:] h -- [days:] d", { trim: "right" }); +// "seconds: 0 -- minutes: 3 -- hours: 2" + +moment.duration(123, "seconds").format("h:mm:ss"); +// "2:03" + +moment.duration(123, "seconds").format("hh:mm:ss"); +// "02:03" + +moment.duration(123, "seconds").format("h:mm:ss", { forceLength: true }); +// "02:03" + +moment.duration.fn.format.defaults.escape = /\((.+?)\)/; +moment.duration(123, "seconds").format("m (minutes)", 2); +// "2.04 minutes" + +moment.duration.fn.format.defaults.years = /[Aa]+/; +moment.duration(123, "weeks").format("a [años]", 2); +// "2.35 años" + +moment.duration.fn.format.defaults.months = /m+/; +moment.duration(123, "weeks").format("m M", 2); +// "28.36 M" + +moment.duration.fn.format.defaults.weeks = /w+/; +moment.duration(123, "days").format("w W", 2); +// "17.57 W" + +moment.duration.fn.format.defaults.days = /d+/; +moment.duration(123, "hours").format("d D", 2); +// "5.12 D" + +moment.duration.fn.format.defaults.hours = /h+/; +moment.duration(123, "minutes").format("h H", 2); +// "2.04 H" + +moment.duration.fn.format.defaults.minutes = /n+/; +moment.duration(123, "seconds").format("n:ss"); +// "2:03" diff --git a/types/moment-duration-format/tsconfig.json b/types/moment-duration-format/tsconfig.json index 27e01ecd7b..07d419c2ea 100644 --- a/types/moment-duration-format/tsconfig.json +++ b/types/moment-duration-format/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" @@ -16,6 +16,7 @@ "forceConsistentCasingInFileNames": true }, "files": [ - "index.d.ts" + "index.d.ts", + "moment-duration-format-tests.ts" ] -} \ No newline at end of file +}