From cfb29cc2d2f9525e6b5706eb5b3fa73510e204eb Mon Sep 17 00:00:00 2001 From: Joel Spadin Date: Sat, 2 Mar 2019 20:16:38 -0600 Subject: [PATCH] Add types for plurals-cldr --- types/plurals-cldr/index.d.ts | 53 ++++++++++++++++++++++++ types/plurals-cldr/plurals-cldr-tests.ts | 15 +++++++ types/plurals-cldr/tsconfig.json | 23 ++++++++++ types/plurals-cldr/tslint.json | 1 + 4 files changed, 92 insertions(+) create mode 100644 types/plurals-cldr/index.d.ts create mode 100644 types/plurals-cldr/plurals-cldr-tests.ts create mode 100644 types/plurals-cldr/tsconfig.json create mode 100644 types/plurals-cldr/tslint.json diff --git a/types/plurals-cldr/index.d.ts b/types/plurals-cldr/index.d.ts new file mode 100644 index 0000000000..0d7277b52a --- /dev/null +++ b/types/plurals-cldr/index.d.ts @@ -0,0 +1,53 @@ +// Type definitions for plurals-cldr 1.0 +// Project: https://github.com/nodeca/plurals-cldr +// Definitions by: Joel Spadin +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export type Form = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other'; + +interface Plural { + /** + * Returns the form name for a given number. If the locale is not + * supported, returns `null`. + * + * @param locale The locale code. + * @param number The number to check. May be passed as a string to keep + * trailing zeroes. + */ + (locale: string, number: number | string): Form | null; + + /** + * Returns an array of available forms for the given locale. If the + * locale is not supported, returns `null`. + * + * @param locale The locale code. + */ + forms(locale: string): Form[] | null; + + /** + * Returns the index of the form for a given number. If the locale is + * not supported, returns `-1`. + * + * This is convenient for implementing a lookup from a compact, ordered + * list. The order of forms for all locales is `zero`, `one`, `two`, + * `few`, `many`, `other`. Remove the forms not used by a locale to get + * the indices of each. + * + * @param locale The locale code. + * @param number The number to check. May be passed as a string to keep + * trailing zeroes. + */ + indexOf(locale: string, number: number | string): number; +} + +/** + * Gets the CLDR cardinal plural forms for numbers in different locales. + */ +declare const plural: Plural & { + /** + * Gets the CLDR ordinal plural forms for numbers in different locales. + */ + ordinal: Plural; +}; + +export default plural; diff --git a/types/plurals-cldr/plurals-cldr-tests.ts b/types/plurals-cldr/plurals-cldr-tests.ts new file mode 100644 index 0000000000..f5aacbd30c --- /dev/null +++ b/types/plurals-cldr/plurals-cldr-tests.ts @@ -0,0 +1,15 @@ +import plural from 'plurals-cldr'; + +plural('en', 0); // $ExpectType "zero" | "one" | "two" | "few" | "many" | "other" | null +plural('en', ''); // $ExpectType "zero" | "one" | "two" | "few" | "many" | "other" | null + +plural.forms('en'); // $ExpectType Form[] | null +plural.indexOf('en', 0); // $ExpectType number +plural.indexOf('en', ''); // $ExpectType number + +plural.ordinal('en', 0); // $ExpectType "zero" | "one" | "two" | "few" | "many" | "other" | null +plural.ordinal('en', ''); // $ExpectType "zero" | "one" | "two" | "few" | "many" | "other" | null + +plural.ordinal.forms('en'); // $ExpectType Form[] | null +plural.ordinal.indexOf('en', 0); // $ExpectType number +plural.ordinal.indexOf('en', ''); // $ExpectType number diff --git a/types/plurals-cldr/tsconfig.json b/types/plurals-cldr/tsconfig.json new file mode 100644 index 0000000000..593fbd1545 --- /dev/null +++ b/types/plurals-cldr/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "plurals-cldr-tests.ts" + ] +} diff --git a/types/plurals-cldr/tslint.json b/types/plurals-cldr/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/plurals-cldr/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }