From e6f3b88ca43d290be2a06699710c1a4221ef5102 Mon Sep 17 00:00:00 2001 From: Yuya Tanaka Date: Thu, 29 Nov 2018 18:59:13 +0900 Subject: [PATCH 1/8] Bump rollup to rid of @types/acorn --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c600258e1..4ee5aabba 100644 --- a/package.json +++ b/package.json @@ -88,7 +88,7 @@ "moment": "^2.22.0", "pre-commit": "^1.2.2", "prettier": "^1.16.1", - "rollup": "^0.57.1", + "rollup": "^0.67.3", "rollup-plugin-babel": "^4.0.0-beta.4", "rollup-plugin-uglify": "^3.0.0", "size-limit": "^0.18.0", From 4b8b23a0d8811eb90f107471ca405349248896f1 Mon Sep 17 00:00:00 2001 From: Yuya Tanaka Date: Thu, 29 Nov 2018 20:22:48 +0900 Subject: [PATCH 2/8] Move type definition to types dir --- .gitignore | 1 + .npmignore | 3 ++- build/index.js | 3 +++ package.json | 3 ++- test/index.d.test.ts | 2 +- tsconfig.json | 20 ++++++++++++++++++++ index.d.ts => types/index.d.ts | 0 7 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 tsconfig.json rename index.d.ts => types/index.d.ts (100%) diff --git a/.gitignore b/.gitignore index c90d1cc7b..d022dbedb 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ coverage /plugin dayjs.min.js /esm +index.d.ts #dev demo.js diff --git a/.npmignore b/.npmignore index 4f08223c0..1839a8b65 100644 --- a/.npmignore +++ b/.npmignore @@ -15,6 +15,7 @@ coverage # dev src test +types build .babelrc .eslintrc.json @@ -25,4 +26,4 @@ docs #other .travis.yml -karma.sauce.conf.js \ No newline at end of file +karma.sauce.conf.js diff --git a/build/index.js b/build/index.js index a5e08fda6..0dd2e134b 100644 --- a/build/index.js +++ b/build/index.js @@ -3,6 +3,7 @@ const configFactory = require('./rollup.config') const fs = require('fs') const util = require('util') const path = require('path') +const mergedirs = require('merge-dirs').default const { promisify } = util @@ -39,6 +40,8 @@ async function build(option) { input: './src/index.js', fileName: './dayjs.min.js' })) + + mergedirs('./types/', './', 'overwrite') } catch (e) { console.error(e) // eslint-disable-line no-console } diff --git a/package.json b/package.json index 4ee5aabba..acdf3ba8a 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "types": "index.d.ts", "module": "dayjs.min.js", "scripts": { - "test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && npm run test-tz && jest", + "test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && npm run test-tz && tsc && jest", "test-tz": "jest test/timezone.test --coverage=false", "lint": "./node_modules/.bin/eslint src/* test/* build/*", "prettier": "prettier --write \"docs/**/*.md\"", @@ -84,6 +84,7 @@ "karma": "^2.0.2", "karma-jasmine": "^1.1.2", "karma-sauce-launcher": "^1.1.0", + "merge-dirs": "^0.2.1", "mockdate": "^2.0.2", "moment": "^2.22.0", "pre-commit": "^1.2.2", diff --git a/test/index.d.test.ts b/test/index.d.test.ts index 4e6edb44c..14796cb24 100644 --- a/test/index.d.test.ts +++ b/test/index.d.test.ts @@ -1,4 +1,4 @@ -import dayjs from '../src' +import * as dayjs from 'dayjs' dayjs() diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 000000000..c79e47e21 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "strict": true, + "noEmit": true, + "lib": [ + // TODO: Unfortunately, rollup brings @types/node and etc. with "dependencies", not "devDependencies". + // It references Symbol type. + "es5", + "es2015.symbol" + ], + "baseUrl": "./types", + "paths": { + "dayjs": [ "." ], + "dayjs/*": [ "*" ] + } + }, + "exclude": [ + "node_modules" + ] +} diff --git a/index.d.ts b/types/index.d.ts similarity index 100% rename from index.d.ts rename to types/index.d.ts From 68a4c3ad0f2ba0b9d0bef4f4ad6cc626de21de74 Mon Sep 17 00:00:00 2001 From: Yuya Tanaka Date: Thu, 29 Nov 2018 20:56:01 +0900 Subject: [PATCH 3/8] Remove trailing spaces in index.d.ts --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 236a7d274..26ea5a4ce 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -95,6 +95,6 @@ declare namespace dayjs { export function locale(arg1: any, arg2?: any): string export function isDayjs(d: any): d is Dayjs - + export function unix(t: number): Dayjs } From 069c4af16df90115739984d35a1ceb4709066762 Mon Sep 17 00:00:00 2001 From: Yuya Tanaka Date: Thu, 29 Nov 2018 21:12:06 +0900 Subject: [PATCH 4/8] Fix wrong argument types --- test/index.d.test.ts | 6 ++++++ types/index.d.ts | 25 ++++++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/test/index.d.test.ts b/test/index.d.test.ts index 14796cb24..156a73023 100644 --- a/test/index.d.test.ts +++ b/test/index.d.test.ts @@ -77,3 +77,9 @@ dayjs().isSame(dayjs(), 'hours') dayjs().isAfter(dayjs(), 'year') dayjs('2000-01-01').isLeapYear() + +dayjs.extend((o, c, d) => { + o.locale.trim() + new c().unix() // eslint-disable-line new-cap + d().unix() +}) diff --git a/types/index.d.ts b/types/index.d.ts index 26ea5a4ce..48b9ef6b2 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,8 +1,11 @@ export = dayjs; -declare function dayjs (config?: dayjs.ConfigType, option?: dayjs.OptionType): dayjs.Dayjs +declare function dayjs (date?: dayjs.DateType, option?: dayjs.OptionType): dayjs.Dayjs declare namespace dayjs { - export type ConfigType = string | number | Date | Dayjs + export type DateType = string | number | Date | Dayjs + + /** @deprecated Renamed to DateType. */ + export type ConfigType = DateType export type OptionType = { locale: string } @@ -23,7 +26,7 @@ declare namespace dayjs { } class Dayjs { - constructor (config?: ConfigType) + constructor (date?: DateType) clone(): Dayjs @@ -57,7 +60,7 @@ declare namespace dayjs { format(template?: string): string - diff(dayjs: ConfigType, unit: OpUnitType, float?: boolean): number + diff(date: DateType, unit: OpUnitType, float?: boolean): number valueOf(): number @@ -77,22 +80,22 @@ declare namespace dayjs { toString(): string - isBefore(dayjs: ConfigType, unit?: OpUnitType): boolean + isBefore(date: DateType, unit?: OpUnitType): boolean - isSame(dayjs: ConfigType, unit?: OpUnitType): boolean + isSame(date: DateType, unit?: OpUnitType): boolean - isAfter(dayjs: ConfigType, unit?: OpUnitType): boolean + isAfter(date: DateType, unit?: OpUnitType): boolean isLeapYear(): boolean - locale(arg1: any, arg2?: any): Dayjs + locale(preset: string | { name: string, [key: string]: any }, object?: { [key: string]: any }): Dayjs } - export type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void + export type PluginFunc = (option: any, c: typeof Dayjs, d: typeof dayjs) => void - export function extend(plugin: PluginFunc, option?: ConfigType): Dayjs + export function extend(plugin: PluginFunc, option?: any): Dayjs - export function locale(arg1: any, arg2?: any): string + export function locale(preset: string | { name: string, [key: string]: any }, object?: { [key: string]: any }, isLocal?: boolean): string export function isDayjs(d: any): d is Dayjs From e088cb4b0cc2020f54aae39c079ddd72e7ff41ac Mon Sep 17 00:00:00 2001 From: Yuya Tanaka Date: Thu, 29 Nov 2018 21:12:23 +0900 Subject: [PATCH 5/8] Add type definitions for plugins --- test/plugin/isBetween.d.test.ts | 6 ++++++ test/plugin/isLeapYear.d.test.ts | 6 ++++++ test/plugin/relativeTime.d.test.ts | 20 ++++++++++++++++++++ test/plugin/weekOfYear.d.test.ts | 6 ++++++ types/plugin/advancedFormat.d.ts | 4 ++++ types/plugin/buddhistEra.d.ts | 4 ++++ types/plugin/isBetween.d.ts | 10 ++++++++++ types/plugin/isLeapYear.d.ts | 10 ++++++++++ types/plugin/relativeTime.d.ts | 13 +++++++++++++ types/plugin/weekOfYear.d.ts | 10 ++++++++++ 10 files changed, 89 insertions(+) create mode 100644 test/plugin/isBetween.d.test.ts create mode 100644 test/plugin/isLeapYear.d.test.ts create mode 100644 test/plugin/relativeTime.d.test.ts create mode 100644 test/plugin/weekOfYear.d.test.ts create mode 100644 types/plugin/advancedFormat.d.ts create mode 100644 types/plugin/buddhistEra.d.ts create mode 100644 types/plugin/isBetween.d.ts create mode 100644 types/plugin/isLeapYear.d.ts create mode 100644 types/plugin/relativeTime.d.ts create mode 100644 types/plugin/weekOfYear.d.ts diff --git a/test/plugin/isBetween.d.test.ts b/test/plugin/isBetween.d.test.ts new file mode 100644 index 000000000..4542e4b61 --- /dev/null +++ b/test/plugin/isBetween.d.test.ts @@ -0,0 +1,6 @@ +import * as dayjs from 'dayjs' +import * as isBetween from 'dayjs/plugin/isBetween' + +dayjs.extend(isBetween) + +dayjs('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25')) === true diff --git a/test/plugin/isLeapYear.d.test.ts b/test/plugin/isLeapYear.d.test.ts new file mode 100644 index 000000000..399930658 --- /dev/null +++ b/test/plugin/isLeapYear.d.test.ts @@ -0,0 +1,6 @@ +import * as dayjs from 'dayjs' +import * as isLeapYear from 'dayjs/plugin/isLeapYear' + +dayjs.extend(isLeapYear) + +dayjs('2010-10-20').isLeapYear() === false diff --git a/test/plugin/relativeTime.d.test.ts b/test/plugin/relativeTime.d.test.ts new file mode 100644 index 000000000..1124553f0 --- /dev/null +++ b/test/plugin/relativeTime.d.test.ts @@ -0,0 +1,20 @@ +import * as dayjs from 'dayjs' +import * as relativeTime from 'dayjs/plugin/relativeTime' + +dayjs.extend(relativeTime) + +dayjs().fromNow().trim() +dayjs().fromNow(true).trim() + +dayjs().from(dayjs()).trim() +dayjs().from(123, true).trim() +dayjs().from('2018-01-23').trim() +dayjs().from(new Date(), true).trim() + +dayjs().toNow().trim() +dayjs().toNow(true).trim() + +dayjs().to(dayjs()).trim() +dayjs().to(123, true).trim() +dayjs().to('2018-01-23').trim() +dayjs().to(new Date(), true).trim() diff --git a/test/plugin/weekOfYear.d.test.ts b/test/plugin/weekOfYear.d.test.ts new file mode 100644 index 000000000..bfaceede9 --- /dev/null +++ b/test/plugin/weekOfYear.d.test.ts @@ -0,0 +1,6 @@ +import * as dayjs from 'dayjs' +import * as weekOfYear from 'dayjs/plugin/weekOfYear' + +dayjs.extend(weekOfYear) + +dayjs('2010-10-20').week() === 43 diff --git a/types/plugin/advancedFormat.d.ts b/types/plugin/advancedFormat.d.ts new file mode 100644 index 000000000..30ec75e5d --- /dev/null +++ b/types/plugin/advancedFormat.d.ts @@ -0,0 +1,4 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin diff --git a/types/plugin/buddhistEra.d.ts b/types/plugin/buddhistEra.d.ts new file mode 100644 index 000000000..30ec75e5d --- /dev/null +++ b/types/plugin/buddhistEra.d.ts @@ -0,0 +1,4 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin diff --git a/types/plugin/isBetween.d.ts b/types/plugin/isBetween.d.ts new file mode 100644 index 000000000..41afb64cf --- /dev/null +++ b/types/plugin/isBetween.d.ts @@ -0,0 +1,10 @@ +import { PluginFunc, DateType } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + isBetween(a: DateType, b: DateType): boolean + } +} diff --git a/types/plugin/isLeapYear.d.ts b/types/plugin/isLeapYear.d.ts new file mode 100644 index 000000000..5be74092b --- /dev/null +++ b/types/plugin/isLeapYear.d.ts @@ -0,0 +1,10 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + isLeapYear(): boolean + } +} diff --git a/types/plugin/relativeTime.d.ts b/types/plugin/relativeTime.d.ts new file mode 100644 index 000000000..306493002 --- /dev/null +++ b/types/plugin/relativeTime.d.ts @@ -0,0 +1,13 @@ +import { PluginFunc, DateType } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + fromNow(withoutSuffix?: boolean): string + from(compared: DateType, withoutSuffix?: boolean): string + toNow(withoutSuffix?: boolean): string + to(compared: DateType, withoutSuffix?: boolean): string + } +} diff --git a/types/plugin/weekOfYear.d.ts b/types/plugin/weekOfYear.d.ts new file mode 100644 index 000000000..360219556 --- /dev/null +++ b/types/plugin/weekOfYear.d.ts @@ -0,0 +1,10 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + week(): number + } +} From 3610c73928c5fd178495f1d92f0c3a0f07d3469d Mon Sep 17 00:00:00 2001 From: Yuya Tanaka Date: Thu, 21 Feb 2019 15:02:13 +0900 Subject: [PATCH 6/8] Fix wrong argument in index.d.test.ts --- test/index.d.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/index.d.test.ts b/test/index.d.test.ts index 156a73023..17ecf6291 100644 --- a/test/index.d.test.ts +++ b/test/index.d.test.ts @@ -70,9 +70,9 @@ dayjs().isSame(dayjs()) dayjs().isAfter(dayjs()) -dayjs().isBefore(dayjs(), 'minutes') +dayjs().isBefore(dayjs(), 'minute') -dayjs().isSame(dayjs(), 'hours') +dayjs().isSame(dayjs(), 'hour') dayjs().isAfter(dayjs(), 'year') From 10518149543f34a1b27bb64d1ac1adb8d13ca470 Mon Sep 17 00:00:00 2001 From: Yuya Tanaka Date: Thu, 21 Feb 2019 15:58:40 +0900 Subject: [PATCH 7/8] Add more plugin types --- test/plugin/dayOfYear.d.test.ts | 6 ++++++ test/plugin/isSameOrAfter.d.test.ts | 6 ++++++ test/plugin/isSameOrBefore.d.test.ts | 6 ++++++ test/plugin/quarterOfYear.d.test.ts | 6 ++++++ types/plugin/customParseFormat.ts | 4 ++++ types/plugin/dayOfYear.d.ts | 10 ++++++++++ types/plugin/isSameOrAfter.d.ts | 10 ++++++++++ types/plugin/isSameOrBefore.d.ts | 10 ++++++++++ types/plugin/localizedFormat.d.ts | 4 ++++ types/plugin/quarterOfYear.d.ts | 10 ++++++++++ 10 files changed, 72 insertions(+) create mode 100644 test/plugin/dayOfYear.d.test.ts create mode 100644 test/plugin/isSameOrAfter.d.test.ts create mode 100644 test/plugin/isSameOrBefore.d.test.ts create mode 100644 test/plugin/quarterOfYear.d.test.ts create mode 100644 types/plugin/customParseFormat.ts create mode 100644 types/plugin/dayOfYear.d.ts create mode 100644 types/plugin/isSameOrAfter.d.ts create mode 100644 types/plugin/isSameOrBefore.d.ts create mode 100644 types/plugin/localizedFormat.d.ts create mode 100644 types/plugin/quarterOfYear.d.ts diff --git a/test/plugin/dayOfYear.d.test.ts b/test/plugin/dayOfYear.d.test.ts new file mode 100644 index 000000000..85173c66e --- /dev/null +++ b/test/plugin/dayOfYear.d.test.ts @@ -0,0 +1,6 @@ +import * as dayjs from 'dayjs' +import * as dayOfYear from 'dayjs/plugin/dayOfYear' + +dayjs.extend(dayOfYear) + +dayjs('2015-01-01T00:00:00.000').dayOfYear() === 1 diff --git a/test/plugin/isSameOrAfter.d.test.ts b/test/plugin/isSameOrAfter.d.test.ts new file mode 100644 index 000000000..b8f257cfd --- /dev/null +++ b/test/plugin/isSameOrAfter.d.test.ts @@ -0,0 +1,6 @@ +import * as dayjs from 'dayjs' +import * as isSameOrAfter from 'dayjs/plugin/isSameOrAfter' + +dayjs.extend(isSameOrAfter) + +dayjs('2010-10-20').isSameOrAfter('2010-10-19', 'year') === true diff --git a/test/plugin/isSameOrBefore.d.test.ts b/test/plugin/isSameOrBefore.d.test.ts new file mode 100644 index 000000000..5dcf966ee --- /dev/null +++ b/test/plugin/isSameOrBefore.d.test.ts @@ -0,0 +1,6 @@ +import * as dayjs from 'dayjs' +import * as isSameOrBefore from 'dayjs/plugin/isSameOrBefore' + +dayjs.extend(isSameOrBefore) + +dayjs('2010-10-20').isSameOrBefore('2010-10-19', 'year') === true diff --git a/test/plugin/quarterOfYear.d.test.ts b/test/plugin/quarterOfYear.d.test.ts new file mode 100644 index 000000000..f3f2b8ae7 --- /dev/null +++ b/test/plugin/quarterOfYear.d.test.ts @@ -0,0 +1,6 @@ +import * as dayjs from 'dayjs' +import * as quarterOfYear from 'dayjs/plugin/quarterOfYear' + +dayjs.extend(quarterOfYear) + +dayjs('2013-01-01T00:00:00.000').quarter() === 1 diff --git a/types/plugin/customParseFormat.ts b/types/plugin/customParseFormat.ts new file mode 100644 index 000000000..30ec75e5d --- /dev/null +++ b/types/plugin/customParseFormat.ts @@ -0,0 +1,4 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin diff --git a/types/plugin/dayOfYear.d.ts b/types/plugin/dayOfYear.d.ts new file mode 100644 index 000000000..028385152 --- /dev/null +++ b/types/plugin/dayOfYear.d.ts @@ -0,0 +1,10 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + dayOfYear(): number + } +} diff --git a/types/plugin/isSameOrAfter.d.ts b/types/plugin/isSameOrAfter.d.ts new file mode 100644 index 000000000..12d6f889d --- /dev/null +++ b/types/plugin/isSameOrAfter.d.ts @@ -0,0 +1,10 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + isSameOrAfter(date: DateType, unit?: OpUnitType): boolean + } +} diff --git a/types/plugin/isSameOrBefore.d.ts b/types/plugin/isSameOrBefore.d.ts new file mode 100644 index 000000000..3442c69ad --- /dev/null +++ b/types/plugin/isSameOrBefore.d.ts @@ -0,0 +1,10 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + isSameOrBefore(date: DateType, unit?: OpUnitType): boolean + } +} diff --git a/types/plugin/localizedFormat.d.ts b/types/plugin/localizedFormat.d.ts new file mode 100644 index 000000000..30ec75e5d --- /dev/null +++ b/types/plugin/localizedFormat.d.ts @@ -0,0 +1,4 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin diff --git a/types/plugin/quarterOfYear.d.ts b/types/plugin/quarterOfYear.d.ts new file mode 100644 index 000000000..e8aad14b8 --- /dev/null +++ b/types/plugin/quarterOfYear.d.ts @@ -0,0 +1,10 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + quarter(): number + } +} From 0bb72c67b235fdbb4acdca913d4b8e634dd28453 Mon Sep 17 00:00:00 2001 From: Yuya Tanaka Date: Fri, 22 Feb 2019 11:49:10 +0900 Subject: [PATCH 8/8] Fix OptionType signature to accept format --- test/index.d.test.ts | 6 ++++++ types/index.d.ts | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/test/index.d.test.ts b/test/index.d.test.ts index 17ecf6291..ab800bab7 100644 --- a/test/index.d.test.ts +++ b/test/index.d.test.ts @@ -8,6 +8,12 @@ dayjs(730944000000) dayjs(new Date(1993, 3, 1)) +dayjs('05/02/69 1:02:03 PM -05:00', 'MM/DD/YY H:mm:ss A Z') + +dayjs('05/02/69 1:02:03 PM -05:00', { format: 'MM/DD/YY H:mm:ss A Z' }) + +dayjs('1993-03-1', { locale: 'ja' }) + dayjs().clone() dayjs().isValid() diff --git a/types/index.d.ts b/types/index.d.ts index 48b9ef6b2..e1733a902 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -7,7 +7,7 @@ declare namespace dayjs { /** @deprecated Renamed to DateType. */ export type ConfigType = DateType - export type OptionType = { locale: string } + export type OptionType = { locale?: string, format?: string } | string type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms' export type UnitType = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'quarter' | 'year' | 'date' | UnitTypeShort;