From bc39d3d32046c106fcf866e1766045c7632939ff Mon Sep 17 00:00:00 2001 From: Winslow Dalpe Date: Sat, 28 Oct 2017 15:51:36 -0700 Subject: [PATCH 1/2] Adding typings for moment-holiday PR upstream to add typings in the library has been open for a while without feedback. Adding the typings here until that PR changes state. --- types/moment-holiday/index.d.ts | 71 +++++++++++ types/moment-holiday/moment-holiday-tests.ts | 126 +++++++++++++++++++ types/moment-holiday/package.json | 6 + types/moment-holiday/tsconfig.json | 23 ++++ types/moment-holiday/tslint.json | 1 + 5 files changed, 227 insertions(+) create mode 100644 types/moment-holiday/index.d.ts create mode 100644 types/moment-holiday/moment-holiday-tests.ts create mode 100644 types/moment-holiday/package.json create mode 100644 types/moment-holiday/tsconfig.json create mode 100644 types/moment-holiday/tslint.json diff --git a/types/moment-holiday/index.d.ts b/types/moment-holiday/index.d.ts new file mode 100644 index 0000000000..fffe81ed73 --- /dev/null +++ b/types/moment-holiday/index.d.ts @@ -0,0 +1,71 @@ +// Type definitions for moment-holiday 1.5 +// Project: https://github.com/kodie/moment-holiday +// Definitions by: Robert Winslow Dalpe +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import * as moment from 'moment'; + +declare module 'moment' { + interface Moment extends Object { + holiday( + holidays?: string[] | string, + adjust?: boolean): Moment | false | { [holidayName: string]: Moment }; + + holidays( + holidays?: string[] | string, + adjust?: boolean): Moment | false | { [holidayName: string]: Moment }; + + isHoliday( + holidays?: string[] | string | null, + adjust?: boolean): boolean | string | string[]; + + previousHoliday(count?: number, adjust?: boolean): Moment[] | Moment; + + previousHolidays(count?: number, adjust?: boolean): Moment[] | Moment; + + nextHoliday(count?: number, adjust?: boolean): Moment[] | Moment; + + nextHolidays(count?: number, adjust?: boolean): Moment[] | Moment; + + holidaysBetween(m: Moment, adjust?: boolean): Moment[] | false; + } + + interface HolidayDefinition { + date: string; + keywords?: string[]; + keywords_y?: string[]; + keywords_n?: string[]; + regions?: string[]; + regions_n?: string[]; + } + + interface HolidaysMapping { + [holidayName: string]: HolidayDefinition; + } + + interface Holidays { + active: HolidaysMapping; + active_last: HolidaysMapping; + } + + interface HolidayModifier { + set( + holidays: HolidaysMapping | string | string[], + specifics?: any): HolidayModifier; + + add( + holidays: HolidaysMapping | string, + specifics?: any): HolidayModifier; + + remove(holidays: string | string[]): HolidayModifier; + + undo(): HolidayModifier; + + load(locales: string | string[]): HolidayModifier; + + extendParser(parserFunc: (m: Moment, date: string) => Moment | Moment[] | false | void): HolidayModifier; + } + + let holidays: Holidays; + let modifyHolidays: HolidayModifier; +} diff --git a/types/moment-holiday/moment-holiday-tests.ts b/types/moment-holiday/moment-holiday-tests.ts new file mode 100644 index 0000000000..d6d0941f38 --- /dev/null +++ b/types/moment-holiday/moment-holiday-tests.ts @@ -0,0 +1,126 @@ +import * as moment from 'moment'; + +let holidayResult: moment.Moment | false | { [holidayName: string]: moment.Moment }; +holidayResult = moment().holiday([]); +holidayResult = moment().holiday([], false); +holidayResult = moment().holiday([], true); +holidayResult = moment().holiday('string'); +holidayResult = moment().holiday('string', false); +holidayResult = moment().holiday('string', true); +holidayResult = moment().holiday(['array', 'of', 'strings']); +holidayResult = moment().holiday(['array', 'of', 'strings'], false); +holidayResult = moment().holiday(['array', 'of', 'strings'], true); + +holidayResult = moment().holidays([]); +holidayResult = moment().holidays([], false); +holidayResult = moment().holidays([], true); +holidayResult = moment().holidays('string'); +holidayResult = moment().holidays('string', false); +holidayResult = moment().holidays('string', true); +holidayResult = moment().holidays(['array', 'of', 'strings']); +holidayResult = moment().holidays(['array', 'of', 'strings'], false); +holidayResult = moment().holidays(['array', 'of', 'strings'], true); + +let isHolidayResult: boolean | string | string[]; +isHolidayResult = moment().isHoliday(); +isHolidayResult = moment().isHoliday([]); +isHolidayResult = moment().isHoliday([], false); +isHolidayResult = moment().isHoliday([], true); +isHolidayResult = moment().isHoliday(['array', 'of', 'strings']); +isHolidayResult = moment().isHoliday(['array', 'of', 'strings'], false); +isHolidayResult = moment().isHoliday(['array', 'of', 'strings'], true); +isHolidayResult = moment().isHoliday('string'); +isHolidayResult = moment().isHoliday('string', false); +isHolidayResult = moment().isHoliday('string', true); +isHolidayResult = moment().isHoliday(null); +isHolidayResult = moment().isHoliday(null, false); +isHolidayResult = moment().isHoliday(null, true); + +let previousHolidayResult: moment.Moment[] | moment.Moment; +previousHolidayResult = moment().previousHoliday(); +previousHolidayResult = moment().previousHoliday(1); +previousHolidayResult = moment().previousHoliday(1, false); +previousHolidayResult = moment().previousHoliday(1, true); +previousHolidayResult = moment().previousHolidays(); +previousHolidayResult = moment().previousHolidays(1); +previousHolidayResult = moment().previousHolidays(1, false); +previousHolidayResult = moment().previousHolidays(1, true); + +let nextHolidayResult: moment.Moment[] | moment.Moment; +nextHolidayResult = moment().nextHoliday(); +nextHolidayResult = moment().nextHoliday(1); +nextHolidayResult = moment().nextHoliday(1, false); +nextHolidayResult = moment().nextHoliday(1, true); +nextHolidayResult = moment().nextHolidays(); +nextHolidayResult = moment().nextHolidays(1); +nextHolidayResult = moment().nextHolidays(1, false); +nextHolidayResult = moment().nextHolidays(1, true); + +let holidaysBetweenResult: moment.Moment[] | false; +holidaysBetweenResult = moment().holidaysBetween(moment()); +holidaysBetweenResult = moment().holidaysBetween(moment(), false); +holidaysBetweenResult = moment().holidaysBetween(moment(), true); + +let holidayDefinition: moment.HolidayDefinition = { + date: 'string' +}; +holidayDefinition = { + date: 'string', + keywords: ['array'], +}; +holidayDefinition = { + date: 'string', + keywords: ['array'], + keywords_n: ['array'], +}; +holidayDefinition = { + date: 'string', + keywords: ['array'], + keywords_n: ['array'], + keywords_y: ['array'], +}; +holidayDefinition = { + date: 'string', + keywords: ['array'], + keywords_n: ['array'], + keywords_y: ['array'], + regions: ['array'], +}; +holidayDefinition = { + date: 'string', + keywords: ['array'], + keywords_n: ['array'], + keywords_y: ['array'], + regions: ['array'], + regions_n: ['array'] +}; + +const holidays: moment.Holidays = moment.holidays; +let activeHolidays: moment.HolidaysMapping = moment.holidays.active; +activeHolidays = { + 'Some holiday name': holidayDefinition +}; + +let lastActiveHolidays: moment.HolidaysMapping = moment.holidays.active_last; +lastActiveHolidays = { + 'Some holiday name': holidayDefinition +}; + +let holidayModifier: moment.HolidayModifier = moment.modifyHolidays; +holidayModifier = holidayModifier.set('string'); +holidayModifier = holidayModifier.set(['array']); +holidayModifier = holidayModifier.set(activeHolidays); +holidayModifier = holidayModifier.set('string', {}); +holidayModifier = holidayModifier.set(['array'], {}); +holidayModifier = holidayModifier.set(activeHolidays, {}); +holidayModifier = holidayModifier.add(activeHolidays); +holidayModifier = holidayModifier.add('string'); +holidayModifier = holidayModifier.add(activeHolidays, {}); +holidayModifier = holidayModifier.add('string', {}); +holidayModifier = holidayModifier.remove('string'); +holidayModifier = holidayModifier.remove(['array']); +holidayModifier = holidayModifier.undo(); +holidayModifier = holidayModifier.load('string'); +holidayModifier = holidayModifier.load(['string']); +holidayModifier = holidayModifier.extendParser((m: moment.Moment, d: string): moment.Moment | moment.Moment[] | false | void => { +}); diff --git a/types/moment-holiday/package.json b/types/moment-holiday/package.json new file mode 100644 index 0000000000..4c7554a28e --- /dev/null +++ b/types/moment-holiday/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "moment": ">=2.0.0" + } +} diff --git a/types/moment-holiday/tsconfig.json b/types/moment-holiday/tsconfig.json new file mode 100644 index 0000000000..d03f37e12c --- /dev/null +++ b/types/moment-holiday/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", + "moment-holiday-tests.ts" + ] +} diff --git a/types/moment-holiday/tslint.json b/types/moment-holiday/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/moment-holiday/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From c5641e472d4a224d742cb02c57694e9452c9acee Mon Sep 17 00:00:00 2001 From: Winslow Dalpe Date: Mon, 6 Nov 2017 19:33:15 -0800 Subject: [PATCH 2/2] Using import = require syntax in moment-holiday tests --- types/moment-holiday/moment-holiday-tests.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/moment-holiday/moment-holiday-tests.ts b/types/moment-holiday/moment-holiday-tests.ts index d6d0941f38..474cf246fb 100644 --- a/types/moment-holiday/moment-holiday-tests.ts +++ b/types/moment-holiday/moment-holiday-tests.ts @@ -1,4 +1,5 @@ -import * as moment from 'moment'; +import moment = require('moment'); +import 'moment-holiday'; let holidayResult: moment.Moment | false | { [holidayName: string]: moment.Moment }; holidayResult = moment().holiday([]);