nhb-toolbox 4.30.1 → 4.30.11
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.
- package/CHANGELOG.md +9 -0
- package/dist/cjs/date/plugins/roundPlugin.js +1 -1
- package/dist/cjs/date/plugins/timeZonePlugin.js +17 -10
- package/dist/dts/utils/types.d.ts +28 -0
- package/dist/esm/date/plugins/roundPlugin.js +1 -1
- package/dist/esm/date/plugins/timeZonePlugin.js +17 -10
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,15 @@
|
|
|
4
4
|
|
|
5
5
|
All notable changes to the package will be documented here.
|
|
6
6
|
|
|
7
|
+
## [4.30.11] - 2026-05-17
|
|
8
|
+
|
|
9
|
+
- **Updated** the *time-zone* abbreviation matching logic of *Chronos plugin method* `getTimeZoneNameShort()` (alias: `getTimeZoneNameAbbr()`) and fixed the caching issue.
|
|
10
|
+
|
|
11
|
+
## [4.30.10] - 2026-05-16
|
|
12
|
+
|
|
13
|
+
- **Added** new *utility types*: `PropertyOptional<T, Keys>` and `PropertyRequired<T, Keys>`.
|
|
14
|
+
- **Fixed** *Chronos plugin method* `round()` not preserving the *metadata* of the *original* instance.
|
|
15
|
+
|
|
7
16
|
## [4.30.1] - 2026-04-27
|
|
8
17
|
|
|
9
18
|
- **Updated** *random hex generation* logic in `randomHex` and `uuid` utilities to use `crypto.getRandomValues` for better performance and security when available, with a fallback to `Math.random` for environments where `crypto` is not available.
|
|
@@ -56,7 +56,7 @@ const roundPlugin = ($Chronos) => {
|
|
|
56
56
|
const diffToStart = Math.abs(date.getTime() - startOfWeek.getTime());
|
|
57
57
|
const diffToEnd = Math.abs(endOfWeek.getTime() - date.getTime());
|
|
58
58
|
const rounded = diffToEnd < diffToStart ? endOfWeek : startOfWeek;
|
|
59
|
-
return withOrigin(new $Chronos(rounded), 'round');
|
|
59
|
+
return withOrigin(new $Chronos(rounded), 'round', offset(this), this.timeZoneName, this.timeZoneId, this.$tzTracker);
|
|
60
60
|
}
|
|
61
61
|
case 'month': {
|
|
62
62
|
const fullMonth = date.getMonth() + date.getDate() / this.lastDateOfMonth;
|
|
@@ -48,10 +48,14 @@ const timeZonePlugin = ($Chronos) => {
|
|
|
48
48
|
}
|
|
49
49
|
return tzIds;
|
|
50
50
|
}, new Map()));
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
const _buildTzNameAbbrMap = () => {
|
|
52
|
+
const result = [];
|
|
53
|
+
for (const [tzAbbr, { offset, tzName }] of Object.entries(timezone_1.TIME_ZONES)) {
|
|
54
|
+
result.unshift([offset, { tzAbbr, tzName }]);
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
};
|
|
58
|
+
const TZ_NAME_ABBR_MAP = new Map(_buildTzNameAbbrMap());
|
|
55
59
|
const _getTimeZoneId = (utc) => {
|
|
56
60
|
const tzIds = TZ_ID_MAP.get(utc);
|
|
57
61
|
if (!tzIds || tzIds?.length === 0)
|
|
@@ -121,12 +125,13 @@ const timeZonePlugin = ($Chronos) => {
|
|
|
121
125
|
if (!utc && tracker && _isValidTzAbbr(tracker))
|
|
122
126
|
return tracker;
|
|
123
127
|
if ((0, guards_1.isValidUTCOffset)(tzMapKey)) {
|
|
124
|
-
if (TZ_ABBR_CACHE.has(tzMapKey))
|
|
128
|
+
if (TZ_ABBR_CACHE.has(tzMapKey)) {
|
|
125
129
|
return TZ_ABBR_CACHE.get(tzMapKey);
|
|
126
|
-
if (TZ_NAME_ABBR_MAP.has(tzMapKey)) {
|
|
127
|
-
return TZ_NAME_ABBR_MAP.get(tzMapKey)?.tzAbbr;
|
|
128
130
|
}
|
|
129
131
|
const tzName = _resolveTzName(tzMapKey);
|
|
132
|
+
if (!tzName && TZ_NAME_ABBR_MAP.has(tzMapKey)) {
|
|
133
|
+
return TZ_NAME_ABBR_MAP.get(tzMapKey)?.tzAbbr;
|
|
134
|
+
}
|
|
130
135
|
if (tzName) {
|
|
131
136
|
const tzAbbr = _abbreviate(tzName);
|
|
132
137
|
TZ_ABBR_CACHE.set(tzMapKey, tzAbbr);
|
|
@@ -134,10 +139,12 @@ const timeZonePlugin = ($Chronos) => {
|
|
|
134
139
|
}
|
|
135
140
|
}
|
|
136
141
|
const zone = _getTimeZoneName(tzMapKey, $Date(this)) ?? UTC;
|
|
137
|
-
|
|
138
|
-
|
|
142
|
+
const fallbackKey = `name-${zone}`;
|
|
143
|
+
if (TZ_ABBR_CACHE.has(fallbackKey)) {
|
|
144
|
+
return TZ_ABBR_CACHE.get(fallbackKey);
|
|
145
|
+
}
|
|
139
146
|
const customAbbr = (0, guards_1.isValidUTCOffset)(zone) ? zone : _abbreviate(zone);
|
|
140
|
-
TZ_ABBR_CACHE.set(
|
|
147
|
+
TZ_ABBR_CACHE.set(fallbackKey, customAbbr);
|
|
141
148
|
return customAbbr;
|
|
142
149
|
};
|
|
143
150
|
$Chronos.prototype.getTimeZoneNameAbbr = function (utc) {
|
|
@@ -317,6 +317,34 @@ export type RangeTuple<T, Min extends number, Max extends number> = $Range<Min,
|
|
|
317
317
|
export type ValueOptional<O, K extends keyof O = keyof O> = {
|
|
318
318
|
[P in keyof O]: P extends K ? O[P] | undefined : O[P];
|
|
319
319
|
};
|
|
320
|
+
/**
|
|
321
|
+
* Makes selected properties optional while keeping the rest required
|
|
322
|
+
* @remarks By default all properties are optional, same as {@link Partial<T>}
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* type A = { name: string; age: number };
|
|
326
|
+
* type B = PropertyOptional<A, 'name'>;
|
|
327
|
+
* // Equivalent to: { name?: string | undefined; age: number }
|
|
328
|
+
*
|
|
329
|
+
* @example
|
|
330
|
+
* type C = PropertyOptional<A>;
|
|
331
|
+
* // Equivalent to: { name?: string | undefined; age?: number | undefined }
|
|
332
|
+
*/
|
|
333
|
+
export type PropertyOptional<O, K extends keyof O = keyof O> = Prettify<Omit<O, K> & Partial<Pick<O, K>>>;
|
|
334
|
+
/**
|
|
335
|
+
* Makes selected properties required while keeping the rest unchanged
|
|
336
|
+
* @remarks By default all properties are required, same as {@link Required<T>}
|
|
337
|
+
*
|
|
338
|
+
* @example
|
|
339
|
+
* type A = { name?: string; age: number };
|
|
340
|
+
* type B = PropertyRequired<A, 'name'>;
|
|
341
|
+
* // Equivalent to: { name: string; age: number | undefined }
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* type C = PropertyRequired<A>;
|
|
345
|
+
* // Equivalent to: { name: string; age: number }
|
|
346
|
+
*/
|
|
347
|
+
export type PropertyRequired<O, K extends keyof O = keyof O> = Prettify<Omit<O, K> & Required<Pick<O, K>>>;
|
|
320
348
|
export type $Without<T, U> = {
|
|
321
349
|
[P in Exclude<keyof T, keyof U>]?: never;
|
|
322
350
|
};
|
|
@@ -53,7 +53,7 @@ export const roundPlugin = ($Chronos) => {
|
|
|
53
53
|
const diffToStart = Math.abs(date.getTime() - startOfWeek.getTime());
|
|
54
54
|
const diffToEnd = Math.abs(endOfWeek.getTime() - date.getTime());
|
|
55
55
|
const rounded = diffToEnd < diffToStart ? endOfWeek : startOfWeek;
|
|
56
|
-
return withOrigin(new $Chronos(rounded), 'round');
|
|
56
|
+
return withOrigin(new $Chronos(rounded), 'round', offset(this), this.timeZoneName, this.timeZoneId, this.$tzTracker);
|
|
57
57
|
}
|
|
58
58
|
case 'month': {
|
|
59
59
|
const fullMonth = date.getMonth() + date.getDate() / this.lastDateOfMonth;
|
|
@@ -45,10 +45,14 @@ export const timeZonePlugin = ($Chronos) => {
|
|
|
45
45
|
}
|
|
46
46
|
return tzIds;
|
|
47
47
|
}, new Map()));
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
const _buildTzNameAbbrMap = () => {
|
|
49
|
+
const result = [];
|
|
50
|
+
for (const [tzAbbr, { offset, tzName }] of Object.entries(TIME_ZONES)) {
|
|
51
|
+
result.unshift([offset, { tzAbbr, tzName }]);
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
};
|
|
55
|
+
const TZ_NAME_ABBR_MAP = new Map(_buildTzNameAbbrMap());
|
|
52
56
|
const _getTimeZoneId = (utc) => {
|
|
53
57
|
const tzIds = TZ_ID_MAP.get(utc);
|
|
54
58
|
if (!tzIds || tzIds?.length === 0)
|
|
@@ -118,12 +122,13 @@ export const timeZonePlugin = ($Chronos) => {
|
|
|
118
122
|
if (!utc && tracker && _isValidTzAbbr(tracker))
|
|
119
123
|
return tracker;
|
|
120
124
|
if (isValidUTCOffset(tzMapKey)) {
|
|
121
|
-
if (TZ_ABBR_CACHE.has(tzMapKey))
|
|
125
|
+
if (TZ_ABBR_CACHE.has(tzMapKey)) {
|
|
122
126
|
return TZ_ABBR_CACHE.get(tzMapKey);
|
|
123
|
-
if (TZ_NAME_ABBR_MAP.has(tzMapKey)) {
|
|
124
|
-
return TZ_NAME_ABBR_MAP.get(tzMapKey)?.tzAbbr;
|
|
125
127
|
}
|
|
126
128
|
const tzName = _resolveTzName(tzMapKey);
|
|
129
|
+
if (!tzName && TZ_NAME_ABBR_MAP.has(tzMapKey)) {
|
|
130
|
+
return TZ_NAME_ABBR_MAP.get(tzMapKey)?.tzAbbr;
|
|
131
|
+
}
|
|
127
132
|
if (tzName) {
|
|
128
133
|
const tzAbbr = _abbreviate(tzName);
|
|
129
134
|
TZ_ABBR_CACHE.set(tzMapKey, tzAbbr);
|
|
@@ -131,10 +136,12 @@ export const timeZonePlugin = ($Chronos) => {
|
|
|
131
136
|
}
|
|
132
137
|
}
|
|
133
138
|
const zone = _getTimeZoneName(tzMapKey, $Date(this)) ?? UTC;
|
|
134
|
-
|
|
135
|
-
|
|
139
|
+
const fallbackKey = `name-${zone}`;
|
|
140
|
+
if (TZ_ABBR_CACHE.has(fallbackKey)) {
|
|
141
|
+
return TZ_ABBR_CACHE.get(fallbackKey);
|
|
142
|
+
}
|
|
136
143
|
const customAbbr = isValidUTCOffset(zone) ? zone : _abbreviate(zone);
|
|
137
|
-
TZ_ABBR_CACHE.set(
|
|
144
|
+
TZ_ABBR_CACHE.set(fallbackKey, customAbbr);
|
|
138
145
|
return customAbbr;
|
|
139
146
|
};
|
|
140
147
|
$Chronos.prototype.getTimeZoneNameAbbr = function (utc) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nhb-toolbox",
|
|
3
|
-
"version": "4.30.
|
|
3
|
+
"version": "4.30.11",
|
|
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",
|
|
@@ -44,12 +44,12 @@
|
|
|
44
44
|
},
|
|
45
45
|
"license": "Apache-2.0",
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@biomejs/biome": "^2.4.
|
|
47
|
+
"@biomejs/biome": "^2.4.15",
|
|
48
48
|
"@types/jest": "^30.0.0",
|
|
49
|
-
"@types/node": "^25.
|
|
49
|
+
"@types/node": "^25.8.0",
|
|
50
50
|
"husky": "^9.1.7",
|
|
51
|
-
"jest": "^30.
|
|
52
|
-
"lint-staged": "^
|
|
51
|
+
"jest": "^30.4.2",
|
|
52
|
+
"lint-staged": "^17.0.4",
|
|
53
53
|
"nhb-scripts": "^1.9.2",
|
|
54
54
|
"ts-jest": "^29.4.9",
|
|
55
55
|
"typescript": "^6.0.3"
|