nhb-toolbox 4.26.45 → 4.26.46-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,25 @@ const timeZonePlugin = (ChronosClass) => {
10
10
  const _isGMT = (factor) => {
11
11
  return factor === 'UTC+00:00' || factor === 'UTC-00:00';
12
12
  };
13
+ const _getTimeZoneDetails = (tzId, date) => {
14
+ const TZ_NAME_TYPES = ['long', 'longOffset'];
15
+ const obj = { tzId };
16
+ for (const type of TZ_NAME_TYPES) {
17
+ const parts = new Intl.DateTimeFormat('en', {
18
+ timeZone: tzId,
19
+ timeZoneName: type,
20
+ }).formatToParts(date);
21
+ const tzPart = parts.find((p) => p.type === 'timeZoneName');
22
+ const key = type === 'long' ? 'tzName' : 'offset';
23
+ const value = type === 'longOffset' ?
24
+ tzPart?.value === 'GMT' ?
25
+ 'UTC+00:00'
26
+ : tzPart?.value?.replace(/^GMT/, 'UTC')
27
+ : tzPart?.value;
28
+ obj[key] = value;
29
+ }
30
+ return obj;
31
+ };
13
32
  const TZ_NAME_ABBR_MAP = new Map(Object.entries(timezone_1.TIME_ZONES).map(([tzAbbr, { offset, tzName }]) => [
14
33
  offset,
15
34
  { tzAbbr, tzName },
@@ -17,6 +36,9 @@ const timeZonePlugin = (ChronosClass) => {
17
36
  const _isLabelKey = (offset) => {
18
37
  return offset in timezone_1.TIME_ZONE_LABELS;
19
38
  };
39
+ const _isValidTzAbbr = (tz) => {
40
+ return tz in timezone_1.TIME_ZONES;
41
+ };
20
42
  const _resolveTzName = (offset) => {
21
43
  if (_isLabelKey(offset)) {
22
44
  return timezone_1.TIME_ZONE_LABELS[offset];
@@ -33,21 +55,16 @@ const timeZonePlugin = (ChronosClass) => {
33
55
  }
34
56
  return tzName;
35
57
  }
36
- else if ((0, guards_1.isValidTimeZoneId)(zone)) {
37
- const { offset, tzName } = timezone_1.TIME_ZONE_IDS[zone];
38
- const { tzNameLongOffset, tzNameLong } = (0, utils_1.getTimeZoneDetails)(zone, date);
39
- const convertedOffset = tzNameLongOffset?.replace(/^GMT/, 'UTC') ?? '';
40
- const resolvedName = _resolveTzName(offset);
41
- const priorityName = convertedOffset === offset ? tzNameLong : resolvedName;
42
- return tzName || priorityName || resolvedName || tzNameLong;
58
+ else if (_isValidTzAbbr(zone)) {
59
+ return timezone_1.TIME_ZONES[zone].tzName;
43
60
  }
44
61
  else {
45
- return zone in timezone_1.TIME_ZONES ?
46
- timezone_1.TIME_ZONES[zone].tzName
47
- : _resolveTzName(timezone_1.TIME_ZONES[zone]?.offset);
62
+ const { offset, tzName } = _getTimeZoneDetails(zone, date);
63
+ return tzName || _resolveTzName(offset);
48
64
  }
49
65
  };
50
- const TZ_ID_MAP = new Map(Object.entries(timezone_1.TIME_ZONE_IDS).reduce((acc, [id, { offset }]) => {
66
+ const TZ_ID_MAP = new Map(Intl.supportedValuesOf('timeZone').reduce((acc, id) => {
67
+ const { offset } = _getTimeZoneDetails(id);
51
68
  const arr = acc.get(offset) ?? [];
52
69
  arr.push(id);
53
70
  acc.set(offset, arr);
@@ -68,13 +85,13 @@ const timeZonePlugin = (ChronosClass) => {
68
85
  offset = zone;
69
86
  tzId = _getTimeZoneId(offset) || offset;
70
87
  }
71
- else if ((0, guards_1.isValidTimeZoneId)(zone)) {
72
- offset = timezone_1.TIME_ZONE_IDS[zone].offset;
73
- tzId = zone;
88
+ else if (_isValidTzAbbr(zone)) {
89
+ offset = timezone_1.TIME_ZONES[zone].offset;
90
+ tzId = _getTimeZoneId(offset) || offset;
74
91
  }
75
92
  else {
76
- offset = zone in timezone_1.TIME_ZONES ? timezone_1.TIME_ZONES[zone].offset : timezone_1.TIME_ZONES['UTC'].offset;
77
- tzId = _getTimeZoneId(offset) || offset;
93
+ offset = _getTimeZoneDetails(zone, $Date(this)).offset;
94
+ tzId = zone;
78
95
  }
79
96
  const $zone = zone || offset;
80
97
  const tzName = _getTimeZoneName($zone, $Date(this)) ?? offset;
@@ -121,7 +138,7 @@ const timeZonePlugin = (ChronosClass) => {
121
138
  const zone = _getTimeZoneName(tzMapKey, $Date(this)) ?? UTC;
122
139
  if (TZ_ABBR_CACHE.has(`name-${zone}`))
123
140
  return TZ_ABBR_CACHE.get(zone);
124
- const customAbbr = (0, guards_1.isValidUTCOffset)(zone) || (0, guards_1.isValidTimeZoneId)(zone) ? zone : _abbreviate(zone);
141
+ const customAbbr = (0, guards_1.isValidUTCOffset)(zone) ? zone : _abbreviate(zone);
125
142
  TZ_ABBR_CACHE.set(`name-${zone}`, customAbbr);
126
143
  return customAbbr;
127
144
  };
@@ -1,12 +1,31 @@
1
1
  import { INTERNALS } from '../constants.js';
2
- import { isValidTimeZoneId, isValidUTCOffset } from '../guards.js';
3
- import { TIME_ZONES, TIME_ZONE_IDS, TIME_ZONE_LABELS } from '../timezone.js';
4
- import { getTimeZoneDetails, extractMinutesFromUTC } from '../utils.js';
2
+ import { isValidUTCOffset } from '../guards.js';
3
+ import { TIME_ZONES, TIME_ZONE_LABELS } from '../timezone.js';
4
+ import { extractMinutesFromUTC } from '../utils.js';
5
5
  export const timeZonePlugin = (ChronosClass) => {
6
6
  const { internalDate: $Date, withOrigin } = ChronosClass[INTERNALS];
7
7
  const _isGMT = (factor) => {
8
8
  return factor === 'UTC+00:00' || factor === 'UTC-00:00';
9
9
  };
10
+ const _getTimeZoneDetails = (tzId, date) => {
11
+ const TZ_NAME_TYPES = ['long', 'longOffset'];
12
+ const obj = { tzId };
13
+ for (const type of TZ_NAME_TYPES) {
14
+ const parts = new Intl.DateTimeFormat('en', {
15
+ timeZone: tzId,
16
+ timeZoneName: type,
17
+ }).formatToParts(date);
18
+ const tzPart = parts.find((p) => p.type === 'timeZoneName');
19
+ const key = type === 'long' ? 'tzName' : 'offset';
20
+ const value = type === 'longOffset' ?
21
+ tzPart?.value === 'GMT' ?
22
+ 'UTC+00:00'
23
+ : tzPart?.value?.replace(/^GMT/, 'UTC')
24
+ : tzPart?.value;
25
+ obj[key] = value;
26
+ }
27
+ return obj;
28
+ };
10
29
  const TZ_NAME_ABBR_MAP = new Map(Object.entries(TIME_ZONES).map(([tzAbbr, { offset, tzName }]) => [
11
30
  offset,
12
31
  { tzAbbr, tzName },
@@ -14,6 +33,9 @@ export const timeZonePlugin = (ChronosClass) => {
14
33
  const _isLabelKey = (offset) => {
15
34
  return offset in TIME_ZONE_LABELS;
16
35
  };
36
+ const _isValidTzAbbr = (tz) => {
37
+ return tz in TIME_ZONES;
38
+ };
17
39
  const _resolveTzName = (offset) => {
18
40
  if (_isLabelKey(offset)) {
19
41
  return TIME_ZONE_LABELS[offset];
@@ -30,21 +52,16 @@ export const timeZonePlugin = (ChronosClass) => {
30
52
  }
31
53
  return tzName;
32
54
  }
33
- else if (isValidTimeZoneId(zone)) {
34
- const { offset, tzName } = TIME_ZONE_IDS[zone];
35
- const { tzNameLongOffset, tzNameLong } = getTimeZoneDetails(zone, date);
36
- const convertedOffset = tzNameLongOffset?.replace(/^GMT/, 'UTC') ?? '';
37
- const resolvedName = _resolveTzName(offset);
38
- const priorityName = convertedOffset === offset ? tzNameLong : resolvedName;
39
- return tzName || priorityName || resolvedName || tzNameLong;
55
+ else if (_isValidTzAbbr(zone)) {
56
+ return TIME_ZONES[zone].tzName;
40
57
  }
41
58
  else {
42
- return zone in TIME_ZONES ?
43
- TIME_ZONES[zone].tzName
44
- : _resolveTzName(TIME_ZONES[zone]?.offset);
59
+ const { offset, tzName } = _getTimeZoneDetails(zone, date);
60
+ return tzName || _resolveTzName(offset);
45
61
  }
46
62
  };
47
- const TZ_ID_MAP = new Map(Object.entries(TIME_ZONE_IDS).reduce((acc, [id, { offset }]) => {
63
+ const TZ_ID_MAP = new Map(Intl.supportedValuesOf('timeZone').reduce((acc, id) => {
64
+ const { offset } = _getTimeZoneDetails(id);
48
65
  const arr = acc.get(offset) ?? [];
49
66
  arr.push(id);
50
67
  acc.set(offset, arr);
@@ -65,13 +82,13 @@ export const timeZonePlugin = (ChronosClass) => {
65
82
  offset = zone;
66
83
  tzId = _getTimeZoneId(offset) || offset;
67
84
  }
68
- else if (isValidTimeZoneId(zone)) {
69
- offset = TIME_ZONE_IDS[zone].offset;
70
- tzId = zone;
85
+ else if (_isValidTzAbbr(zone)) {
86
+ offset = TIME_ZONES[zone].offset;
87
+ tzId = _getTimeZoneId(offset) || offset;
71
88
  }
72
89
  else {
73
- offset = zone in TIME_ZONES ? TIME_ZONES[zone].offset : TIME_ZONES['UTC'].offset;
74
- tzId = _getTimeZoneId(offset) || offset;
90
+ offset = _getTimeZoneDetails(zone, $Date(this)).offset;
91
+ tzId = zone;
75
92
  }
76
93
  const $zone = zone || offset;
77
94
  const tzName = _getTimeZoneName($zone, $Date(this)) ?? offset;
@@ -118,7 +135,7 @@ export const timeZonePlugin = (ChronosClass) => {
118
135
  const zone = _getTimeZoneName(tzMapKey, $Date(this)) ?? UTC;
119
136
  if (TZ_ABBR_CACHE.has(`name-${zone}`))
120
137
  return TZ_ABBR_CACHE.get(zone);
121
- const customAbbr = isValidUTCOffset(zone) || isValidTimeZoneId(zone) ? zone : _abbreviate(zone);
138
+ const customAbbr = isValidUTCOffset(zone) ? zone : _abbreviate(zone);
122
139
  TZ_ABBR_CACHE.set(`name-${zone}`, customAbbr);
123
140
  return customAbbr;
124
141
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "4.26.45",
3
+ "version": "4.26.46-beta.1",
4
4
  "description": "A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",