Skip to content

Commit 361d437

Browse files
yprestoiamkun
authored andcommitted
fix: Add plugin type definitions (#418)
1 parent 35e268a commit 361d437

27 files changed

+221
-19
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ coverage
1717
/plugin
1818
dayjs.min.js
1919
/esm
20+
index.d.ts
2021

2122
#dev
2223
demo.js

.npmignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ coverage
1515
# dev
1616
src
1717
test
18+
types
1819
build
1920
.babelrc
2021
.eslintrc.json
@@ -25,4 +26,4 @@ docs
2526

2627
#other
2728
.travis.yml
28-
karma.sauce.conf.js
29+
karma.sauce.conf.js

build/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const configFactory = require('./rollup.config')
33
const fs = require('fs')
44
const util = require('util')
55
const path = require('path')
6+
const mergedirs = require('merge-dirs').default
67

78
const { promisify } = util
89

@@ -39,6 +40,8 @@ async function build(option) {
3940
input: './src/index.js',
4041
fileName: './dayjs.min.js'
4142
}))
43+
44+
mergedirs('./types/', './', 'overwrite')
4245
} catch (e) {
4346
console.error(e) // eslint-disable-line no-console
4447
}

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"types": "index.d.ts",
77
"module": "dayjs.min.js",
88
"scripts": {
9-
"test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && npm run test-tz && jest",
9+
"test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && npm run test-tz && tsc && jest",
1010
"test-tz": "jest test/timezone.test --coverage=false",
1111
"lint": "./node_modules/.bin/eslint src/* test/* build/*",
1212
"prettier": "prettier --write \"docs/**/*.md\"",
@@ -84,11 +84,12 @@
8484
"karma": "^2.0.2",
8585
"karma-jasmine": "^1.1.2",
8686
"karma-sauce-launcher": "^1.1.0",
87+
"merge-dirs": "^0.2.1",
8788
"mockdate": "^2.0.2",
8889
"moment": "^2.22.0",
8990
"pre-commit": "^1.2.2",
9091
"prettier": "^1.16.1",
91-
"rollup": "^0.57.1",
92+
"rollup": "^0.67.3",
9293
"rollup-plugin-babel": "^4.0.0-beta.4",
9394
"rollup-plugin-uglify": "^3.0.0",
9495
"size-limit": "^0.18.0",

test/index.d.test.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dayjs from '../src'
1+
import * as dayjs from 'dayjs'
22

33
dayjs()
44

@@ -8,6 +8,12 @@ dayjs(730944000000)
88

99
dayjs(new Date(1993, 3, 1))
1010

11+
dayjs('05/02/69 1:02:03 PM -05:00', 'MM/DD/YY H:mm:ss A Z')
12+
13+
dayjs('05/02/69 1:02:03 PM -05:00', { format: 'MM/DD/YY H:mm:ss A Z' })
14+
15+
dayjs('1993-03-1', { locale: 'ja' })
16+
1117
dayjs().clone()
1218

1319
dayjs().isValid()
@@ -70,10 +76,16 @@ dayjs().isSame(dayjs())
7076

7177
dayjs().isAfter(dayjs())
7278

73-
dayjs().isBefore(dayjs(), 'minutes')
79+
dayjs().isBefore(dayjs(), 'minute')
7480

75-
dayjs().isSame(dayjs(), 'hours')
81+
dayjs().isSame(dayjs(), 'hour')
7682

7783
dayjs().isAfter(dayjs(), 'year')
7884

7985
dayjs('2000-01-01').isLeapYear()
86+
87+
dayjs.extend((o, c, d) => {
88+
o.locale.trim()
89+
new c().unix() // eslint-disable-line new-cap
90+
d().unix()
91+
})

test/plugin/dayOfYear.d.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as dayjs from 'dayjs'
2+
import * as dayOfYear from 'dayjs/plugin/dayOfYear'
3+
4+
dayjs.extend(dayOfYear)
5+
6+
dayjs('2015-01-01T00:00:00.000').dayOfYear() === 1

test/plugin/isBetween.d.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as dayjs from 'dayjs'
2+
import * as isBetween from 'dayjs/plugin/isBetween'
3+
4+
dayjs.extend(isBetween)
5+
6+
dayjs('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25')) === true

