Skip to content

Commit

Permalink
fix: Add type declarations for TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Oct 6, 2018
1 parent 89d793e commit 3b58480
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage
dist
node_modules
test/typings.test.js
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"search.exclude": {
"**/src/lookup/data.js": true,
"**/dist/**": true,
"**/test/browser/**": true
"**/test/browser/**": true,
"**/test/typings.test.js": true
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.

## Release History

* 2018-10-06 v0.1.2 Add TypeScript export declarations.
* 2018-09-19 v0.1.0 Add parseString without a time zone to cover a gap in date-fns
* 2018-09-17 v0.0.1 Initial release

Expand Down
4 changes: 2 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ Load the main module in the browser with [RequireJS]:
```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.29.0/date_fns.min.js"></script>
<script src="https://unpkg.com/timezone-support@1.3.2/dist/index.umd.js"></script>
<script src="https://unpkg.com/date-fns-timezone@0.1.0/dist/index.umd.js"></script>
<script src="https://unpkg.com/timezone-support@1.5.1/dist/index.umd.js"></script>
<script src="https://unpkg.com/date-fns-timezone@0.1.2/dist/index.umd.js"></script>
<script>
require(['date-fns-timezone'], ({ parseFromTimeZone, formatToTimeZone }) => {
})
Expand Down
44 changes: 44 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
type DateInput = string | number | Date
interface TimeZoneOptions {
timeZone: string
}

declare function convertToLocalTime (dateInput: DateInput, options: TimeZoneOptions): Date
declare function convertToTimeZone (dateInput: DateInput, options: TimeZoneOptions): Date

declare function parseFromString (dateString: string, format: string): Date
declare function parseFromTimeZone (dateString: string, format: string | TimeZoneOptions, options?: TimeZoneOptions): Date

declare function formatToTimeZone (dateInput: DateInput, format: string, options: TimeZoneOptions): string

export {
convertToLocalTime, convertToTimeZone, parseFromString, parseFromTimeZone, formatToTimeZone
}

declare module 'date-fns-timezone' {
export {
convertToLocalTime, convertToTimeZone, parseFromString, parseFromTimeZone, formatToTimeZone
}
}

declare module 'date-fns-timezone/dist/convertToLocalTime' {
export { convertToLocalTime }
}

declare module 'date-fns-timezone/dist/convertToTimeZone' {
export { convertToTimeZone }
}

declare module 'date-fns-timezone/dist/parseFromString' {
export { parseFromString }
}

declare module 'date-fns-timezone/dist/parseFromTimeZone' {
export { parseFromTimeZone }
}

declare module 'date-fns-timezone/dist/formatToTimeZone' {
export { formatToTimeZone }
}

export as namespace dateFnsTimezone;
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
"lint": "standard --verbose",
"compile": "rollup -c",
"build": "run-s lint compile",
"compile:tests": "tsc --lib es6 test/typings.test.ts",
"check": "jest",
"test": "run-s lint check",
"test": "run-s lint compile:tests check",
"doc": "jsdoc -c .jsdoc.json",
"coverage": "test `node --version | cut -c 2` -eq 8 && cat coverage/lcov.info | coveralls",
"benchmark": "node perf",
Expand Down Expand Up @@ -104,7 +105,8 @@
"serve-static": "^1.13.2",
"standard": "^12.0.1",
"tiny-glob": "^0.2.2",
"travis-deploy-once": "^5.0.8"
"travis-deploy-once": "^5.0.8",
"typescript": "^3.1.1"
},
"keywords": [
"date-fns",
Expand Down
31 changes: 31 additions & 0 deletions test/typings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
convertToLocalTime, convertToTimeZone, parseFromString, parseFromTimeZone, formatToTimeZone
} from '..'

declare function test(label: string, callback: Function)

test('TypeScript Type Information', () => {
const timestamp = 1538822326765
const date = new Date(timestamp)
const dateString = '2018-10-06T10:38:46.765Z'
const customDateString = '6.10.2018 12:38:46.765'
const customFormat = 'D.M.YYYY H:mm:ss.SSS'
const timeZoneOptions = { timeZone: 'Europe/Berlin' }

convertToLocalTime(date, timeZoneOptions)
convertToLocalTime(dateString, timeZoneOptions)
convertToLocalTime(timestamp, timeZoneOptions)

convertToTimeZone(date, timeZoneOptions)
convertToTimeZone(dateString, timeZoneOptions)
convertToTimeZone(timestamp, timeZoneOptions)

parseFromString(customDateString, customFormat)

parseFromTimeZone(customDateString, customFormat, timeZoneOptions)
parseFromTimeZone(dateString, timeZoneOptions)

formatToTimeZone(date, customFormat, timeZoneOptions)
formatToTimeZone(dateString, customFormat, timeZoneOptions)
formatToTimeZone(timestamp, customFormat, timeZoneOptions)
})

0 comments on commit 3b58480

Please sign in to comment.