From b5deac6480a6ef470962109856e0961aeea30d2b Mon Sep 17 00:00:00 2001 From: Michael Mok Date: Fri, 6 Sep 2019 02:25:56 +0800 Subject: [PATCH] [node-polyglot] Add types for new options and static methods from latest release (#37991) * [node-polyglot] Update types to match v2.3.1 * [node-polyglot] Update tests to match types * [node-polyglot] Run prettier on test file * [node-polyglot] Add myself to the contributors list Co-Authored-By: Liam Ross --- types/node-polyglot/index.d.ts | 16 ++- types/node-polyglot/node-polyglot-tests.ts | 153 +++++++++++++-------- 2 files changed, 108 insertions(+), 61 deletions(-) diff --git a/types/node-polyglot/index.d.ts b/types/node-polyglot/index.d.ts index 3e0e02acb8..13d29d1193 100644 --- a/types/node-polyglot/index.d.ts +++ b/types/node-polyglot/index.d.ts @@ -1,7 +1,8 @@ -// Type definitions for node-polyglot v0.4.3 +// Type definitions for node-polyglot v0.4.4 // Project: https://github.com/airbnb/polyglot.js // Definitions by: Tim Jackson-Kiely // Liam Ross +// Michael Mok // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare namespace Polyglot { @@ -12,18 +13,27 @@ declare namespace Polyglot { [interpolationKey: string]: any; } + interface InterpolationTokenOptions { + prefix?: string; + suffix?: string; + } + interface PolyglotOptions { phrases?: any; locale?: string; allowMissing?: boolean; onMissingKey?: (key: string, options?: Polyglot.InterpolationOptions, locale?: string) => string; + warn?: (message: string) => void; + interpolation?: InterpolationTokenOptions; } + + function transformPhrase(phrase: string, options?: number | Polyglot.InterpolationOptions, locale?: string): string; } declare class Polyglot { constructor(options?: Polyglot.PolyglotOptions); - extend(phrases: any): void; + extend(phrases: any, prefix?: string): void; t(phrase: string, options?: number | Polyglot.InterpolationOptions): string; @@ -34,6 +44,8 @@ declare class Polyglot { locale(locale?: string): string; has(phrase: string): boolean; + + unset(phrases: any, prefix?: string): void; } export = Polyglot; diff --git a/types/node-polyglot/node-polyglot-tests.ts b/types/node-polyglot/node-polyglot-tests.ts index 1e641cc498..8a683966ca 100644 --- a/types/node-polyglot/node-polyglot-tests.ts +++ b/types/node-polyglot/node-polyglot-tests.ts @@ -1,70 +1,105 @@ - -import Polyglot = require("node-polyglot"); +import Polyglot = require('node-polyglot'); function instantiatePolyglot(): void { - var polyglot = new Polyglot(); - var phrasedPolyglot = new Polyglot({phrases: {"hello": "Hello"}}); - var localePolyglot = new Polyglot({locale: "fr"}); - var allowMissingPolyglot = new Polyglot({ - phrases: {"hello": "Hello"}, - allowMissing: true - }); - var onMissingKeySimplePolyglot = new Polyglot({ - phrases: {"hello": "Hello"}, - onMissingKey: (key: string): string => { - return 'ouups!'; - } - }); - var onMissingKeyComplexPolyglot = new Polyglot({ - phrases: {"hello": "Hello"}, - onMissingKey: (key: string, options: Polyglot.InterpolationOptions, locale: string): string => { - return 'ouups!'; - } - }); + var polyglot = new Polyglot(); + var phrasedPolyglot = new Polyglot({ phrases: { hello: 'Hello' } }); + var localePolyglot = new Polyglot({ locale: 'fr' }); + var allowMissingPolyglot = new Polyglot({ + phrases: { hello: 'Hello' }, + allowMissing: true, + }); + var onMissingKeySimplePolyglot = new Polyglot({ + phrases: { hello: 'Hello' }, + onMissingKey: (key: string): string => { + return 'ouups!'; + }, + }); + var onMissingKeyComplexPolyglot = new Polyglot({ + phrases: { hello: 'Hello' }, + onMissingKey: (key: string, options: Polyglot.InterpolationOptions, locale: string): string => { + return 'ouups!'; + }, + }); + var warnPolyglot = new Polyglot({ + warn: (message: string): void => { + return; + }, + }); + var interpolationPrefixPolyglot = new Polyglot({ + interpolation: { prefix: '$[' }, + }); + var interpolationSuffixPolyglot = new Polyglot({ interpolation: { suffix: ']' } }); } function translate(): void { - var polyglot = new Polyglot(); + var polyglot = new Polyglot(); - polyglot.extend({ - "hello": "Hello", - "hello_name": "Hola, %{name}.", - "nav": { - "sidebar": { - "welcome": "Welcome" - } - }, - "num_cars": "%{smart_count} car |||| %{smart_count} cars" - }); + polyglot.extend({ + hello: 'Hello', + hello_name: 'Hola, %{name}.', + nav: { + sidebar: { + welcome: 'Welcome', + }, + }, + num_cars: '%{smart_count} car |||| %{smart_count} cars', + }); + polyglot.extend( + { + hello: 'Hello', + hello_name: 'Hola, %{name}.', + }, + 'nested', + ); - polyglot.t("hello"); - polyglot.t("hello_name"); - polyglot.t("nav.sidebar.welcome"); - polyglot.t("num_cars", {smart_count: 0}); - polyglot.t("num_cars", 0); - polyglot.t("hello_name", {name: "Spike"}); - polyglot.t("i_like_to_write_in_language", { - _: "I like to write in %{language}.", - language: "Javascript" - }); - - polyglot.has("hello"); - polyglot.has("world"); + polyglot.t('hello'); + polyglot.t('hello_name'); + polyglot.t('nav.sidebar.welcome'); + polyglot.t('num_cars', { smart_count: 0 }); + polyglot.t('num_cars', 0); + polyglot.t('hello_name', { name: 'Spike' }); + polyglot.t('i_like_to_write_in_language', { + _: 'I like to write in %{language}.', + language: 'Javascript', + }); - polyglot.replace({ - "hello": "hey", - "nav": { - "sidebar": { - "welcome": "Greetings" - } - } - }); + polyglot.has('hello'); + polyglot.has('world'); - polyglot.clear(); + polyglot.replace({ + hello: 'hey', + nav: { + sidebar: { + welcome: 'Greetings', + }, + }, + }); - if (polyglot.locale("fr")) { - }; - - if (polyglot.locale()) { - }; + polyglot.unset('hello'); + polyglot.unset({ + hello_name: 'Hola, %{name}.', + }); + polyglot.unset('hello', 'nested'); + polyglot.unset( + { + hello_name: 'Hola, %{name}.', + }, + 'nested', + ); + + polyglot.clear(); + + if (polyglot.locale('fr')) { + } + + if (polyglot.locale()) { + } +} + +function transform(): void { + Polyglot.transformPhrase('Hello'); + Polyglot.transformPhrase('Hola, %{name}.', { name: 'Spike' }); + Polyglot.transformPhrase('%{smart_count} car |||| %{smart_count} cars', 0); + Polyglot.transformPhrase('%{smart_count} car |||| %{smart_count} cars', { smart_count: 0 }); + Polyglot.transformPhrase('Bonjour', undefined, 'fr'); }