Skip to content

Commit

Permalink
Removed dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Blackbourn committed Jun 24, 2023
1 parent fc17c68 commit 679f231
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 75 deletions.
58 changes: 54 additions & 4 deletions manual/guide/iana-timezones.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,60 @@ const newYearsDay = tzBrussels.makeDate(2000, 0, 1).toISOString()
// returns "2000-01-01T01:00:00Z"
```

There is a utility function [[`loadTimezone`]] which wraps this up using
[dynamic imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import).
This library used to have a couple of helper functions for this.
Unfortunately the cause webpack warnings because of the dynamic import,
so they were removed. However you can add these to you projects.

The following loads the time zone names.

In typescript:

```ts
export async function loadTimezoneNames(
version: string = 'latest'
): Promise<string[]> {
const path = `@jetblack/tzdata/dist/${version}/zones.json`
const { default: names } = await import(path)
return names
}
```

In javascript:

```js
export async function loadTimezoneNames(version = 'latest') {
const path = `@jetblack/tzdata/dist/${version}/zones.json`
const { default: names } = await import(path)
return names
}
```

The following loads a timezone.

In typescript:

```ts
export async function loadTimezone(
name: string,
version: string = 'latest'
): Promise<IANATimezone> {
const path = `@jetblack/tzdata/dist/${version}/${name}.min.json`
const { default: tzData } = await import(path)
return new IANATimezone(name, tzData.map(minDataToTimezoneOffset))
}
```

In JavaScript:

```js
export async function loadTimezone(name, version = 'latest') {
const path = `@jetblack/tzdata/dist/${version}/${name}.min.json`
const { default: tzData } = await import(path)
return new IANATimezone(name, tzData.map(minDataToTimezoneOffset))
}
```

You can use these as follows.

```js
import { loadTimezone } from '@jetblack/date'
Expand All @@ -37,7 +89,6 @@ console.log(tzChicago.makeDate(2022, 12, 25).toISOString())
// 2023-01-25T06:00:00.000Z
```


## Dynamic

When the required timezones are not known at build time they may be accessed dynamically.
Expand Down Expand Up @@ -76,7 +127,6 @@ console.log(tzChicago.makeDate(2022, 12, 25).toISOString())

The list of all available zones is provided at `dist/latest/zones.json`.


## What next ?

{@page ./calendars.md}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@jetblack/date",
"private": false,
"version": "2.5.0",
"version": "2.6.0-alpha.0",
"description": "Date utilities",
"keywords": [
"date",
Expand Down
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ export {
minDataToTimezoneOffset,
timezoneFromJSON,
fetchTimezone,
fetchTimezoneNames,
loadTimezone,
loadTimezoneNames
fetchTimezoneNames
} from './tzdata'
export { tzUtc } from './UTCTimezone'
export { weekOfYear } from './weekOfYear'
Expand Down
54 changes: 0 additions & 54 deletions src/tzdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,57 +137,3 @@ export async function fetchTimezoneNames(
const names = await response.json()
return names
}

/**
* Load a timezone using dynamic import.
*
* @example
*
* ```ts
* import { loadTimezone } from '@jetblack/date'
*
* const tzChicago = await loadTimezone('America/Chicago')
* console.log(tzChicago.makeDate(2022, 12, 25).toISOString())
* // 2023-01-25T06:00:00.000Z
* ```
*
* @category Timezone
*
* @param name The name of the timezone.
* @param version The database version.
* @returns A promise resolving to the timezone.
*/
export async function loadTimezone(
name: string,
version: string = 'latest'
): Promise<IANATimezone> {
const path = `@jetblack/tzdata/dist/${version}/${name}.min.json`
const { default: tzData } = await import(path)
return new IANATimezone(name, tzData.map(minDataToTimezoneOffset))
}

/**
* Load the timezone names using dynamic import.
*
* @example
*
* ```ts
* import { loadTimezoneNames } from '@jetblack/map'
*
* const names = await loadTimezoneNames()
* console.log(names.filter(name => name.startsWith('Arctic')))
* // (1) ['Arctic/Longyearbyen']
* ```
*
* @category Timezone
*
* @param version The database version.
* @returns A promise resolving to the list of timezone names.
*/
export async function loadTimezoneNames(
version: string = 'latest'
): Promise<string[]> {
const path = `@jetblack/tzdata/dist/${version}/zones.json`
const { default: names } = await import(path)
return names
}
13 changes: 0 additions & 13 deletions test/tzdata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
IANATimezone,
dataToTimezoneOffset,
minDataToTimezoneOffset,
loadTimezone,
loadTimezoneNames,
fetchTimezone
} from '../src'
import chicagoTzData from '@jetblack/tzdata/dist/latest/America/Chicago.json'
Expand All @@ -28,17 +26,6 @@ describe('tzdata', () => {
expect(tzChicago.offset(date)).toBe(-360)
})

it('should load minified tzdata with dynamic import', async () => {
const tzChicago = await loadTimezone('America/Chicago')
const date = new Date('2000-01-01T00:00:00Z')
expect(tzChicago.offset(date)).toBe(-360)
})

it('should load timezone names with dynamic import', async () => {
const names = await loadTimezoneNames()
expect(names.length > 0).toBeTruthy()
})

//TODO: This fails because "fetch" is not in scope. Don't know why.
// it('should fetch minified tzdata with dynamic import', async () => {
// const tzChicago = await fetchTimezone('America/Chicago')
Expand Down

0 comments on commit 679f231

Please sign in to comment.