Skip to content

Commit

Permalink
fix: Add plugin type definitions (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
ypresto authored and iamkun committed Feb 23, 2019
1 parent 35e268a commit 361d437
Show file tree
Hide file tree
Showing 27 changed files with 221 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ coverage
/plugin
dayjs.min.js
/esm
index.d.ts

#dev
demo.js
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ coverage
# dev
src
test
types
build
.babelrc
.eslintrc.json
Expand All @@ -25,4 +26,4 @@ docs

#other
.travis.yml
karma.sauce.conf.js
karma.sauce.conf.js
3 changes: 3 additions & 0 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\"",
Expand Down Expand Up @@ -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",
Expand Down
18 changes: 15 additions & 3 deletions test/index.d.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dayjs from '../src'
import * as dayjs from 'dayjs'

dayjs()

Expand All @@ -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()
Expand Down Expand Up @@ -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()
})
6 changes: 6 additions & 0 deletions test/plugin/dayOfYear.d.test.ts
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions test/plugin/isBetween.d.test.ts
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions test/plugin/isLeapYear.d.test.ts
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions test/plugin/isSameOrAfter.d.test.ts
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions test/plugin/isSameOrBefore.d.test.ts
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions test/plugin/quarterOfYear.d.test.ts
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions test/plugin/relativeTime.d.test.ts
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 6 additions & 0 deletions test/plugin/weekOfYear.d.test.ts
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
29 changes: 16 additions & 13 deletions index.d.ts → types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,7 +26,7 @@ declare namespace dayjs {
}

class Dayjs {
constructor (config?: ConfigType)
constructor (date?: DateType)

clone(): Dayjs

Expand Down Expand Up @@ -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

Expand All @@ -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
}
4 changes: 4 additions & 0 deletions types/plugin/advancedFormat.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin
4 changes: 4 additions & 0 deletions types/plugin/buddhistEra.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin
4 changes: 4 additions & 0 deletions types/plugin/customParseFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin
10 changes: 10 additions & 0 deletions types/plugin/dayOfYear.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin

declare module 'dayjs' {
interface Dayjs {
dayOfYear(): number
}
}
10 changes: 10 additions & 0 deletions types/plugin/isBetween.d.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 10 additions & 0 deletions types/plugin/isLeapYear.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin

declare module 'dayjs' {
interface Dayjs {
isLeapYear(): boolean
}
}
10 changes: 10 additions & 0 deletions types/plugin/isSameOrAfter.d.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 10 additions & 0 deletions types/plugin/isSameOrBefore.d.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
4 changes: 4 additions & 0 deletions types/plugin/localizedFormat.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin
10 changes: 10 additions & 0 deletions types/plugin/quarterOfYear.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin

declare module 'dayjs' {
interface Dayjs {
quarter(): number
}
}
13 changes: 13 additions & 0 deletions types/plugin/relativeTime.d.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 10 additions & 0 deletions types/plugin/weekOfYear.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin

declare module 'dayjs' {
interface Dayjs {
week(): number
}
}

0 comments on commit 361d437

Please sign in to comment.