From 4c2c6fd18a00bc291c7f18f156b0245a85c00b30 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Mon, 18 Feb 2019 22:32:00 +0200 Subject: [PATCH 01/11] index.d.ts move & changes - move inside /src for auto-detection by tsc - fix UnitType type to match actual js code - change signature of PluginFunc and extend to allow better type handling # Conflicts: # package.json --- package.json | 2 +- index.d.ts => src/index.d.ts | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) rename index.d.ts => src/index.d.ts (73%) diff --git a/package.json b/package.json index c600258e1..187269682 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "scripts": { "test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && npm run test-tz && jest", "test-tz": "jest test/timezone.test --coverage=false", - "lint": "./node_modules/.bin/eslint src/* test/* build/*", + "lint": "./node_modules/.bin/eslint src/*.js test/*.js build/*.js", "prettier": "prettier --write \"docs/**/*.md\"", "babel": "cross-env BABEL_ENV=build babel src --out-dir esm --copy-files", "build": "cross-env BABEL_ENV=build node build && npm run size", diff --git a/index.d.ts b/src/index.d.ts similarity index 73% rename from index.d.ts rename to src/index.d.ts index 236a7d274..e96a49e08 100644 --- a/index.d.ts +++ b/src/index.d.ts @@ -6,8 +6,10 @@ declare namespace dayjs { export type OptionType = { locale: string } - type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms' - export type UnitType = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'quarter' | 'year' | 'date' | UnitTypeShort; + export type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms' + export type UnitTypeSingular = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year' | 'date' | UnitTypeShort; + export type UnitTypePlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'quarters' | 'years' | 'dates' + export type UnitType = UnitTypeSingular | UnitTypePlural type OpUnitTypeShort = 'w' export type OpUnitType = UnitType | "week" | OpUnitTypeShort; @@ -88,13 +90,13 @@ declare namespace dayjs { locale(arg1: any, arg2?: any): Dayjs } - export type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void + export type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void - export function extend(plugin: PluginFunc, option?: ConfigType): Dayjs + export function extend(plugin: PluginFunc, option?: ConfigType): Dayjs & TPlugin export function locale(arg1: any, arg2?: any): string export function isDayjs(d: any): d is Dayjs - + export function unix(t: number): Dayjs } From 9e69f1c0789cd9909de7b7e53e3890257b4aeb66 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Thu, 24 Jan 2019 00:11:26 +0200 Subject: [PATCH 02/11] add type declaration for relativeTime plugin and add type test --- src/plugin/relativeTime/index.d.ts | 11 +++++++++++ test/index.d.test.ts | 3 +++ 2 files changed, 14 insertions(+) create mode 100644 src/plugin/relativeTime/index.d.ts diff --git a/src/plugin/relativeTime/index.d.ts b/src/plugin/relativeTime/index.d.ts new file mode 100644 index 000000000..f4536b55c --- /dev/null +++ b/src/plugin/relativeTime/index.d.ts @@ -0,0 +1,11 @@ +import { ConfigType, PluginFunc } from '../../index' + +interface RelativeTimePlugin { + fromNow(withoutSuffix?: boolean): string + from(compared: ConfigType, withoutSuffix?: boolean): string + toNow(withoutSuffix?: boolean): string + to(compared: ConfigType, withoutSuffix?: boolean): string +} + +declare const pluginFn: PluginFunc +export default pluginFn diff --git a/test/index.d.test.ts b/test/index.d.test.ts index 4e6edb44c..bafef81d8 100644 --- a/test/index.d.test.ts +++ b/test/index.d.test.ts @@ -1,4 +1,5 @@ import dayjs from '../src' +import relativeTime from '../src/plugin/relativeTime/index' dayjs() @@ -77,3 +78,5 @@ dayjs().isSame(dayjs(), 'hours') dayjs().isAfter(dayjs(), 'year') dayjs('2000-01-01').isLeapYear() + +dayjs.extend(relativeTime).toNow() From 9b73a8fff69c720b8ebe52936463310310231de8 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Thu, 24 Jan 2019 00:36:45 +0200 Subject: [PATCH 03/11] add advancedFormat plugin types --- src/plugin/advancedFormat/index.d.ts | 8 ++++++++ test/index.d.test.ts | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 src/plugin/advancedFormat/index.d.ts diff --git a/src/plugin/advancedFormat/index.d.ts b/src/plugin/advancedFormat/index.d.ts new file mode 100644 index 000000000..c24773930 --- /dev/null +++ b/src/plugin/advancedFormat/index.d.ts @@ -0,0 +1,8 @@ +import { ConfigType, PluginFunc } from '../../index' + +interface AdvancedFormatPlugin { + format(advancedFormat: string): string +} + +declare const pluginFn: PluginFunc +export default pluginFn diff --git a/test/index.d.test.ts b/test/index.d.test.ts index bafef81d8..667c94149 100644 --- a/test/index.d.test.ts +++ b/test/index.d.test.ts @@ -1,5 +1,6 @@ import dayjs from '../src' -import relativeTime from '../src/plugin/relativeTime/index' +import relativeTime from '../src/plugin/relativeTime' +import advancedFormat from '../src/plugin/advancedFormat' dayjs() @@ -80,3 +81,4 @@ dayjs().isAfter(dayjs(), 'year') dayjs('2000-01-01').isLeapYear() dayjs.extend(relativeTime).toNow() +dayjs.extend(advancedFormat).format() From 049536e9c76e57bab4a1cbe9d7a80a39d8567085 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Thu, 24 Jan 2019 00:39:50 +0200 Subject: [PATCH 04/11] add buddhistEra plugin types --- src/plugin/buddhistEra/index.d.ts | 8 ++++++++ test/index.d.test.ts | 2 ++ 2 files changed, 10 insertions(+) create mode 100644 src/plugin/buddhistEra/index.d.ts diff --git a/src/plugin/buddhistEra/index.d.ts b/src/plugin/buddhistEra/index.d.ts new file mode 100644 index 000000000..824730fef --- /dev/null +++ b/src/plugin/buddhistEra/index.d.ts @@ -0,0 +1,8 @@ +import { ConfigType, PluginFunc } from '../../index' + +interface BuddhistEraPlugin { + format(buddhistEraFormat: string): string +} + +declare const pluginFn: PluginFunc +export default pluginFn diff --git a/test/index.d.test.ts b/test/index.d.test.ts index 667c94149..a57b855f1 100644 --- a/test/index.d.test.ts +++ b/test/index.d.test.ts @@ -1,6 +1,7 @@ import dayjs from '../src' import relativeTime from '../src/plugin/relativeTime' import advancedFormat from '../src/plugin/advancedFormat' +import buddhistEra from '../src/plugin/buddhistEra' dayjs() @@ -82,3 +83,4 @@ dayjs('2000-01-01').isLeapYear() dayjs.extend(relativeTime).toNow() dayjs.extend(advancedFormat).format() +dayjs.extend(buddhistEra).format() From 19b6b424d3993f9866e5df10a3b752d2e772ca28 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Thu, 7 Feb 2019 16:59:04 +0200 Subject: [PATCH 05/11] fix type of extend and add isLeafYear type definitions # Conflicts: # src/index.d.ts --- src/index.d.ts | 10 ++++++---- src/plugin/isLeapYear/index.d.ts | 8 ++++++++ test/index.d.test.ts | 9 +++++---- 3 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 src/plugin/isLeapYear/index.d.ts diff --git a/src/index.d.ts b/src/index.d.ts index e96a49e08..3d8335cbd 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,5 +1,7 @@ export = dayjs; -declare function dayjs (config?: dayjs.ConfigType, option?: dayjs.OptionType): dayjs.Dayjs +type WithReturnType = TFunction extends (...args: infer U) => infer R ? (...args: U) => R & TExtendedReturnType : TFunction; + +declare function dayjs(config?: dayjs.ConfigType, option?: dayjs.OptionType): dayjs.Dayjs declare namespace dayjs { export type ConfigType = string | number | Date | Dayjs @@ -85,14 +87,14 @@ declare namespace dayjs { isAfter(dayjs: ConfigType, unit?: OpUnitType): boolean - isLeapYear(): boolean - locale(arg1: any, arg2?: any): Dayjs } export type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void - export function extend(plugin: PluginFunc, option?: ConfigType): Dayjs & TPlugin + export type dayjsWithPlugin = WithReturnType; + + export function extend(plugin: PluginFunc, option?: ConfigType): dayjsWithPlugin; export function locale(arg1: any, arg2?: any): string diff --git a/src/plugin/isLeapYear/index.d.ts b/src/plugin/isLeapYear/index.d.ts new file mode 100644 index 000000000..c6c232d11 --- /dev/null +++ b/src/plugin/isLeapYear/index.d.ts @@ -0,0 +1,8 @@ +import { ConfigType, PluginFunc } from '../../index' + +interface IsLeapYearPlugin { + isLeapYear(): boolean +} + +declare const pluginFn: PluginFunc +export default pluginFn diff --git a/test/index.d.test.ts b/test/index.d.test.ts index a57b855f1..6365a39b3 100644 --- a/test/index.d.test.ts +++ b/test/index.d.test.ts @@ -2,6 +2,7 @@ import dayjs from '../src' import relativeTime from '../src/plugin/relativeTime' import advancedFormat from '../src/plugin/advancedFormat' import buddhistEra from '../src/plugin/buddhistEra' +import isLeapYear from '../src/plugin/isLeapYear' dayjs() @@ -79,8 +80,8 @@ dayjs().isSame(dayjs(), 'hours') dayjs().isAfter(dayjs(), 'year') -dayjs('2000-01-01').isLeapYear() -dayjs.extend(relativeTime).toNow() -dayjs.extend(advancedFormat).format() -dayjs.extend(buddhistEra).format() +dayjs.extend(relativeTime)().toNow() +dayjs.extend(advancedFormat)().format() +dayjs.extend(buddhistEra)().format() +dayjs.extend(isLeapYear)('2000-01-01').isLeapYear(); \ No newline at end of file From c2af2c24406f035d2570b61f809d0095411b0937 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Thu, 24 Jan 2019 01:01:12 +0200 Subject: [PATCH 06/11] add isBetween plugin type --- src/plugin/isBetween/index.d.ts | 8 ++++++++ test/index.d.test.ts | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 src/plugin/isBetween/index.d.ts diff --git a/src/plugin/isBetween/index.d.ts b/src/plugin/isBetween/index.d.ts new file mode 100644 index 000000000..171c6b477 --- /dev/null +++ b/src/plugin/isBetween/index.d.ts @@ -0,0 +1,8 @@ +import { ConfigType, PluginFunc, UnitType } from '../../index' + +interface IsBetweenPlugin { + isBetween(d1: ConfigType, d2: ConfigType, unit: UnitType): boolean; +} + +declare const pluginFn: PluginFunc +export default pluginFn diff --git a/test/index.d.test.ts b/test/index.d.test.ts index 6365a39b3..c7fddbfeb 100644 --- a/test/index.d.test.ts +++ b/test/index.d.test.ts @@ -3,6 +3,7 @@ import relativeTime from '../src/plugin/relativeTime' import advancedFormat from '../src/plugin/advancedFormat' import buddhistEra from '../src/plugin/buddhistEra' import isLeapYear from '../src/plugin/isLeapYear' +import isBetween from '../src/plugin/isBetween' dayjs() @@ -84,4 +85,5 @@ dayjs().isAfter(dayjs(), 'year') dayjs.extend(relativeTime)().toNow() dayjs.extend(advancedFormat)().format() dayjs.extend(buddhistEra)().format() -dayjs.extend(isLeapYear)('2000-01-01').isLeapYear(); \ No newline at end of file +dayjs.extend(isLeapYear)('2000-01-01').isLeapYear(); +dayjs.extend(isBetween)('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25'), 'year'); \ No newline at end of file From 2ccd8f89e8b2f168a217cbeade413b88da4de69c Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Thu, 24 Jan 2019 01:37:58 +0200 Subject: [PATCH 07/11] add multiple plugins chaining test --- test/plugins.test.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 test/plugins.test.js diff --git a/test/plugins.test.js b/test/plugins.test.js new file mode 100644 index 000000000..4d7e24148 --- /dev/null +++ b/test/plugins.test.js @@ -0,0 +1,23 @@ +import MockDate from 'mockdate' +import dayjs from '../src' +import relativeTime from '../src/plugin/relativeTime' +import isLeapYear from '../src/plugin/isLeapYear' + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +it('should allow multiple plugin chaining', () => { + const now = dayjs() + expect(now.fromNow).not.toBeDefined() + expect(now.isLeapYear).not.toBeDefined() + + const extendedDayjs = dayjs.extend(relativeTime).extend(isLeapYear) + const extendedNow = extendedDayjs() + expect(typeof extendedNow.fromNow).toBe('function') + expect(typeof extendedNow.isLeapYear).toBe('function') +}) From 47d99579bdf03d51e953e75e2c7030b12e04b6d7 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Thu, 7 Feb 2019 17:00:44 +0200 Subject: [PATCH 08/11] change how dayjs types are defined to support chaining extend calls # Conflicts: # src/index.d.ts --- src/index.d.ts | 104 +++++++++++++++++++++---------------------- test/index.d.test.ts | 9 ++-- 2 files changed, 56 insertions(+), 57 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 3d8335cbd..43f158f92 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,55 +1,64 @@ -export = dayjs; -type WithReturnType = TFunction extends (...args: infer U) => infer R ? (...args: U) => R & TExtendedReturnType : TFunction; -declare function dayjs(config?: dayjs.ConfigType, option?: dayjs.OptionType): dayjs.Dayjs +declare const dayjs: dayjs +export default dayjs -declare namespace dayjs { - export type ConfigType = string | number | Date | Dayjs +export type ConfigType = string | number | Date | Dayjs - export type OptionType = { locale: string } +export type OptionType = { locale: string } export type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms' export type UnitTypeSingular = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year' | 'date' | UnitTypeShort; - export type UnitTypePlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'quarters' | 'years' | 'dates' - export type UnitType = UnitTypeSingular | UnitTypePlural +export type UnitTypePlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'quarters' | 'years' | 'dates' +export type UnitType = UnitTypeSingular | UnitTypePlural type OpUnitTypeShort = 'w' export type OpUnitType = UnitType | "week" | OpUnitTypeShort; - interface DayjsObject { - years: number - months: number - date: number - hours: number - minutes: number - seconds: number - milliseconds: number - } +export type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void - class Dayjs { - constructor (config?: ConfigType) +export interface dayjs { + (config?: ConfigType, option?: OptionType): {} extends TPlugin ? Dayjs : Dayjs & TPlugin - clone(): Dayjs + extend(plugin: PluginFunc, option?: ConfigType): dayjs + locale(arg1: any, arg2?: any): string + isDayjs(d: any): d is Dayjs + unix(t: number): Dayjs +} + + export interface DayjsObject { + years: number + months: number + date: number + hours: number + minutes: number + seconds: number + milliseconds: number +} - isValid(): boolean + export class Dayjs { + constructor(config?: ConfigType) - year(): number + clone(): Dayjs - month(): number + isValid(): boolean - date(): number + year(): number - day(): number + month(): number - hour(): number + date(): number - minute(): number + day(): number - second(): number + hour(): number - millisecond(): number + minute(): number - set(unit: UnitType, value: number): Dayjs + second(): number + + millisecond(): number + + set(unit: UnitType, value: number): Dayjs add(value: number, unit: OpUnitType): Dayjs @@ -59,27 +68,27 @@ declare namespace dayjs { endOf(unit: OpUnitType): Dayjs - format(template?: string): string + format(template?: string): string diff(dayjs: ConfigType, unit: OpUnitType, float?: boolean): number - valueOf(): number + valueOf(): number - unix(): number + unix(): number - daysInMonth(): number + daysInMonth(): number - toDate(): Date + toDate(): Date - toArray(): number[] + toArray(): number[] - toJSON(): string + toJSON(): string - toISOString(): string + toISOString(): string - toObject(): DayjsObject + toObject(): DayjsObject - toString(): string + toString(): string isBefore(dayjs: ConfigType, unit?: OpUnitType): boolean @@ -87,18 +96,5 @@ declare namespace dayjs { isAfter(dayjs: ConfigType, unit?: OpUnitType): boolean - locale(arg1: any, arg2?: any): Dayjs - } - - export type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void - - export type dayjsWithPlugin = WithReturnType; - - export function extend(plugin: PluginFunc, option?: ConfigType): dayjsWithPlugin; - - export function locale(arg1: any, arg2?: any): string - - export function isDayjs(d: any): d is Dayjs - - export function unix(t: number): Dayjs + locale(arg1: any, arg2?: any): Dayjs } diff --git a/test/index.d.test.ts b/test/index.d.test.ts index c7fddbfeb..2054ea8f1 100644 --- a/test/index.d.test.ts +++ b/test/index.d.test.ts @@ -81,9 +81,12 @@ dayjs().isSame(dayjs(), 'hours') dayjs().isAfter(dayjs(), 'year') - dayjs.extend(relativeTime)().toNow() dayjs.extend(advancedFormat)().format() dayjs.extend(buddhistEra)().format() -dayjs.extend(isLeapYear)('2000-01-01').isLeapYear(); -dayjs.extend(isBetween)('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25'), 'year'); \ No newline at end of file +dayjs.extend(isLeapYear)('2000-01-01').isLeapYear() +dayjs.extend(isBetween)('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25'), 'year') + +const multiplePluginsDayjs = dayjs.extend(relativeTime).extend(isLeapYear)() +multiplePluginsDayjs.toNow() +multiplePluginsDayjs.isLeapYear() From 279e2fe62402f89bf1b504d8289fb1ab2fb0e6b8 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Mon, 18 Feb 2019 22:32:40 +0200 Subject: [PATCH 09/11] fix formatting # Conflicts: # src/index.d.ts --- src/index.d.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 43f158f92..b55bc4868 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -6,13 +6,13 @@ export type ConfigType = string | number | Date | Dayjs export type OptionType = { locale: string } - export type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms' - export type UnitTypeSingular = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year' | 'date' | UnitTypeShort; +export type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms' +export type UnitTypeSingular = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year' | 'date' | UnitTypeShort; export type UnitTypePlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'quarters' | 'years' | 'dates' export type UnitType = UnitTypeSingular | UnitTypePlural - type OpUnitTypeShort = 'w' - export type OpUnitType = UnitType | "week" | OpUnitTypeShort; +export type OpUnitTypeShort = 'w' +export type OpUnitType = UnitType | "week" | OpUnitTypeShort; export type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void @@ -60,13 +60,13 @@ export interface dayjs { set(unit: UnitType, value: number): Dayjs - add(value: number, unit: OpUnitType): Dayjs + add(value: number, unit: OpUnitType): Dayjs - subtract(value: number, unit: OpUnitType): Dayjs + subtract(value: number, unit: OpUnitType): Dayjs - startOf(unit: OpUnitType): Dayjs + startOf(unit: OpUnitType): Dayjs - endOf(unit: OpUnitType): Dayjs + endOf(unit: OpUnitType): Dayjs format(template?: string): string From 2cde4d7bcd0df300075dfd47abfae4a740372d49 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Mon, 18 Feb 2019 22:32:58 +0200 Subject: [PATCH 10/11] change export type # Conflicts: # src/index.d.ts --- src/index.d.ts | 116 ++++++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 55 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index b55bc4868..0b8388339 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,94 +1,100 @@ +declare const dayjs: dayjs; +export = dayjs; -declare const dayjs: dayjs -export default dayjs +declare namespace dayjs { + type ConfigType = string | number | Date | Dayjs -export type ConfigType = string | number | Date | Dayjs + type OptionType = { locale: string } -export type OptionType = { locale: string } + type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms' + type UnitTypeSingular = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year' | 'date' | UnitTypeShort; + type UnitTypePlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'quarters' | 'years' | 'dates' + type UnitType = UnitTypeSingular | UnitTypePlural -export type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms' -export type UnitTypeSingular = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year' | 'date' | UnitTypeShort; -export type UnitTypePlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'quarters' | 'years' | 'dates' -export type UnitType = UnitTypeSingular | UnitTypePlural + type OpUnitTypeShort = 'w' + type OpUnitType = UnitType | "week" | OpUnitTypeShort; -export type OpUnitTypeShort = 'w' -export type OpUnitType = UnitType | "week" | OpUnitTypeShort; - -export type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void - -export interface dayjs { - (config?: ConfigType, option?: OptionType): {} extends TPlugin ? Dayjs : Dayjs & TPlugin + type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void +} - extend(plugin: PluginFunc, option?: ConfigType): dayjs - locale(arg1: any, arg2?: any): string - isDayjs(d: any): d is Dayjs - unix(t: number): Dayjs +interface dayjs { + (config?: dayjs.ConfigType, option?: dayjs.OptionType): {} extends TPlugin + ? Dayjs + : Dayjs & TPlugin; + + extend( + plugin: dayjs.PluginFunc, + option?: dayjs.ConfigType + ): dayjs; + locale(arg1: any, arg2?: any): string; + isDayjs(d: any): d is Dayjs; + unix(t: number): Dayjs; } - export interface DayjsObject { - years: number - months: number - date: number - hours: number - minutes: number - seconds: number - milliseconds: number +declare interface DayjsObject { + years: number; + months: number; + date: number; + hours: number; + minutes: number; + seconds: number; + milliseconds: number; } - export class Dayjs { - constructor(config?: ConfigType) +declare class Dayjs { + constructor(config?: dayjs.ConfigType); - clone(): Dayjs + clone(): Dayjs; - isValid(): boolean + isValid(): boolean; - year(): number + year(): number; - month(): number + month(): number; - date(): number + date(): number; - day(): number + day(): number; - hour(): number + hour(): number; - minute(): number + minute(): number; - second(): number + second(): number; - millisecond(): number + millisecond(): number; - set(unit: UnitType, value: number): Dayjs + set(unit: dayjs.UnitType, value: number): Dayjs; - add(value: number, unit: OpUnitType): Dayjs + add(value: number, unit: dayjs.OpUnitType): Dayjs; - subtract(value: number, unit: OpUnitType): Dayjs + subtract(value: number, unit: dayjs.OpUnitType): Dayjs; - startOf(unit: OpUnitType): Dayjs + startOf(unit: dayjs.OpUnitType): Dayjs; - endOf(unit: OpUnitType): Dayjs + endOf(unit: dayjs.OpUnitType): Dayjs; - format(template?: string): string + format(template?: string): string; diff(dayjs: ConfigType, unit: OpUnitType, float?: boolean): number - valueOf(): number + valueOf(): number; - unix(): number + unix(): number; - daysInMonth(): number + daysInMonth(): number; - toDate(): Date + toDate(): Date; - toArray(): number[] + toArray(): number[]; - toJSON(): string + toJSON(): string; - toISOString(): string + toISOString(): string; - toObject(): DayjsObject + toObject(): DayjsObject; - toString(): string + toString(): string; isBefore(dayjs: ConfigType, unit?: OpUnitType): boolean @@ -96,5 +102,5 @@ export interface dayjs { isAfter(dayjs: ConfigType, unit?: OpUnitType): boolean - locale(arg1: any, arg2?: any): Dayjs + locale(arg1: any, arg2?: any): Dayjs; } From f79c6a985f3e371261759015db462cac7348e934 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Mon, 18 Feb 2019 22:54:21 +0200 Subject: [PATCH 11/11] add types for `CustomParseFormat` plugin, and allow creating types for static plugins --- src/index.d.ts | 20 ++++++++++++++------ src/plugin/customParseFormat/index.d.ts | 8 ++++++++ test/{index.d.test.ts => index.test.d.ts} | 4 ++++ 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 src/plugin/customParseFormat/index.d.ts rename test/{index.d.test.ts => index.test.d.ts} (88%) diff --git a/src/index.d.ts b/src/index.d.ts index 0b8388339..02f790bbf 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -14,7 +14,15 @@ declare namespace dayjs { type OpUnitTypeShort = 'w' type OpUnitType = UnitType | "week" | OpUnitTypeShort; - type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void + type PluginFunc = (option: ConfigType, dayjsClass: InstanceType, dayjsFactory: dayjs) => void; + + /** + * Branding for static plugins, to enable better type inference. + * `extend` from this to make the plugin static + */ + interface StaticPlugin { + __staticPlugin: never; + } } interface dayjs { @@ -25,7 +33,7 @@ interface dayjs { extend( plugin: dayjs.PluginFunc, option?: dayjs.ConfigType - ): dayjs; + ): UPlugin extends dayjs.StaticPlugin ? (dayjs & UPlugin) : dayjs; locale(arg1: any, arg2?: any): string; isDayjs(d: any): d is Dayjs; unix(t: number): Dayjs; @@ -76,7 +84,7 @@ declare class Dayjs { format(template?: string): string; - diff(dayjs: ConfigType, unit: OpUnitType, float?: boolean): number + diff(dayjs: ConfigType, unit: OpUnitType, float?: boolean): number valueOf(): number; @@ -96,11 +104,11 @@ declare class Dayjs { toString(): string; - isBefore(dayjs: ConfigType, unit?: OpUnitType): boolean + isBefore(dayjs: ConfigType, unit?: OpUnitType): boolean - isSame(dayjs: ConfigType, unit?: OpUnitType): boolean + isSame(dayjs: ConfigType, unit?: OpUnitType): boolean - isAfter(dayjs: ConfigType, unit?: OpUnitType): boolean + isAfter(dayjs: ConfigType, unit?: OpUnitType): boolean locale(arg1: any, arg2?: any): Dayjs; } diff --git a/src/plugin/customParseFormat/index.d.ts b/src/plugin/customParseFormat/index.d.ts new file mode 100644 index 000000000..91ff52fc7 --- /dev/null +++ b/src/plugin/customParseFormat/index.d.ts @@ -0,0 +1,8 @@ +import { ConfigType, PluginFunc, StaticPlugin } from '../../index' + +interface CustomParseFormatPlugin extends StaticPlugin { + (input: string, format: string): this; +} + +declare const pluginFn: PluginFunc; +export default pluginFn diff --git a/test/index.d.test.ts b/test/index.test.d.ts similarity index 88% rename from test/index.d.test.ts rename to test/index.test.d.ts index 2054ea8f1..10a9a7b9f 100644 --- a/test/index.d.test.ts +++ b/test/index.test.d.ts @@ -4,6 +4,7 @@ import advancedFormat from '../src/plugin/advancedFormat' import buddhistEra from '../src/plugin/buddhistEra' import isLeapYear from '../src/plugin/isLeapYear' import isBetween from '../src/plugin/isBetween' +import customParseFormat from '../src/plugin/customParseFormat' dayjs() @@ -90,3 +91,6 @@ dayjs.extend(isBetween)('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25' const multiplePluginsDayjs = dayjs.extend(relativeTime).extend(isLeapYear)() multiplePluginsDayjs.toNow() multiplePluginsDayjs.isLeapYear() + +const dayjsWithCustomParseFormat = dayjs.extend(customParseFormat); +dayjsWithCustomParseFormat('05/02/69 1:02:03 PM -05:00', 'MM/DD/YY H:mm:ss A Z');