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..b27ff8c974 --- /dev/null +++ b/types/humanize-duration/index.d.ts @@ -0,0 +1,111 @@ +// 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.2 + +export as namespace humanizeDuration; + +export = HumanizeDuration; + +declare function HumanizeDuration(ms: number, options?: HumanizeDuration.Options): string; + +declare namespace HumanizeDuration { + 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" + ); + 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; + } + + interface UnitTranslationOptions { + y?: () => string; + mo?: () => string; + w?: () => string; + d?: () => string; + h?: () => string; + m?: () => string; + s?: () => string; + ms?: () => string; + } + + 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; + } + + interface HumanizerOptions extends Options { + languages?: { + [key: string]: UnitTranslationOptions; + }; + } + + interface Humanizer { + (ms: number, options?: Options): string; + languages: { + [x: string]: UnitTranslationOptions; + }; + } + + 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[]; +} diff --git a/types/humanize-duration/tsconfig.json b/types/humanize-duration/tsconfig.json new file mode 100644 index 0000000000..ef6b99f73b --- /dev/null +++ b/types/humanize-duration/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "allowSyntheticDefaultImports": true, + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": 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" }