test/plugin/isLeapYear.d.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as dayjs from 'dayjs'
2+
import * as isLeapYear from 'dayjs/plugin/isLeapYear'
3+
4+
dayjs.extend(isLeapYear)
5+
6+
dayjs('2010-10-20').isLeapYear() === false

test/plugin/isSameOrAfter.d.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as dayjs from 'dayjs'
2+
import * as isSameOrAfter from 'dayjs/plugin/isSameOrAfter'
3+
4+
dayjs.extend(isSameOrAfter)
5+
6+
dayjs('2010-10-20').isSameOrAfter('2010-10-19', 'year') === true

test/plugin/isSameOrBefore.d.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as dayjs from 'dayjs'
2+
import * as isSameOrBefore from 'dayjs/plugin/isSameOrBefore'
3+
4+
dayjs.extend(isSameOrBefore)
5+
6+
dayjs('2010-10-20').isSameOrBefore('2010-10-19', 'year') === true

test/plugin/quarterOfYear.d.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as dayjs from 'dayjs'
2+
import * as quarterOfYear from 'dayjs/plugin/quarterOfYear'
3+
4+
dayjs.extend(quarterOfYear)
5+
6+
dayjs('2013-01-01T00:00:00.000').quarter() === 1

test/plugin/relativeTime.d.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as dayjs from 'dayjs'
2+
import * as relativeTime from 'dayjs/plugin/relativeTime'
3+
4+
dayjs.extend(relativeTime)
5+
6+
dayjs().fromNow().trim()
7+
dayjs().fromNow(true).trim()
8+
9+
dayjs().from(dayjs()).trim()
10+
dayjs().from(123, true).trim()
11+
dayjs().from('2018-01-23').trim()
12+
dayjs().from(new Date(), true).trim()
13+
14+
dayjs().toNow().trim()
15+
dayjs().toNow(true).trim()
16+
17+
dayjs().to(dayjs()).trim()
18+
dayjs().to(123, true).trim()
19+
dayjs().to('2018-01-23').trim()
20+
dayjs().to(new Date(), true).trim()

test/plugin/weekOfYear.d.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as dayjs from 'dayjs'
2+
import * as weekOfYear from 'dayjs/plugin/weekOfYear'
3+
4+
dayjs.extend(weekOfYear)
5+
6+
dayjs('2010-10-20').week() === 43

tsconfig.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"noEmit": true,
5+
"lib": [
6+
// TODO: Unfortunately, rollup brings @types/node and etc. with "dependencies", not "devDependencies".
7+
// It references Symbol type.
8+
"es5",
9+
"es2015.symbol"
10+
],
11+
"baseUrl": "./types",
12+
"paths": {
13+
"dayjs": [ "." ],
14+
"dayjs/*": [ "*" ]
15+
}
16+
},
17+
"exclude": [
18+
"node_modules"
19+
]
20+
}

index.d.ts types/index.d.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
export = dayjs;
2-
declare function dayjs (config?: dayjs.ConfigType, option?: dayjs.OptionType): dayjs.Dayjs
2+
declare function dayjs (date?: dayjs.DateType, option?: dayjs.OptionType): dayjs.Dayjs
33

