From e5dcaed7426d4dcf2070a52c1920b71ccd33a515 Mon Sep 17 00:00:00 2001 From: Derek Sifford Date: Wed, 10 Jul 2019 15:32:27 -0400 Subject: [PATCH] [@wordpress/date] add new definitions (#36698) --- types/wordpress__date/index.d.ts | 99 +++++++++++++++++ types/wordpress__date/package.json | 6 + types/wordpress__date/tsconfig.json | 19 ++++ types/wordpress__date/tslint.json | 1 + .../wordpress__date/wordpress__date-tests.ts | 104 ++++++++++++++++++ 5 files changed, 229 insertions(+) create mode 100644 types/wordpress__date/index.d.ts create mode 100644 types/wordpress__date/package.json create mode 100644 types/wordpress__date/tsconfig.json create mode 100644 types/wordpress__date/tslint.json create mode 100644 types/wordpress__date/wordpress__date-tests.ts diff --git a/types/wordpress__date/index.d.ts b/types/wordpress__date/index.d.ts new file mode 100644 index 0000000000..184bbdf69f --- /dev/null +++ b/types/wordpress__date/index.d.ts @@ -0,0 +1,99 @@ +// Type definitions for @wordpress/date 3.3 +// Project: https://github.com/WordPress/gutenberg/tree/master/packages/date/README.md +// Definitions by: Derek Sifford +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.5 + +import { Moment, MomentInput } from 'moment'; + +export interface DateSettings { + formats: { + date: string; + datetime: string; + datetimeAbbreviated: string; + time: string; + }; + l10n: { + locale: string; + meridiem: { + AM: string; + PM: string; + am: string; + pm: string; + }; + months: string[]; + monthsShort: string[]; + relative: { + future: string; + past: string; + } + weekdays: string[]; + weekdaysShort: string[]; + }; + timezone: { + offset: string; + string: string; + }; +} + +/** + * Formats a date (like `date()` in PHP), in the site's timezone. + * + * @param dateFormat - PHP-style formatting string. See {@link https://php.net/date }. + * @param [dateValue] - Date object or string, parsable by moment.js. + * + * @returns - Formatted date. + */ +export function date(dateFormat: string, dateValue?: MomentInput): string; + +/** + * Formats a date (like `date_i18n()` in PHP). + * + * @param dateFormat - PHP-style formatting string. See {@link https://php.net/date }. + * @param [dateValue] - Date object or string, parsable by moment.js. + * @param [gmt=false] - `true` for GMT/UTC, `false` for site's timezone. + * + * @returns - Formatted date. + */ +export function dateI18n(dateFormat: string, dateValue?: MomentInput, gmt?: boolean): string; + +/** + * Formats a date. Does not alter the date's timezone. + * + * @param dateFormat - PHP-style formatting string. See {@link https://php.net/date }. + * @param [dateValue] - Date object or string, parsable by moment.js. + * + * @return - Formatted date. + */ +export function format(dateFormat: string, dateValue?: MomentInput): string; + +/** + * Create and return a JavaScript Date Object from a date string in the WP timezone. + * + * @param [dateValue] - Date formatted in the WP timezone. + */ +export function getDate(dateValue?: MomentInput): Date; + +/** + * Formats a date (like `date()` in PHP), in the UTC timezone. + * + * @param dateFormat - PHP-style formatting string. See {@link https://php.net/date }. + * @param [dateValue] - Date object or string, parsable by moment.js. + * + * @return - Formatted date. + */ +export function gmdate(dateFormat: string, dateValue?: MomentInput): string; + +/** + * Check whether a date is considered in the future according to the WordPress settings. + * + * @param dateValue - Value parsable by moment.js in the Defined WP Timezone. + */ +export function isInTheFuture(dateValue: MomentInput): boolean; + +/** + * Adds a locale to moment, using the format supplied by `wp_localize_script()`. + * + * @param dateSettings - Settings, including locale data. + */ +export function setSettings(dateSettings: DateSettings): void; diff --git a/types/wordpress__date/package.json b/types/wordpress__date/package.json new file mode 100644 index 0000000000..0c932c1d0b --- /dev/null +++ b/types/wordpress__date/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "moment": "^2.24.0" + } +} diff --git a/types/wordpress__date/tsconfig.json b/types/wordpress__date/tsconfig.json new file mode 100644 index 0000000000..52296d05f3 --- /dev/null +++ b/types/wordpress__date/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "paths": { + "@wordpress/date": ["wordpress__date"] + } + }, + "files": ["index.d.ts", "wordpress__date-tests.ts"] +} diff --git a/types/wordpress__date/tslint.json b/types/wordpress__date/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/wordpress__date/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/wordpress__date/wordpress__date-tests.ts b/types/wordpress__date/wordpress__date-tests.ts new file mode 100644 index 0000000000..261852497c --- /dev/null +++ b/types/wordpress__date/wordpress__date-tests.ts @@ -0,0 +1,104 @@ +import * as moment from 'moment'; +import * as date from '@wordpress/date'; + +// +// date +// + +// $ExpectType string +date.date('l'); + +// $ExpectType string +date.date('l', new Date()); + +// $ExpectType string +date.date('l', '2019/01/01'); + +// $ExpectType string +date.date('l', 2019); + +// $ExpectType string +date.date('l', [2019, '01', 1]); + +// $ExpectType string +date.date('l', moment()); + +// +// dateI18n +// + +// $ExpectType string +date.dateI18n('l'); + +// $ExpectType string +date.dateI18n('l', new Date()); + +// $ExpectType string +date.dateI18n('l', moment(), true); + +// +// format +// + +// $ExpectType string +date.format('l'); + +// $ExpectType string +date.format('l', '2019'); + +// $ExpectType string +date.format('l', moment()); + +// +// getDate +// + +// $ExpectType Date +date.getDate(); + +// $ExpectType Date +date.getDate('2019'); + +// $ExpectType Date +date.getDate(new Date()); + +// $ExpectType Date +date.getDate(moment()); + +// +// gmdate +// + +// $ExpectType string +date.gmdate('l'); + +// $ExpectType string +date.gmdate('l', '2019'); + +// $ExpectType string +date.gmdate('l', Date.now()); + +// $ExpectType string +date.gmdate('l', [2019]); + +// +// isInTheFuture +// + +// $ExpectType boolean +date.isInTheFuture('2019'); + +// $ExpectType boolean +date.isInTheFuture(Date.now()); + +// $ExpectType boolean +date.isInTheFuture(moment()); + +// +// setSettings +// + +declare const settings: date.DateSettings; + +// $ExpectType void +date.setSettings(settings);