Skip to content

Commit 943623e

Browse files
committed
fix: Use valid JavaScript identifiers for exports in vanilla browser modules
BREAKING CHANGE: Previous kebab-case keys in the `window` object changed to camelCase global vbariables. Usage of shims or tools loading UMD modules becomes easier. See the migration guide for more information.
1 parent 36491fb commit 943623e

9 files changed

Lines changed: 120 additions & 37 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Lightweight time zone listing and date converting. Intended for adding time zone
1717
* Generated from the official time zone database version 2018g. Canonical time zone names, aliases, UTC offsets, and daylight-saving time changes.
1818
* Minimal interface for time zone lookup and conversions. Parsing, formatting and manipulating dates is usually the task for a higher-level date library.
1919

20+
**Attention**: export identifiers in vanilla browser modules changed in the version 2.0.0. See the [migration guide] for more information.
21+
2022
### Table of Contents
2123

2224
- [Synopsis](#synopsis)
@@ -83,6 +85,7 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.
8385

8486
## Release History
8587

88+
* 2018-06-10 v2.0.0 Use proper identifiers in vanilla browser modules.
8689
* 2018-11-17 v1.8.0 Include time zone data for years 1970-2038.
8790
* 2018-11-17 v1.7.0 Include full time zone data separately loadable.
8891
* 2018-11-06 v1.6.0 Upgrade the time zone database to the version 2018g.
@@ -96,7 +99,7 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.
9699

97100
## License
98101

99-
Copyright (c) 2018 Ferdinand Prantl
102+
Copyright (c) 2018-2019 Ferdinand Prantl
100103

101104
Licensed under the MIT license.
102105

@@ -107,3 +110,4 @@ Licensed under the MIT license.
107110
[date-fns]: https://github.com/date-fns/date-fns
108111
[timeZone plugin]: https://github.com/prantlf/dayjs/blob/combined/docs/en/Plugin.md#timezone
109112
[date-fns-timezone]: https://github.com/prantlf/date-fns-timezone
113+
[migration guide]: docs/migration.md

bin/create-timezone-data

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ commander.version(pkg.version)
1313
.option('-c, --as-cjs-module', 'format the time zone data as a CommonJS module')
1414
.option('-d, --as-amd-module', 'format the time zone data as an AMD module')
1515
.option('-m, --as-module', 'format the time zone data as a JavaScript module')
16-
.option('-n, --umd-name <name>', 'UMD global export name, if not "timeZoneData"')
16+
.option('-n, --umd-name <name>', 'UMD global export name, if not "timezoneData"')
1717
.option('-o, --output-file <file>', 'write the time zone data to a file')
1818
.option('-u, --as-umd-module', 'format the time zone data as an UMD module')
1919
.option('-o, --output-file <file>', 'write the time zone data to a file')

docs/API.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ Load the main module in the browser with plain JavaScript:
4545
<script src="./node_modules/timezone-support/dist/index.umd.js"></script>
4646
<script>
4747
(() => {
48-
const { findTimeZone, getZonedTime } = window['timezone-support']
48+
const { findTimeZone, getZonedTime } = window.timezoneSupport
4949
})()
5050
</script>
5151
```
5252

53-
You can also load a specific version from CDN, for example: https://unpkg.com/timezone-support@1.8.0/dist/index.umd.js.
53+
You can also load a specific version from CDN, for example: https://unpkg.com/timezone-support@2.0.0/dist/index.umd.js.
5454

5555
## Modules
5656

@@ -446,7 +446,7 @@ Options:
446446
-c, --as-cjs-module format the time zone data as a CommonJS module
447447
-d, --as-amd-module format the time zone data as an AMD module
448448
-m, --as-module format the time zone data as a JavaScript module
449-
-n, --umd-name <name> UMD global export name, if not "timeZoneData"
449+
-n, --umd-name <name> UMD global export name, if not "timezoneData"
450450
-o, --output-file <file> write the time zone data to a file
451451
-u, --as-umd-module format the time zone data as an UMD module
452452
-o, --output-file <file> write the time zone data to a file

docs/design.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Design Concepts
22

3-
The purpose of this library is to offer an efficient support for time zone handling in th esmallest package.
3+
The purpose of this library is to offer an efficient support for time zone handling in the smallest package.
44

55
* Lightweight - nothing else is included. Thus serving well for other date & time libraries, but also for applications, which do not manipulate dates.
66
* Tiny - use packed time zone data, unpacked on demand. Compromise between loading time and being ready to use immediately.

docs/migration.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Migration from 1.x to 2.x
2+
3+
Names of global variables that expose the exported objects in vanilla JavaScript modules changed. Basically, convert the kebab-case keys exposing the global objects to camelCase. For example: `window['timezone-support']` to `window.timezoneSupport`.
4+
5+
Nothing else changed. File names of scripts in the `dist` directory did not change. The structure of objects exported from these files did not change either. The behaviour of exposed functions did not change.
6+
7+
## Renamed Variables
8+
9+
Names of global variables that expose the exported objects in vanilla JavaScript modules changed. They do not contain hyphens any more and they became valid JavaScript identifiers. It was necessary to satisfy tools, which integrate UMD modules via shims.
10+
11+
| File name | Old object | New object |
12+
| :-------------- | :-------------------------------- | :------------------------- |
13+
| index | window['timezone-support'] | window.timezoneSupport |
14+
| index-1900-2050 | window['timezone-support'] | window.timezoneSupport |
15+
| index-1970-2038 | window['timezone-support'] | window.timezoneSupport |
16+
| index-2012-2022 | window['timezone-support'] | window.timezoneSupport |
17+
| lookup-convert | window['timezone-lookup-convert'] | window.timezoneSupport |
18+
| data | window['timezone-data'] | window.timezoneData |
19+
| data-1900-2050 | window['timezone-data-1900-2050'] | window.timezoneData |
20+
| data-1970-2038 | window['timezone-data-1970-2038'] | window.timezoneData |
21+
| data-2012-2022 | window['timezone-data-2012-2022'] | window.timezoneData |
22+
| parse-format | window['timezone-parse-format'] | window.timezoneParseFormat |
23+
24+
The `lookup-convert` file is meant to be used together with a `data*` file. The name of the object-exposing variable (`timezoneSupport`) can be the same as the one exposed from `index*` files. It allows for easier dependency modifications without additional code changes in dependency sources.
25+
26+
The `data*` files are not meant to be used together; just one of them is supposed to be used with the `lookup-convert` file. The exposing variable name can be the same (`timezoneData`). It allows for easier swapping of data-files when having the exposing variable called always `timezoneData`.
27+
28+
## Change Examples
29+
30+
Replace keys that you used to consume objects exported from files in the `dist` directory:
31+
32+
```js
33+
const { populateTimeZones, findTimeZone, getZonedTime } = window['timezone-lookup-convert']
34+
populateTimeZones(window['timezone-data-1970-2038'])
35+
36+
const berlin = findTimeZone('Europe/Berlin')
37+
const inputDate = new Date('2018-09-01T09:19:17.276Z')
38+
const berlinTime = getZonedTime(inputDate, berlin)
39+
```
40+
41+
with the new keys:
42+
43+
```js
44+
const { populateTimeZones, findTimeZone, getZonedTime } = window.timezoneSupport
45+
populateTimeZones(window.timezoneData)
46+
// The rest of API remains the same.
47+
const berlin = findTimeZone('Europe/Berlin')
48+
const inputDate = new Date('2018-09-01T09:19:17.276Z')
49+
const berlinTime = getZonedTime(inputDate, berlin)
50+
```
51+
52+
And other old keys, for example:
53+
54+
```js
55+
const { findTimeZone, getZonedTime } = window['timezone-support']
56+
const { formatZonedTime } = window['timezone-parse-format']
57+
58+
const zonedTime = { year: 2018, month: 9, day: 2, hours: 10, minutes: 0,
59+
zone: { abbreviation: 'CEST', offset: -120 } }
60+
const displayTime = formatZonedTime(zonedTime, 'D.M.YYYY H:mm zZ')
61+
```
62+
63+
with the new keys:
64+
65+
```js
66+
const { findTimeZone, getZonedTime } = window.timezoneSupport
67+
const { formatZonedTime } = window.timezoneParseFormat
68+
// The rest of API remains the same.
69+
const zonedTime = { year: 2018, month: 9, day: 2, hours: 10, minutes: 0,
70+
zone: { abbreviation: 'CEST', offset: -120 } }
71+
const displayTime = formatZonedTime(zonedTime, 'D.M.YYYY H:mm zZ')
72+
```

docs/usage.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const { formatZonedTime } = require('timezone-support/dist/parse-format')
8484

8585
const zonedTime = { year: 2018, month: 9, day: 2, hours: 10, minutes: 0,
8686
zone: { abbreviation: 'CEST', offset: -120 } }
87-
const displayTime = formatZonedTime(zonedTime, 'D.M YYYY H:mm:ss Z')
87+
const displayTime = formatZonedTime(zonedTime, 'D.M.YYYY H:mm:ss zZ')
8888
```
8989

9090
See the function [formatZonedTime](./API.md#formatzonedtime) for more information.
@@ -96,8 +96,8 @@ Some applications let their users freely configure, what date format will be use
9696
```js
9797
const { parseZonedTime } = require('timezone-support/dist/parse-format')
9898

99-
const displayTime = '2.9. 2018 10:00 CET+02:00'
100-
const zonedTime = parseZonedTime(displayTime, 'D.M YYYY H:mm:ss Z')
99+
const displayTime = '2.9.2018 10:00 CET+02:00'
100+
const zonedTime = parseZonedTime(displayTime, 'D.M.YYYY H:mm zZ')
101101
```
102102

103103
See the function [parseZonedTime](./API.md#parsezonedtime) for more information.
@@ -183,17 +183,17 @@ Data for 2012-2022: 27 KB minified, 6.5 KB gzipped
183183
Custom time zone data can be used if the module `lookup-convert` is loaded instead of the default `index` module.
184184

185185
```html
186-
<script src="https://unpkg.com/timezone-support@1.8.0/dist/lookup-convert.umd.js"></script>
187-
<script src="https://unpkg.com/timezone-support@1.8.0/dist/data-2012-2022.umd.js"></script>
186+
<script src="https://unpkg.com/timezone-support@2.0.0/dist/lookup-convert.umd.js"></script>
187+
<script src="https://unpkg.com/timezone-support@2.0.0/dist/data-2012-2022.umd.js"></script>
188188
<script>
189189
(() => {
190-
const { populateTimeZones, findTimeZone, getZonedTime } = window['timezone-lookup-convert']
191-
const data = window['timezone-data-2012-2022']
190+
const { populateTimeZones, findTimeZone, getZonedTime } = window.timezoneSupport
191+
const data = window.timezoneData']
192192
193193
populateTimeZones(data)
194194
195195
const berlin = findTimeZone('Europe/Berlin')
196-
const isoString = '2018-09-09:19:17.276Z'
196+
const isoString = '2018-09-01T09:19:17.276Z'
197197
const berlinTime = getZonedTime(new Date(isoString), berlin)
198198
})()
199199
</script>
@@ -202,13 +202,13 @@ Custom time zone data can be used if the module `lookup-convert` is loaded inste
202202
If you want to use the time zone data for years 2012-2022 published by this project, you can simplify your code by using a bundled package with both data and code.
203203
204204
```html
205-
<script src="https://unpkg.com/timezone-support@1.8.0/dist/index-2012-2022.umd.js"></script>
205+
<script src="https://unpkg.com/timezone-support@2.0.0/dist/index-2012-2022.umd.js"></script>
206206
<script>
207207
(() => {
208-
const { findTimeZone, getZonedTime } = window['timezone-support']
208+
const { findTimeZone, getZonedTime } = window.timezoneSupport
209209
210210
const berlin = findTimeZone('Europe/Berlin')
211-
const isoString = '2018-09-09:19:17.276Z'
211+
const isoString = '2018-09-01T09:19:17.276Z'
212212
const berlinTime = getZonedTime(new Date(isoString), berlin)
213213
})()
214214
</script>
@@ -238,7 +238,7 @@ See the function [populateTimeZones](./API.md#populatetimezones) for more inform
238238
239239
Except for the time zone data for the three year spans bundled with this module, other data modules can be generated to customize the year span and thus the overall package size. There is a command line tool [`create-timezone-data`](./API.md#data-generator) for this included in this package.
240240
241-
For example, you can generate time zone data for years 1978-2028 and save it to the module `data-1978-2028.js` in the CommonJS format, which you wil bundle to your application:
241+
For example, you can generate time zone data for years 1978-2028 and save it to the module `data-1978-2028.js` in the CommonJS format, which you will bundle to your application:
242242
243243
```sh
244244
create-timezone-data -c -o custom-data-1978-2028.js 1978 2028
@@ -253,10 +253,10 @@ const data = require('./data-1978-2028')
253253
populateTimeZones(data)
254254
```
255255
256-
Let us have alook, how the same would work in an application loading their assets directly in the browser. You would generate a UMD module instead:
256+
Let us have a look, how the same would work in an application loading their assets directly in the browser. You would generate a UMD module instead:
257257
258258
```sh
259-
create-timezone-data -u -o custom-data-1978-2028.js -n timeZoneData10782028 1978 2028
259+
create-timezone-data -u -o custom-data-1978-2028.js -n timezoneData10782028 1978 2028
260260
```
261261
262262
And then load them at the beginning of a plain JavaScript application:
@@ -266,9 +266,9 @@ And then load them at the beginning of a plain JavaScript application:
266266
<script src="./data-1978-2028.js"></script>
267267
<script>
268268
(() => {
269-
const { populateTimeZones } = window['timezone-lookup-convert']
269+
const { populateTimeZones } = window.timezoneSupport
270270
271-
populateTimeZones(window.timeZoneData10782028)
271+
populateTimeZones(window.timezoneData10782028)
272272
})()
273273
</script>
274274
```
@@ -280,7 +280,9 @@ Or load it in an application using AMD modules:
280280
<script src="https://unpkg.com/timezone-support/dist/lookup-convert.umd.js"></script>
281281
<script src="./data-1978-2028.js"></script>
282282
<script>
283-
require(['timezone-lookup-convert', 'timeZoneData10782028'], function (tz, data) {
283+
require([
284+
'timezone-lookup-convert', 'timezone-data-1078-2028'
285+
], function (tz, data) {
284286
tz.populateTimeZones(data)
285287
})()
286288
</script>

rollup.config.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export default [
118118
output: {
119119
file: 'dist/index.umd.js',
120120
format: 'umd',
121-
name: 'timezone-support',
121+
name: 'timezoneSupport',
122122
sourcemap: true
123123
},
124124
plugins: [
@@ -131,7 +131,7 @@ export default [
131131
output: {
132132
file: 'dist/index-1970-2038.umd.js',
133133
format: 'umd',
134-
name: 'timezone-support',
134+
name: 'timezoneSupport',
135135
sourcemap: true
136136
},
137137
plugins: [
@@ -144,7 +144,7 @@ export default [
144144
output: {
145145
file: 'dist/index-2012-2022.umd.js',
146146
format: 'umd',
147-
name: 'timezone-support',
147+
name: 'timezoneSupport',
148148
sourcemap: true
149149
},
150150
plugins: [
@@ -157,7 +157,7 @@ export default [
157157
output: {
158158
file: 'dist/index-1900-2050.umd.js',
159159
format: 'umd',
160-
name: 'timezone-support',
160+
name: 'timezoneSupport',
161161
sourcemap: true
162162
},
163163
plugins: [
@@ -170,7 +170,7 @@ export default [
170170
output: {
171171
file: 'dist/lookup-convert.umd.js',
172172
format: 'umd',
173-
name: 'timezone-lookup-convert',
173+
name: 'timezoneSupport',
174174
sourcemap: true
175175
},
176176
plugins: [
@@ -183,7 +183,7 @@ export default [
183183
output: {
184184
file: 'dist/data.umd.js',
185185
format: 'umd',
186-
name: 'timezone-data',
186+
name: 'timezoneData',
187187
sourcemap: true
188188
},
189189
plugins: [
@@ -196,7 +196,7 @@ export default [
196196
output: {
197197
file: 'dist/data-1970-2038.umd.js',
198198
format: 'umd',
199-
name: 'timezone-data-1970-2038',
199+
name: 'timezoneData',
200200
sourcemap: true
201201
},
202202
plugins: [
@@ -209,7 +209,7 @@ export default [
209209
output: {
210210
file: 'dist/data-2012-2022.umd.js',
211211
format: 'umd',
212-
name: 'timezone-data-2012-2022',
212+
name: 'timezoneData',
213213
sourcemap: true
214214
},
215215
plugins: [
@@ -222,7 +222,7 @@ export default [
222222
output: {
223223
file: 'dist/data-1900-2050.umd.js',
224224
format: 'umd',
225-
name: 'timezone-data-1900-2050',
225+
name: 'timezoneData',
226226
sourcemap: true
227227
},
228228
plugins: [
@@ -235,7 +235,7 @@ export default [
235235
output: {
236236
file: 'dist/parse-format.umd.js',
237237
format: 'umd',
238-
name: 'timezone-parse-format',
238+
name: 'timezoneParseFormat',
239239
sourcemap: true
240240
},
241241
plugins: [

util/data-creator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function formatUMDModule (content, umdName) {
2828
return `(function (global, factory) {
2929
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3030
typeof define === 'function' && define.amd ? define(['exports'], factory) :
31-
factory(global.${umdName || 'timeZoneData'} = {})
31+
factory(global.${umdName || 'timezoneData'} = {})
3232
} (this, (function (exports) {
3333
Object.assign(exports, ${content})
3434
Object.defineProperty(exports, '__esModule', { value: true })

util/generate-browser-tests.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ function formatFunctionImport (input) {
3232
throw new Error('Statement requiring the code module not found.')
3333
}
3434
const name = match[2]
35-
const variable = name.startsWith('index') ? 'support' : name
35+
let variable
36+
if (name.startsWith('parse-format')) {
37+
variable = 'ParseFormat'
38+
} else {
39+
variable = 'Support'
40+
}
3641
const functionCodeLine = input.replace(importFunctionsExpression,
37-
`const $1 = window['timezone-${variable}']`)
42+
`const $1 = window.timezone${variable}`)
3843
const functionScriptElement = [
3944
'<script src="../../dist/' + name + '.umd.js"></script>'
4045
]
@@ -48,7 +53,7 @@ function formatDataImport (input) {
4853
}
4954
const name = match[2]
5055
const dataCodeLine = input.replace(importDataExpression,
51-
`const $1 = window['timezone-${name}']`)
56+
'const $1 = window.timezoneData')
5257
const dataScriptElement = [
5358
'<script src="../../dist/' + name + '.umd.js"></script>'
5459
]

0 commit comments

Comments
 (0)