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 c600258e1..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,11 +84,12 @@ "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", "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", diff --git a/test/index.d.test.ts b/test/index.d.test.ts index 4e6edb44c..ab800bab7 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() @@ -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() @@ -70,10 +76,16 @@ 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') 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/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/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/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/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/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 58% rename from index.d.ts rename to types/index.d.ts index 236a7d274..e1733a902 100644 --- a/index.d.ts +++ b/types/index.d.ts @@ -1,10 +1,13 @@ 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 - export type OptionType = { locale: string } + /** @deprecated Renamed to DateType. */ + export type ConfigType = DateType + + 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; @@ -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,24 +80,24 @@ 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 - + export function unix(t: number): Dayjs } 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/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/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/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 + } +} 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 + } +}