44
declare namespace dayjs {
5-
export type ConfigType = string | number | Date | Dayjs
5+
export type DateType = string | number | Date | Dayjs
66

7-
export type OptionType = { locale: string }
7+
/** @deprecated Renamed to DateType. */
8+
export type ConfigType = DateType
9+
10+
export type OptionType = { locale?: string, format?: string } | string
811

912
type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms'
1013
export type UnitType = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'quarter' | 'year' | 'date' | UnitTypeShort;
@@ -23,7 +26,7 @@ declare namespace dayjs {
2326
}
2427

2528
class Dayjs {
26-
constructor (config?: ConfigType)
29+
constructor (date?: DateType)
2730

2831
clone(): Dayjs
2932

@@ -57,7 +60,7 @@ declare namespace dayjs {
5760

5861
format(template?: string): string
5962

60-
diff(dayjs: ConfigType, unit: OpUnitType, float?: boolean): number
63+
diff(date: DateType, unit: OpUnitType, float?: boolean): number
6164

6265
valueOf(): number
6366

@@ -77,24 +80,24 @@ declare namespace dayjs {
7780

7881
toString(): string
7982

80-
isBefore(dayjs: ConfigType, unit?: OpUnitType): boolean
83+
isBefore(date: DateType, unit?: OpUnitType): boolean
8184

82-
isSame(dayjs: ConfigType, unit?: OpUnitType): boolean
85+
isSame(date: DateType, unit?: OpUnitType): boolean
8386

84-
isAfter(dayjs: ConfigType, unit?: OpUnitType): boolean
87+
isAfter(date: DateType, unit?: OpUnitType): boolean
8588

8689
isLeapYear(): boolean
8790

88-
locale(arg1: any, arg2?: any): Dayjs
91+
locale(preset: string | { name: string, [key: string]: any }, object?: { [key: string]: any }): Dayjs
8992
}
9093

91-
export type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void
94+
export type PluginFunc = (option: any, c: typeof Dayjs, d: typeof dayjs) => void
9295

93-
export function extend(plugin: PluginFunc, option?: ConfigType): Dayjs
96+
export function extend(plugin: PluginFunc, option?: any): Dayjs
9497

95-
export function locale(arg1: any, arg2?: any): string
98+
export function locale(preset: string | { name: string, [key: string]: any }, object?: { [key: string]: any }, isLocal?: boolean): string
9699

97100
export function isDayjs(d: any): d is Dayjs
98-
101+
99102
export function unix(t: number): Dayjs
100103
}

types/plugin/advancedFormat.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin

types/plugin/buddhistEra.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin

types/plugin/customParseFormat.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin

types/plugin/dayOfYear.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin
5+
6+
declare module 'dayjs' {
7+
interface Dayjs {
8+
dayOfYear(): number
9+
}
10+
}

types/plugin/isBetween.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PluginFunc, DateType } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin
5+
6+
declare module 'dayjs' {
7+
interface Dayjs {
8+
isBetween(a: DateType, b: DateType): boolean
9+
}
10+
}

types/plugin/isLeapYear.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin
5+
6+
declare module 'dayjs' {
7+
interface Dayjs {
8+
isLeapYear(): boolean
9+
}
10+
}

types/plugin/isSameOrAfter.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin
5+
6+
declare module 'dayjs' {
7+
interface Dayjs {
8+
isSameOrAfter(date: DateType, unit?: OpUnitType): boolean
9+
}
10+
}

types/plugin/isSameOrBefore.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin
5+
6+
declare module 'dayjs' {
7+
interface Dayjs {
8+
isSameOrBefore(date: DateType, unit?: OpUnitType): boolean
9+
}
10+
}

types/plugin/localizedFormat.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin

types/plugin/quarterOfYear.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin
5+
6+
declare module 'dayjs' {
7+
interface Dayjs {
8+
quarter(): number
9+
}
10+
}

types/plugin/relativeTime.d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { PluginFunc, DateType } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin
5+
6+
declare module 'dayjs' {
7+
interface Dayjs {
8+
fromNow(withoutSuffix?: boolean): string
9+
from(compared: DateType, withoutSuffix?: boolean): string
10+
toNow(withoutSuffix?: boolean): string
11+
to(compared: DateType, withoutSuffix?: boolean): string
12+
}
13+
}

types/plugin/weekOfYear.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin
5+
6+
declare module 'dayjs' {
7+
interface Dayjs {
8+
week(): number
9+
}
10+
}

0 commit comments

Comments
 (0)