From c29584ebbb53f0354f82592ba337abd859147dd1 Mon Sep 17 00:00:00 2001 From: Rigoberto Molina Date: Sat, 13 Apr 2019 21:04:06 -0700 Subject: [PATCH 1/7] Types for npm package: humanize-duration --- .../humanize-duration-tests.ts | 93 +++++++++++++++++++ types/humanize-duration/index.d.ts | 75 +++++++++++++++ types/humanize-duration/tsconfig.json | 22 +++++ types/humanize-duration/tslint.json | 1 + 4 files changed, 191 insertions(+) create mode 100644 types/humanize-duration/humanize-duration-tests.ts create mode 100644 types/humanize-duration/index.d.ts create mode 100644 types/humanize-duration/tsconfig.json create mode 100644 types/humanize-duration/tslint.json diff --git a/types/humanize-duration/humanize-duration-tests.ts b/types/humanize-duration/humanize-duration-tests.ts new file mode 100644 index 0000000000..95cdf16f51 --- /dev/null +++ b/types/humanize-duration/humanize-duration-tests.ts @@ -0,0 +1,93 @@ +import humanizeDuration from "humanize-duration"; + +humanizeDuration(3000); // '3 seconds' +humanizeDuration(2250); // '2.25 seconds' +humanizeDuration(97320000); // '1 day, 3 hours, 2 minutes' + +humanizeDuration(3000, { language: "es" }); // '3 segundos' +humanizeDuration(5000, { language: "ko" }); // '5 초' + +humanizeDuration(3000, { language: "en", fallbacks: ["en"] }); // '3 seconds' +humanizeDuration(3000, { language: "bad language", fallbacks: ["bad language", "es"] }); // '3 segundos' + +humanizeDuration(22140000, { delimiter: " and " }); // '6 hours and 9 minutes' +humanizeDuration(22140000, { delimiter: "--" }); // '6 hours--9 minutes' + +humanizeDuration(260040000, { spacer: " whole " }); // '3 whole days, 14 whole minutes' +humanizeDuration(260040000, { spacer: "" }); // '3days, 14minutes' + +humanizeDuration(1000000000000); // '31 years, 8 months, 1 week, 19 hours, 46 minutes, 40 seconds' +humanizeDuration(1000000000000, { largest: 2 }); // '31 years, 8 month' + +humanizeDuration(3600000, { units: ["h"] }); // '1 hour' +humanizeDuration(3600000, { units: ["m"] }); // '60 minutes' +humanizeDuration(3600000, { units: ["d", "h"] }); // '1 hour' + +humanizeDuration(1200); // '1.2 seconds' +humanizeDuration(1200, { round: true }); // '1 second' +humanizeDuration(1600, { round: true }); // '2 seconds' + +humanizeDuration(1200); // '1.2 seconds' +humanizeDuration(1200, { decimal: " point " }); // '1 point 2 seconds' + +humanizeDuration(22140000, { conjunction: " and " }); // '6 hours and 9 minutes' +humanizeDuration(22141000, { conjunction: " and " }); // '6 hours, 9 minutes, and 1 second' +humanizeDuration(22140000, { conjunction: " and ", serialComma: false }); // '6 hours and 9 minutes' +humanizeDuration(22141000, { conjunction: " and ", serialComma: false }); // '6 hours, 9 minutes and 1 second' + +humanizeDuration(8123.456789); // 8.12 seconds +humanizeDuration(8123.456789, { maxDecimalPoints: 3 }); // 8.123 seconds +humanizeDuration(8123.456789, { maxDecimalPoints: 6 }); // 8.123456 seconds +humanizeDuration(8123.45, { maxDecimalPoints: 6 }); // 8.12345 seconds +humanizeDuration(8000, { maxDecimalPoints: 6 }); // 8 seconds + +humanizeDuration(400); // '0.4 seconds' +humanizeDuration(400, { // '1 year, 1 month, 5 days' + unitMeasures: { + d: 1, + mo: 30, + w: 7, + y: 365, + }, +}); + +humanizeDuration(3602000, { + language: "es", + round: true, + spacer: " glorioso ", + units: ["m"], +}); // '60 glorioso minutos' + +const spanishHumanizer = humanizeDuration.humanizer({ + language: "es", + units: ["y", "mo", "d"], +}); + +spanishHumanizer(71177400000); // '2 años, 3 meses, 2 días' +spanishHumanizer(71177400000, { units: ["d", "h"] }); // '823 días, 19.5 horas' + +const shortEnglishHumanizer = humanizeDuration.humanizer({ + language: "shortEn", + languages: { + shortEn: { + d: () => "d", + h: () => "h", + m: () => "m", + mo: () => "mo", + ms: () => "ms", + s: () => "s", + w: () => "w", + y: () => "y", + }, + }, +}); + +shortEnglishHumanizer(15600000); // '4 h, 20 m' + +const humanizer = humanizeDuration.humanizer(); + +humanizer.languages.shortEn = { + y: () => "y", +}; + +humanizeDuration.getSupportedLanguages(); diff --git a/types/humanize-duration/index.d.ts b/types/humanize-duration/index.d.ts new file mode 100644 index 0000000000..e4af90a364 --- /dev/null +++ b/types/humanize-duration/index.d.ts @@ -0,0 +1,75 @@ +// Type definitions for humanize-duration v3.18.0 +// Project: https://github.com/EvanHahn/HumanizeDuration.js +// Definitions by: Rigoberto Molina +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export as namespace humanizeDuration; + +export = HumanizeDuration; + +declare function HumanizeDuration(ms: number): string; +declare function HumanizeDuration(ms: number, options: HumanizeDuration.Options): string; + +declare namespace HumanizeDuration { + export type LanguageCode = ( + "ar" | "bg" | "ca" | "zh_CN" | "zh_TW" | "hr" | "cs" | "da" | + "nl" | "en" | "fa" | "fi" | "fr" | "de" | "el" | "hu" | "is" | + "id" | "it" | "ja" | "ko" | "lo" | "lt" | "ms" | "no" | "pl" | + "pt" | "ro" | "ru" | "sk" | "es" | "sv" | "tr" | "th" | "uk" | + "ur" | "vi" + ); + export type Unit = "y" | "mo" | "w" | "d" | "h" | "m" | "s" | "ms"; + export interface UnitMeasuresOptions { + y: number; + mo: number; + w: number; + d: number; + h: number; + m: number; + s: number; + ms: number; + } + + export interface UnitTranslationOptions { + y: () => string; + mo: () => string; + w: () => string; + d: () => string; + h: () => string; + m: () => string; + s: () => string; + ms: () => string; + } + + export interface Options { + language?: string; + fallbacks?: string[]; + delimiter?: string; + spacer?: string; + largest?: number; + units?: Unit[]; + round?: boolean; + decimal?: string; + conjunction?: string; + serialComma?: boolean; + maxDecimalPoints?: number; + unitMeasures?: Partial; + } + + export interface HumanizerOptions extends Options { + languages?: { + [key: string]: Partial; + } + } + + export interface Humanizer { + (ms: number, options?: HumanizeDuration.Options): string; + languages: { + [key: string]: Partial; + } + } + + export function humanizer(options?: HumanizerOptions): Humanizer; + + export function getSupportedLanguages(): LanguageCode[]; +} diff --git a/types/humanize-duration/tsconfig.json b/types/humanize-duration/tsconfig.json new file mode 100644 index 0000000000..d2f3963617 --- /dev/null +++ b/types/humanize-duration/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "humanize-duration-tests.ts" + ] +} diff --git a/types/humanize-duration/tslint.json b/types/humanize-duration/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/humanize-duration/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 9f4f1078d959c51b5d5e1fa7f6233dc3e115ddb0 Mon Sep 17 00:00:00 2001 From: Rigoberto Molina Date: Sat, 13 Apr 2019 21:16:43 -0700 Subject: [PATCH 2/7] Add "strictFunctionTypes": true --- types/humanize-duration/tsconfig.json | 1 + 1 file changed, 1 insertion(+) diff --git a/types/humanize-duration/tsconfig.json b/types/humanize-duration/tsconfig.json index d2f3963617..4ce169b1bb 100644 --- a/types/humanize-duration/tsconfig.json +++ b/types/humanize-duration/tsconfig.json @@ -6,6 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, + "strictFunctionTypes": true, "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ From a600c03d204816d1935134bb7e8f8d5e830648f1 Mon Sep 17 00:00:00 2001 From: Rigoberto Molina Date: Sat, 13 Apr 2019 21:28:02 -0700 Subject: [PATCH 3/7] Add "allowSyntheticDefaultImports": true --- types/humanize-duration/tsconfig.json | 1 + 1 file changed, 1 insertion(+) diff --git a/types/humanize-duration/tsconfig.json b/types/humanize-duration/tsconfig.json index 4ce169b1bb..ef6b99f73b 100644 --- a/types/humanize-duration/tsconfig.json +++ b/types/humanize-duration/tsconfig.json @@ -4,6 +4,7 @@ "lib": [ "es6" ], + "allowSyntheticDefaultImports": true, "noImplicitAny": true, "noImplicitThis": true, "strictFunctionTypes": true, From 1a3dd468a9990434ca9c9f1bfa26964ebe6c2904 Mon Sep 17 00:00:00 2001 From: Rigoberto Molina Date: Sat, 13 Apr 2019 21:31:52 -0700 Subject: [PATCH 4/7] Fix some linting errors --- types/humanize-duration/index.d.ts | 66 +++++++++++++++--------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/types/humanize-duration/index.d.ts b/types/humanize-duration/index.d.ts index e4af90a364..45533c097f 100644 --- a/types/humanize-duration/index.d.ts +++ b/types/humanize-duration/index.d.ts @@ -1,47 +1,47 @@ -// Type definitions for humanize-duration v3.18.0 +// Type definitions for humanize-duration 3.18 // Project: https://github.com/EvanHahn/HumanizeDuration.js // Definitions by: Rigoberto Molina // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 export as namespace humanizeDuration; export = HumanizeDuration; -declare function HumanizeDuration(ms: number): string; -declare function HumanizeDuration(ms: number, options: HumanizeDuration.Options): string; +declare function HumanizeDuration(ms: number, options?: HumanizeDuration.Options): string; declare namespace HumanizeDuration { - export type LanguageCode = ( + type LanguageCode = ( "ar" | "bg" | "ca" | "zh_CN" | "zh_TW" | "hr" | "cs" | "da" | "nl" | "en" | "fa" | "fi" | "fr" | "de" | "el" | "hu" | "is" | "id" | "it" | "ja" | "ko" | "lo" | "lt" | "ms" | "no" | "pl" | "pt" | "ro" | "ru" | "sk" | "es" | "sv" | "tr" | "th" | "uk" | "ur" | "vi" ); - export type Unit = "y" | "mo" | "w" | "d" | "h" | "m" | "s" | "ms"; - export interface UnitMeasuresOptions { - y: number; - mo: number; - w: number; - d: number; - h: number; - m: number; - s: number; - ms: number; + type Unit = "y" | "mo" | "w" | "d" | "h" | "m" | "s" | "ms"; + interface UnitMeasuresOptions { + y?: number; + mo?: number; + w?: number; + d?: number; + h?: number; + m?: number; + s?: number; + ms?: number; } - export interface UnitTranslationOptions { - y: () => string; - mo: () => string; - w: () => string; - d: () => string; - h: () => string; - m: () => string; - s: () => string; - ms: () => string; + interface UnitTranslationOptions { + y?: () => string; + mo?: () => string; + w?: () => string; + d?: () => string; + h?: () => string; + m?: () => string; + s?: () => string; + ms?: () => string; } - export interface Options { + interface Options { language?: string; fallbacks?: string[]; delimiter?: string; @@ -53,23 +53,23 @@ declare namespace HumanizeDuration { conjunction?: string; serialComma?: boolean; maxDecimalPoints?: number; - unitMeasures?: Partial; + unitMeasures?: UnitMeasuresOptions; } - export interface HumanizerOptions extends Options { + interface HumanizerOptions extends Options { languages?: { - [key: string]: Partial; - } + [key: string]: UnitTranslationOptions; + }; } - export interface Humanizer { + interface Humanizer { (ms: number, options?: HumanizeDuration.Options): string; languages: { - [key: string]: Partial; - } + [key: string]: UnitTranslationOptions; + }; } - export function humanizer(options?: HumanizerOptions): Humanizer; + function humanizer(options?: HumanizerOptions): Humanizer; - export function getSupportedLanguages(): LanguageCode[]; + function getSupportedLanguages(): LanguageCode[]; } From c561b5670205b7526d471354566a0db31a61d445 Mon Sep 17 00:00:00 2001 From: Rigoberto Molina Date: Sat, 13 Apr 2019 21:50:53 -0700 Subject: [PATCH 5/7] Fix more linting errors --- types/humanize-duration/index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/humanize-duration/index.d.ts b/types/humanize-duration/index.d.ts index 45533c097f..add42cd63b 100644 --- a/types/humanize-duration/index.d.ts +++ b/types/humanize-duration/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/EvanHahn/HumanizeDuration.js // Definitions by: Rigoberto Molina // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.1 +// TypeScript Version: 2.2 export as namespace humanizeDuration; @@ -63,9 +63,9 @@ declare namespace HumanizeDuration { } interface Humanizer { - (ms: number, options?: HumanizeDuration.Options): string; + (ms: number, options?: Options): string; languages: { - [key: string]: UnitTranslationOptions; + [x: string]: UnitTranslationOptions; }; } From fadc14aec3cb5c487d1d68897c1714ee1b1f2f10 Mon Sep 17 00:00:00 2001 From: Rigoberto Molina Date: Sat, 13 Apr 2019 22:03:31 -0700 Subject: [PATCH 6/7] Add documentation for options --- types/humanize-duration/index.d.ts | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/types/humanize-duration/index.d.ts b/types/humanize-duration/index.d.ts index add42cd63b..073c099fdd 100644 --- a/types/humanize-duration/index.d.ts +++ b/types/humanize-duration/index.d.ts @@ -42,17 +42,50 @@ declare namespace HumanizeDuration { } interface Options { + /** + * Language for unit display (accepts an ISO 639-1 code from one of the supported languages). + */ language?: string; + /** + * Fallback languages if the provided language cannot be found (accepts an ISO 639-1 code from one of the supported languages). It works from left to right. + */ fallbacks?: string[]; + /** + * String to display between the previous unit and the next value. + */ delimiter?: string; + /** + * String to display between each value and unit. + */ spacer?: string; + /** + * Number representing the maximum number of units to display for the duration. + */ largest?: number; + /** + * Array of strings to define which units are used to display the duration (if needed). + */ units?: Unit[]; + /** + * Boolean value. Use true to round the smallest unit displayed (can be combined with largest and units). + */ round?: boolean; + /** + * String to substitute for the decimal point in a decimal fraction. + */ decimal?: string; + /** + * String to include before the final unit. You can also set serialComma to false to eliminate the final comma. + */ conjunction?: string; serialComma?: boolean; + /** + * Number that defines a maximal decimal points for float values. + */ maxDecimalPoints?: number; + /** + * Customize the value used to calculate each unit of time. + */ unitMeasures?: UnitMeasuresOptions; } @@ -71,5 +104,8 @@ declare namespace HumanizeDuration { function humanizer(options?: HumanizerOptions): Humanizer; + /** + * This function won't return any new languages you define; it will only return the defaults supported by the library. + */ function getSupportedLanguages(): LanguageCode[]; } From e8b2d266011cc67bf70d5b2b920a2556ee2b5c3a Mon Sep 17 00:00:00 2001 From: Rigoberto Molina Date: Sat, 13 Apr 2019 22:11:37 -0700 Subject: [PATCH 7/7] Fix JSDoc multiline comment alignment --- types/humanize-duration/index.d.ts | 48 +++++++++++++++--------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/types/humanize-duration/index.d.ts b/types/humanize-duration/index.d.ts index 073c099fdd..b27ff8c974 100644 --- a/types/humanize-duration/index.d.ts +++ b/types/humanize-duration/index.d.ts @@ -43,49 +43,49 @@ declare namespace HumanizeDuration { interface Options { /** - * Language for unit display (accepts an ISO 639-1 code from one of the supported languages). - */ + * Language for unit display (accepts an ISO 639-1 code from one of the supported languages). + */ language?: string; /** - * Fallback languages if the provided language cannot be found (accepts an ISO 639-1 code from one of the supported languages). It works from left to right. - */ + * Fallback languages if the provided language cannot be found (accepts an ISO 639-1 code from one of the supported languages). It works from left to right. + */ fallbacks?: string[]; /** - * String to display between the previous unit and the next value. - */ + * String to display between the previous unit and the next value. + */ delimiter?: string; /** - * String to display between each value and unit. - */ + * String to display between each value and unit. + */ spacer?: string; /** - * Number representing the maximum number of units to display for the duration. - */ + * Number representing the maximum number of units to display for the duration. + */ largest?: number; /** - * Array of strings to define which units are used to display the duration (if needed). - */ + * Array of strings to define which units are used to display the duration (if needed). + */ units?: Unit[]; /** - * Boolean value. Use true to round the smallest unit displayed (can be combined with largest and units). - */ + * Boolean value. Use true to round the smallest unit displayed (can be combined with largest and units). + */ round?: boolean; /** - * String to substitute for the decimal point in a decimal fraction. - */ + * String to substitute for the decimal point in a decimal fraction. + */ decimal?: string; /** - * String to include before the final unit. You can also set serialComma to false to eliminate the final comma. - */ + * String to include before the final unit. You can also set serialComma to false to eliminate the final comma. + */ conjunction?: string; serialComma?: boolean; /** - * Number that defines a maximal decimal points for float values. - */ + * Number that defines a maximal decimal points for float values. + */ maxDecimalPoints?: number; /** - * Customize the value used to calculate each unit of time. - */ + * Customize the value used to calculate each unit of time. + */ unitMeasures?: UnitMeasuresOptions; } @@ -105,7 +105,7 @@ declare namespace HumanizeDuration { function humanizer(options?: HumanizerOptions): Humanizer; /** - * This function won't return any new languages you define; it will only return the defaults supported by the library. - */ + * This function won't return any new languages you define; it will only return the defaults supported by the library. + */ function getSupportedLanguages(): LanguageCode[]; }