nhb-toolbox 4.20.46 → 4.20.50
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 +10 -0
- package/dist/cjs/date/chronos-statics.js +2 -0
- package/dist/cjs/number/Currency.js +3 -2
- package/dist/dts/date/chronos-fn.d.ts +4 -4
- package/dist/dts/date/chronos-statics.d.ts +290 -0
- package/dist/dts/date/types.d.ts +9 -296
- package/dist/dts/object/convert.d.ts +3 -9
- package/dist/dts/object/types.d.ts +16 -9
- package/dist/esm/date/chronos-statics.js +1 -0
- package/dist/esm/number/Currency.js +3 -2
- package/package.json +8 -2
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,16 @@ All notable changes to the package will be documented here.
|
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
+
## [4.20.50] - 2025-09-25
|
|
10
|
+
|
|
11
|
+
- **Fixed** _return type_ of `convertObjectValues` utility to correctly reflect the _transformed object structure_ and `keys` option is now _more strict_: **only accepts keys which values are string and/or number** and **the array cannot be left empty**.
|
|
12
|
+
- **Updated** _options type_ for `with()` _static method_ of `Chronos`.
|
|
13
|
+
|
|
14
|
+
## [4.20.48] - 2025-09-22
|
|
15
|
+
|
|
16
|
+
- **Wrapped** `ChronosMethods` type in `LooseLiteral` to allow passing _custom method names_ without _type errors_ when creating a custom [`Chronos Plugin`](https://toolbox.nazmul-nhb.dev/docs/classes/Chronos/plugins#%EF%B8%8F-writing-your-own-custom-plugin).
|
|
17
|
+
- **Updated** _error message_ in `convert` method in `Currency` class.
|
|
18
|
+
|
|
9
19
|
## [4.20.46] - 2025-09-22
|
|
10
20
|
|
|
11
21
|
- `Chronos` class is now _exported via subpath_ `'nhb-toolbox/chronos'`.
|
|
@@ -56,11 +56,12 @@ class Currency {
|
|
|
56
56
|
const url = `https://api.frankfurter.app/latest?amount=${this.#amount}&from=${this.#code}`;
|
|
57
57
|
try {
|
|
58
58
|
const res = await fetch(url, { redirect: 'error' });
|
|
59
|
-
if (!res.ok)
|
|
59
|
+
if (!res.ok) {
|
|
60
60
|
throw new Error(`FrankFurter Error: ${res.status}. "${res.statusText}"`);
|
|
61
|
+
}
|
|
61
62
|
const data = await res.json();
|
|
62
63
|
if (!data.rates?.[to]) {
|
|
63
|
-
throw new Error(`Currency "${to}" not found in FrankFurter Database!`);
|
|
64
|
+
throw new Error(`Currency "${to}" not allowed or not found in FrankFurter Database!`);
|
|
64
65
|
}
|
|
65
66
|
return data.rates[to] / this.#amount;
|
|
66
67
|
}
|
|
@@ -32,11 +32,10 @@ import type { ChronosStatics } from './types';
|
|
|
32
32
|
* **Available Static Methods:**
|
|
33
33
|
*
|
|
34
34
|
* ```ts
|
|
35
|
-
* chronos.today(options?: FormatOptions): string;
|
|
36
|
-
* chronos.yesterday(): Chronos;
|
|
37
|
-
* chronos.tomorrow(): Chronos;
|
|
38
35
|
* chronos.now(): number;
|
|
39
|
-
* chronos.
|
|
36
|
+
* chronos.tomorrow(): Chronos;
|
|
37
|
+
* chronos.yesterday(): Chronos;
|
|
38
|
+
* chronos.today(options?: FormatOptions): string;
|
|
40
39
|
* chronos.with(options: ChronosWithOptions): Chronos;
|
|
41
40
|
* chronos.parse(dateStr: string, format: string): Chronos;
|
|
42
41
|
* chronos.utc(dateLike: ChronosInput): Chronos;
|
|
@@ -48,6 +47,7 @@ import type { ChronosStatics } from './types';
|
|
|
48
47
|
* chronos.isValidChronos(value: unknown): boolean;
|
|
49
48
|
* chronos.formatTimePart(time: string, format?: TimeParts): string;
|
|
50
49
|
* chronos.getDatesForDay(day: WeekDay, options?: WeekdayOptions): string[];
|
|
50
|
+
* chronos.use(plugin: ChronosPlugin): void;
|
|
51
51
|
* ```
|
|
52
52
|
*/
|
|
53
53
|
declare const chronosStatics: ChronosStatics;
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import type { Chronos } from './Chronos';
|
|
2
|
+
import type { ChronosInput, ChronosPlugin, ChronosWithOptions, DateRangeOptions, FormatOptions, RelativeRangeOptions, TimeParts, WeekDay, WeekdayOptions } from './types';
|
|
3
|
+
/** All the statics methods in `Chronos` class */
|
|
4
|
+
export interface ChronosStatics {
|
|
5
|
+
/**
|
|
6
|
+
* * Converts a date into a `Chronos` object and access to all `Chronos` methods and properties.
|
|
7
|
+
*
|
|
8
|
+
* @description
|
|
9
|
+
* This function serves as a wrapper around the `Chronos` class constructor and allows you to create a new `Chronos` instance from various types of date representations.
|
|
10
|
+
*
|
|
11
|
+
* Accepts no arguments (defaults to now).
|
|
12
|
+
*
|
|
13
|
+
* @returns Instance of `Chronos` with all methods and properties.
|
|
14
|
+
*/
|
|
15
|
+
(): Chronos;
|
|
16
|
+
/**
|
|
17
|
+
* * Converts a date into a `Chronos` object and access to all `Chronos` methods and properties.
|
|
18
|
+
*
|
|
19
|
+
* @description
|
|
20
|
+
* This function serves as a wrapper around the `Chronos` class constructor and allows you to create a new `Chronos` instance from various types of date representations.
|
|
21
|
+
*
|
|
22
|
+
* @param value - A date value in `number`, it should be a timestamp (milliseconds since the Unix epoch).
|
|
23
|
+
*
|
|
24
|
+
* @returns Instance of `Chronos` with all methods and properties.
|
|
25
|
+
*/
|
|
26
|
+
(value: number): Chronos;
|
|
27
|
+
/**
|
|
28
|
+
* * Converts a date into a `Chronos` object and access to all `Chronos` methods and properties.
|
|
29
|
+
*
|
|
30
|
+
* **Note**: *If a date is provided **without a time component**, the instance will default to `00:00:00.000` UTC
|
|
31
|
+
* and convert it to the **equivalent local time** using the current environment's UTC offset.*
|
|
32
|
+
*
|
|
33
|
+
* @description
|
|
34
|
+
* This function serves as a wrapper around the `Chronos` class constructor and allows you to create a new `Chronos` instance from various types of date representations.
|
|
35
|
+
*
|
|
36
|
+
* @param value - A date value in `string`, it should be in a format that can be parsed by the `Date` constructor.
|
|
37
|
+
*
|
|
38
|
+
* @returns Instance of `Chronos` with all methods and properties.
|
|
39
|
+
*/
|
|
40
|
+
(value: string): Chronos;
|
|
41
|
+
/**
|
|
42
|
+
* * Converts a date into a `Chronos` object and access to all `Chronos` methods and properties.
|
|
43
|
+
*
|
|
44
|
+
* **Note**: *If a date is provided **without a time component**, the instance will default to `00:00:00.000` UTC
|
|
45
|
+
* and convert it to the **equivalent local time** using the current environment's UTC offset.*
|
|
46
|
+
*
|
|
47
|
+
* @description
|
|
48
|
+
* This function serves as a wrapper around the `Chronos` class constructor and allows you to create a new `Chronos` instance from various types of date representations.
|
|
49
|
+
*
|
|
50
|
+
* @param value - A date value as `Date` object, it will be used as is.
|
|
51
|
+
*
|
|
52
|
+
* @returns Instance of `Chronos` with all methods and properties.
|
|
53
|
+
*/
|
|
54
|
+
(value: Date): Chronos;
|
|
55
|
+
/**
|
|
56
|
+
* * Converts a date into a `Chronos` object and access to all `Chronos` methods and properties.
|
|
57
|
+
*
|
|
58
|
+
* **Note**: *If a date is provided **without a time component**, the instance will default to `00:00:00.000` UTC
|
|
59
|
+
* and convert it to the **equivalent local time** using the current environment's UTC offset.*
|
|
60
|
+
*
|
|
61
|
+
* @description
|
|
62
|
+
* This function serves as a wrapper around the `Chronos` class constructor and allows you to create a new `Chronos` instance from various types of date representations.
|
|
63
|
+
*
|
|
64
|
+
* @param value - A date value as `Chronos` object.
|
|
65
|
+
*
|
|
66
|
+
* @returns Instance of `Chronos` with all methods and properties.
|
|
67
|
+
*/
|
|
68
|
+
(value: Chronos): Chronos;
|
|
69
|
+
/**
|
|
70
|
+
* * Converts a date into a `Chronos` object and access to all `Chronos` methods and properties.
|
|
71
|
+
*
|
|
72
|
+
* @description
|
|
73
|
+
* This function serves as a wrapper around the `Chronos` class constructor and allows you to create a new `Chronos` instance from various types of date representations.
|
|
74
|
+
*
|
|
75
|
+
* @param year The full year designation is required for cross-century date accuracy. If year is between 0 and 99, year is assumed to be 1900 + year.
|
|
76
|
+
* @param month The month as a number between 1 and 12 (January to December).
|
|
77
|
+
* @param date The date as a number between 1 and 31.
|
|
78
|
+
* @param hours Must be supplied if minutes is supplied. A number from 0 to 23 (midnight to 11pm) that specifies the hour.
|
|
79
|
+
* @param minutes Must be supplied if seconds is supplied. A number from 0 to 59 that specifies the minutes.
|
|
80
|
+
* @param seconds Must be supplied if milliseconds is supplied. A number from 0 to 59 that specifies the seconds.
|
|
81
|
+
* @param ms A number from 0 to 999 that specifies the milliseconds.
|
|
82
|
+
*
|
|
83
|
+
* @returns Instance of `Chronos` with all methods and properties.
|
|
84
|
+
*/
|
|
85
|
+
(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Chronos;
|
|
86
|
+
/**
|
|
87
|
+
* @static Injects a plugin into the `Chronos` system.
|
|
88
|
+
* @param plugin The plugin to inject.
|
|
89
|
+
*
|
|
90
|
+
* - **NOTE:** *Once a plugin is injected, all the registered methods for that plugin will be available for the whole project.*
|
|
91
|
+
* - See full list of plugins and the methods they register {@link https://toolbox.nazmul-nhb.dev/docs/classes/Chronos/plugins#-official-plugins here}.
|
|
92
|
+
*/
|
|
93
|
+
use(plugin: ChronosPlugin): void;
|
|
94
|
+
/**
|
|
95
|
+
* * Returns the current date and time in a specified format in local time.
|
|
96
|
+
* * Default format is dd, `MMM DD, YYYY HH:mm:ss` = `Sun, Apr 06, 2025 16:11:55`
|
|
97
|
+
* @param options - Configure format string and whether to format using utc offset.
|
|
98
|
+
* @returns Formatted date string in desired format.
|
|
99
|
+
*/
|
|
100
|
+
today(options?: FormatOptions): string;
|
|
101
|
+
/**
|
|
102
|
+
* * Returns a new `Chronos` instance representing yesterday's date.
|
|
103
|
+
*
|
|
104
|
+
* @returns A `Chronos` instance for the next calendar day.
|
|
105
|
+
*/
|
|
106
|
+
yesterday(): Chronos;
|
|
107
|
+
/**
|
|
108
|
+
* * Returns a new `Chronos` instance representing tomorrow's date.
|
|
109
|
+
*
|
|
110
|
+
* @returns A `Chronos` instance for the next calendar day.
|
|
111
|
+
*/
|
|
112
|
+
tomorrow(): Chronos;
|
|
113
|
+
/**
|
|
114
|
+
* * Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC).
|
|
115
|
+
* * It basically calls `Date.now()`.
|
|
116
|
+
* @returns The number of milliseconds elapsed since the Unix epoch.
|
|
117
|
+
*/
|
|
118
|
+
now(): number;
|
|
119
|
+
/**
|
|
120
|
+
* * Parses a date string with a given format (limited support only).
|
|
121
|
+
*
|
|
122
|
+
* * **Supported format tokens**:
|
|
123
|
+
* - `YYYY`: Full year (e.g., 2023)
|
|
124
|
+
* - `YY`: Two-digit year (e.g., 23 for 2023, 99 for 1999)
|
|
125
|
+
* - `MM`: Month (01-12)
|
|
126
|
+
* - `M`: 1-Digit Month (1-9)
|
|
127
|
+
* - `DD`: Day of the month (01-31)
|
|
128
|
+
* - `D`: 1-Digit Day of the month (1-9)
|
|
129
|
+
* - `HH`: Hour (00-23)
|
|
130
|
+
* - `H`: 1-Digit Hour (0-9)
|
|
131
|
+
* - `mm`: Minute (00-59)
|
|
132
|
+
* - `m`: 1-Digit Minute (0-9)
|
|
133
|
+
* - `ss`: Second (00-59)
|
|
134
|
+
* - `s`: 1-Digit Second (0-9)
|
|
135
|
+
*
|
|
136
|
+
* **Example**:
|
|
137
|
+
* ```ts
|
|
138
|
+
* Chronos.parse('23-12-31 15:30:45', 'YY-MM-DD HH:mm:ss');
|
|
139
|
+
* // returns Chronos instance with the parsed date 2023-12-31T15:30:45
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @param dateStr - The date string to be parsed
|
|
143
|
+
* @param format - The format of the date string. Tokens like `YYYY`, `MM`, `DD`, `HH`, `mm`, `ss` are used to specify the structure.
|
|
144
|
+
* @returns A new `Chronos` instance representing the parsed date.
|
|
145
|
+
* @throws `Error` If the date string does not match the format.
|
|
146
|
+
*/
|
|
147
|
+
parse(dateStr: string, format: string): Chronos;
|
|
148
|
+
/**
|
|
149
|
+
* * Creates a new `Chronos` instance with the provided time component(s).
|
|
150
|
+
*
|
|
151
|
+
* @param options - One or more time components to override.
|
|
152
|
+
* @returns A new `Chronos` instance with the provided time components applied.
|
|
153
|
+
*
|
|
154
|
+
* @remarks
|
|
155
|
+
* - Unspecified components are filled with the current time's (`Chronos`) respective values.
|
|
156
|
+
* - For option `month`, value should be number from `1` (January) to `12` (December).
|
|
157
|
+
* - If the `date` component is omitted and the current day is the last day of its month,
|
|
158
|
+
* the resulting instance will also use the last day of the target month.
|
|
159
|
+
* - _This rule does **not** apply if the `date` component is explicitly provided,
|
|
160
|
+
* even if that value exceeds the last day of the target month._
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* // Override only the year and month
|
|
164
|
+
* const c = Chronos.with({ year: 2025, month: 12 });
|
|
165
|
+
*/
|
|
166
|
+
with(options: ChronosWithOptions): Chronos;
|
|
167
|
+
/**
|
|
168
|
+
* * Creates a UTC-based Chronos instance.
|
|
169
|
+
* If no date is provided, it uses the current date and time.
|
|
170
|
+
*
|
|
171
|
+
* **This is the base time, meaning conversion in other timezone will consider UTC time as the base time.**
|
|
172
|
+
*
|
|
173
|
+
* @param dateLike Optional input date to base the UTC time on.
|
|
174
|
+
* If omitted, the current system date/time is used.
|
|
175
|
+
* @returns A new Chronos instance representing the UTC equivalent of the input.
|
|
176
|
+
*/
|
|
177
|
+
utc(dateLike?: ChronosInput): Chronos;
|
|
178
|
+
/**
|
|
179
|
+
* * Formats a time-only string into a formatted time string.
|
|
180
|
+
*
|
|
181
|
+
* @param time - Time string to be formatted. Supported formats include:
|
|
182
|
+
* - `HH:mm` → e.g., `'14:50'`
|
|
183
|
+
* - `HH:mm:ss` → e.g., `'14:50:00'`
|
|
184
|
+
* - `HH:mm:ss.SSS` → e.g., `'14:50:00.800'`
|
|
185
|
+
* - `HH:mm+TimeZoneOffset(HH)` → e.g., `'14:50+06'`
|
|
186
|
+
* - `HH:mm:ss+TimeZoneOffset(HH)` → e.g., `'14:50:00+06'`
|
|
187
|
+
* - `HH:mm:ss+TimeZoneOffset(HH:mm)` → e.g., `'14:50:00+05:30'`
|
|
188
|
+
* - `HH:mm:ss.SSS+TimeZoneOffset(HH)` → e.g., `'14:50:00.800+06'`
|
|
189
|
+
* - `HH:mm:ss.SSS+TimeZoneOffset(HH:mm)` → e.g., `'14:50:00.800+06:30'`
|
|
190
|
+
*
|
|
191
|
+
* * *Input will default to today's date and assume local timezone if no offset is provided.*
|
|
192
|
+
*
|
|
193
|
+
* @param format - Format string accepted by `formatStrict()` method (`TimeParts`). Default: `hh:mm:ss a` → 02:33:36 pm.
|
|
194
|
+
* @returns Formatted time string in local (System) time.
|
|
195
|
+
*/
|
|
196
|
+
formatTimePart(time: string, format?: TimeParts): string;
|
|
197
|
+
/**
|
|
198
|
+
* * Returns ISO date strings for each occurrence of a weekday from today, spanning a relative time range.
|
|
199
|
+
*
|
|
200
|
+
* @param day - The weekday to match (e.g., `'Wednesday'`, `'Sunday'`).
|
|
201
|
+
* @param options - Relative range (e.g., 7 days, 4 weeks) and output format (local with timezone or utc).
|
|
202
|
+
* @returns Array of ISO date strings in the specified format. Returns empty array if no matches in the time span.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* Chronos.getDatesForDay('Wednesday', { span: 7, unit: 'day' });
|
|
206
|
+
* //=> [ '2025-05-28T21:16:06.198+06:00', '2025-06-04T21:16:06.198+06:00' ]
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* Chronos.getDatesForDay('Wednesday', {
|
|
210
|
+
* span: 7,
|
|
211
|
+
* unit: 'day',
|
|
212
|
+
* format: 'utc'
|
|
213
|
+
* });
|
|
214
|
+
* //=> [ '2025-05-28T15:17:10.812Z', '2025-06-04T15:17:10.812Z' ]
|
|
215
|
+
*/
|
|
216
|
+
getDatesForDay(day: WeekDay, options?: RelativeRangeOptions): string[];
|
|
217
|
+
/**
|
|
218
|
+
* * Returns ISO date strings for each occurrence of a weekday between two fixed dates.
|
|
219
|
+
*
|
|
220
|
+
* @param day - The weekday to match (e.g., `'Monday'`, `'Friday'`).
|
|
221
|
+
* @param options - Absolute date range (e.g. `'2025-06-30'`, ` new Date()`, `new Chronos()` etc.) and output format (local with timezone or utc).
|
|
222
|
+
* @returns Array of ISO date strings in the specified format. Returns empty array if no matches in the range.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* Chronos.getDatesForDay('Monday', {
|
|
226
|
+
* from: '2025-05-28',
|
|
227
|
+
* to: '2025-06-30',
|
|
228
|
+
* format: 'local'
|
|
229
|
+
* });
|
|
230
|
+
* //=> [ '2025-01-06T...', '2025-01-13T...', ... ]
|
|
231
|
+
*/
|
|
232
|
+
getDatesForDay(day: WeekDay, options?: DateRangeOptions): string[];
|
|
233
|
+
/**
|
|
234
|
+
* * Returns ISO date strings for each occurrence of a weekday.
|
|
235
|
+
*
|
|
236
|
+
* @param day - The weekday to match (e.g., `'Wednesday'`, `'Sunday'`).
|
|
237
|
+
* @param options - Relative range (e.g., 7 days, 4 weeks) or Absolute date range and output format.
|
|
238
|
+
* @returns Array of ISO date strings in the specified format.
|
|
239
|
+
*/
|
|
240
|
+
getDatesForDay(day: WeekDay, options?: WeekdayOptions): string[];
|
|
241
|
+
/**
|
|
242
|
+
* * Returns earliest Chronos
|
|
243
|
+
* @param dates Date inputs.
|
|
244
|
+
*/
|
|
245
|
+
min(...dates: ChronosInput[]): Chronos;
|
|
246
|
+
/**
|
|
247
|
+
* * Returns latest Chronos
|
|
248
|
+
* @param dates Date inputs.
|
|
249
|
+
*/
|
|
250
|
+
max(...dates: ChronosInput[]): Chronos;
|
|
251
|
+
/**
|
|
252
|
+
* * Checks if the year in the date string or year (from 0 - 9999) is a leap year.
|
|
253
|
+
* - A year is a leap year if it is divisible by 4, but not divisible by 100, unless it is also divisible by 400.
|
|
254
|
+
* - For example, 2000 and 2400 are leap years, but 1900 and 2100 are not.
|
|
255
|
+
*
|
|
256
|
+
* @description
|
|
257
|
+
* This method accepts different types of date inputs and extracts the year to check if it's a leap year.
|
|
258
|
+
* If the provided date is a `number`, it will be treated as a year (must be a valid year from 0 to 9999).
|
|
259
|
+
* If the year is out of this range (negative or larger than 9999), it will be treated as a Unix timestamp.
|
|
260
|
+
* If the provided date is a string or a `Date` object, it will be parsed and the year will be extracted.
|
|
261
|
+
* If a `Chronos` instance is passed, the year will be directly accessed from the instance.
|
|
262
|
+
*
|
|
263
|
+
* @param date - A `number` (year or Unix timestamp), `string`, `Date`, or `Chronos` instance representing a date.
|
|
264
|
+
* @returns `true` if the year is a leap year, `false` otherwise.
|
|
265
|
+
*/
|
|
266
|
+
isLeapYear(date: ChronosInput): boolean;
|
|
267
|
+
/**
|
|
268
|
+
* * Checks if the given value is a valid `Date` object.
|
|
269
|
+
* - A value is considered valid if it is an instance of the built-in `Date` class.
|
|
270
|
+
* - This does not check whether the date itself is valid (e.g., `new Date('invalid')`).
|
|
271
|
+
* @param value - The value to test.
|
|
272
|
+
* @returns `true` if the value is a valid Date object, otherwise `false`.
|
|
273
|
+
*/
|
|
274
|
+
isValidDate(value: unknown): value is Date;
|
|
275
|
+
/**
|
|
276
|
+
* * Checks if the given value is a valid date string.
|
|
277
|
+
* - A value is considered a valid date string if it is a string and can be parsed by `Date.parse()`.
|
|
278
|
+
* - This uses the native JavaScript date parser internally.
|
|
279
|
+
* @param value - The value to test.
|
|
280
|
+
* @returns `true` if the value is a valid date string, otherwise `false`.
|
|
281
|
+
*/
|
|
282
|
+
isDateString(value: unknown): value is string;
|
|
283
|
+
/**
|
|
284
|
+
* * Checks if the given value is an instance of `Chronos`.
|
|
285
|
+
* - Useful for verifying Chronos objects in type guards or validations.
|
|
286
|
+
* @param value - The value to test.
|
|
287
|
+
* @returns `true` if the value is an instance of `Chronos`, otherwise `false`.
|
|
288
|
+
*/
|
|
289
|
+
isValidChronos(value: unknown): value is Chronos;
|
|
290
|
+
}
|
package/dist/dts/date/types.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { Enumerate, NumberRange } from '../number/types';
|
|
2
|
-
import type { LooseLiteral
|
|
2
|
+
import type { LooseLiteral } from '../utils/types';
|
|
3
3
|
import type { Chronos } from './Chronos';
|
|
4
|
+
import type { ChronosStatics } from './chronos-statics';
|
|
4
5
|
import type { DATE_FORMATS, DAY_FORMATS, DAYS, HOUR_FORMATS, MILLISECOND_FORMATS, MINUTE_FORMATS, MONTH_FORMATS, MONTHS, SECOND_FORMATS, TIME_FORMATS, TIME_ZONES, WESTERN_ZODIAC_SIGNS, YEAR_FORMATS, ZODIAC_PRESETS } from './constants';
|
|
5
6
|
import type { SEASON_PRESETS } from './seasons';
|
|
7
|
+
export type { ChronosStatics };
|
|
6
8
|
/** - Minute in numeric string from `00` to `23` */
|
|
7
9
|
export type ClockHour = `0${Enumerate<10>}` | `${NumberRange<10, 23>}`;
|
|
8
10
|
/** - Minute in numeric string from `00` to `59` */
|
|
@@ -133,7 +135,7 @@ export interface ChronosInternals {
|
|
|
133
135
|
export type WithoutOrigin = Omit<Chronos, '#ORIGIN' | 'origin'>;
|
|
134
136
|
type PluginMethods = 'timeZone';
|
|
135
137
|
/** Methods (both instance and static) in `Chronos` class that return `Chronos` instance. */
|
|
136
|
-
export type ChronosMethods = {
|
|
138
|
+
export type ChronosMethods = LooseLiteral<{
|
|
137
139
|
[K in keyof WithoutOrigin]: Chronos extends {
|
|
138
140
|
[key in K]: (...args: any[]) => Chronos;
|
|
139
141
|
} ? K : never;
|
|
@@ -141,7 +143,7 @@ export type ChronosMethods = {
|
|
|
141
143
|
[K in keyof typeof Chronos]: typeof Chronos extends {
|
|
142
144
|
[key in K]: (...args: any[]) => Chronos;
|
|
143
145
|
} ? K : never;
|
|
144
|
-
}[keyof typeof Chronos] | PluginMethods
|
|
146
|
+
}[keyof typeof Chronos] | PluginMethods>;
|
|
145
147
|
/**
|
|
146
148
|
* * Accepted Input type for `Chronos`
|
|
147
149
|
*
|
|
@@ -150,296 +152,8 @@ export type ChronosMethods = {
|
|
|
150
152
|
*
|
|
151
153
|
*/
|
|
152
154
|
export type ChronosInput = number | string | Date | Chronos;
|
|
153
|
-
/** Represents key of `ChronosStatics` (each method and property) */
|
|
155
|
+
/** Represents key of `ChronosStatics` (each static method and property) */
|
|
154
156
|
export type ChronosStaticKey = keyof ChronosStatics;
|
|
155
|
-
/** All the statics methods in `Chronos` class */
|
|
156
|
-
export interface ChronosStatics {
|
|
157
|
-
/**
|
|
158
|
-
* * Converts a date into a `Chronos` object and access to all `Chronos` methods and properties.
|
|
159
|
-
*
|
|
160
|
-
* @description
|
|
161
|
-
* This function serves as a wrapper around the `Chronos` class constructor and allows you to create a new `Chronos` instance from various types of date representations.
|
|
162
|
-
*
|
|
163
|
-
* Accepts no arguments (defaults to now).
|
|
164
|
-
*
|
|
165
|
-
* @returns Instance of `Chronos` with all methods and properties.
|
|
166
|
-
*/
|
|
167
|
-
(): Chronos;
|
|
168
|
-
/**
|
|
169
|
-
* * Converts a date into a `Chronos` object and access to all `Chronos` methods and properties.
|
|
170
|
-
*
|
|
171
|
-
* @description
|
|
172
|
-
* This function serves as a wrapper around the `Chronos` class constructor and allows you to create a new `Chronos` instance from various types of date representations.
|
|
173
|
-
*
|
|
174
|
-
* @param value - A date value in `number`, it should be a timestamp (milliseconds since the Unix epoch).
|
|
175
|
-
*
|
|
176
|
-
* @returns Instance of `Chronos` with all methods and properties.
|
|
177
|
-
*/
|
|
178
|
-
(value: number): Chronos;
|
|
179
|
-
/**
|
|
180
|
-
* * Converts a date into a `Chronos` object and access to all `Chronos` methods and properties.
|
|
181
|
-
*
|
|
182
|
-
* **Note**: *If a date is provided **without a time component**, the instance will default to `00:00:00.000` UTC
|
|
183
|
-
* and convert it to the **equivalent local time** using the current environment's UTC offset.*
|
|
184
|
-
*
|
|
185
|
-
* @description
|
|
186
|
-
* This function serves as a wrapper around the `Chronos` class constructor and allows you to create a new `Chronos` instance from various types of date representations.
|
|
187
|
-
*
|
|
188
|
-
* @param value - A date value in `string`, it should be in a format that can be parsed by the `Date` constructor.
|
|
189
|
-
*
|
|
190
|
-
* @returns Instance of `Chronos` with all methods and properties.
|
|
191
|
-
*/
|
|
192
|
-
(value: string): Chronos;
|
|
193
|
-
/**
|
|
194
|
-
* * Converts a date into a `Chronos` object and access to all `Chronos` methods and properties.
|
|
195
|
-
*
|
|
196
|
-
* **Note**: *If a date is provided **without a time component**, the instance will default to `00:00:00.000` UTC
|
|
197
|
-
* and convert it to the **equivalent local time** using the current environment's UTC offset.*
|
|
198
|
-
*
|
|
199
|
-
* @description
|
|
200
|
-
* This function serves as a wrapper around the `Chronos` class constructor and allows you to create a new `Chronos` instance from various types of date representations.
|
|
201
|
-
*
|
|
202
|
-
* @param value - A date value as `Date` object, it will be used as is.
|
|
203
|
-
*
|
|
204
|
-
* @returns Instance of `Chronos` with all methods and properties.
|
|
205
|
-
*/
|
|
206
|
-
(value: Date): Chronos;
|
|
207
|
-
/**
|
|
208
|
-
* * Converts a date into a `Chronos` object and access to all `Chronos` methods and properties.
|
|
209
|
-
*
|
|
210
|
-
* **Note**: *If a date is provided **without a time component**, the instance will default to `00:00:00.000` UTC
|
|
211
|
-
* and convert it to the **equivalent local time** using the current environment's UTC offset.*
|
|
212
|
-
*
|
|
213
|
-
* @description
|
|
214
|
-
* This function serves as a wrapper around the `Chronos` class constructor and allows you to create a new `Chronos` instance from various types of date representations.
|
|
215
|
-
*
|
|
216
|
-
* @param value - A date value as `Chronos` object.
|
|
217
|
-
*
|
|
218
|
-
* @returns Instance of `Chronos` with all methods and properties.
|
|
219
|
-
*/
|
|
220
|
-
(value: Chronos): Chronos;
|
|
221
|
-
/**
|
|
222
|
-
* * Converts a date into a `Chronos` object and access to all `Chronos` methods and properties.
|
|
223
|
-
*
|
|
224
|
-
* @description
|
|
225
|
-
* This function serves as a wrapper around the `Chronos` class constructor and allows you to create a new `Chronos` instance from various types of date representations.
|
|
226
|
-
*
|
|
227
|
-
* @param year The full year designation is required for cross-century date accuracy. If year is between 0 and 99, year is assumed to be 1900 + year.
|
|
228
|
-
* @param month The month as a number between 1 and 12 (January to December).
|
|
229
|
-
* @param date The date as a number between 1 and 31.
|
|
230
|
-
* @param hours Must be supplied if minutes is supplied. A number from 0 to 23 (midnight to 11pm) that specifies the hour.
|
|
231
|
-
* @param minutes Must be supplied if seconds is supplied. A number from 0 to 59 that specifies the minutes.
|
|
232
|
-
* @param seconds Must be supplied if milliseconds is supplied. A number from 0 to 59 that specifies the seconds.
|
|
233
|
-
* @param ms A number from 0 to 999 that specifies the milliseconds.
|
|
234
|
-
*
|
|
235
|
-
* @returns Instance of `Chronos` with all methods and properties.
|
|
236
|
-
*/
|
|
237
|
-
(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Chronos;
|
|
238
|
-
/**
|
|
239
|
-
* @static Injects a plugin into the `Chronos` system.
|
|
240
|
-
* @param plugin The plugin to inject.
|
|
241
|
-
*
|
|
242
|
-
* - **NOTE:** *Once a plugin is injected, all the registered methods for that plugin will be available for the whole project.*
|
|
243
|
-
* - See full list of plugins and the methods they register {@link https://toolbox.nazmul-nhb.dev/docs/classes/Chronos/plugins#-official-plugins here}.
|
|
244
|
-
*/
|
|
245
|
-
use(plugin: ChronosPlugin): void;
|
|
246
|
-
/**
|
|
247
|
-
* * Returns the current date and time in a specified format in local time.
|
|
248
|
-
* * Default format is dd, `MMM DD, YYYY HH:mm:ss` = `Sun, Apr 06, 2025 16:11:55`
|
|
249
|
-
* @param options - Configure format string and whether to format using utc offset.
|
|
250
|
-
* @returns Formatted date string in desired format.
|
|
251
|
-
*/
|
|
252
|
-
today(options?: FormatOptions): string;
|
|
253
|
-
/**
|
|
254
|
-
* * Returns a new `Chronos` instance representing yesterday's date.
|
|
255
|
-
*
|
|
256
|
-
* @returns A `Chronos` instance for the next calendar day.
|
|
257
|
-
*/
|
|
258
|
-
yesterday(): Chronos;
|
|
259
|
-
/**
|
|
260
|
-
* * Returns a new `Chronos` instance representing tomorrow's date.
|
|
261
|
-
*
|
|
262
|
-
* @returns A `Chronos` instance for the next calendar day.
|
|
263
|
-
*/
|
|
264
|
-
tomorrow(): Chronos;
|
|
265
|
-
/**
|
|
266
|
-
* * Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC).
|
|
267
|
-
* * It basically calls `Date.now()`.
|
|
268
|
-
* @returns The number of milliseconds elapsed since the Unix epoch.
|
|
269
|
-
*/
|
|
270
|
-
now(): number;
|
|
271
|
-
/**
|
|
272
|
-
* * Parses a date string with a given format (limited support only).
|
|
273
|
-
*
|
|
274
|
-
* * **Supported format tokens**:
|
|
275
|
-
* - `YYYY`: Full year (e.g., 2023)
|
|
276
|
-
* - `YY`: Two-digit year (e.g., 23 for 2023, 99 for 1999)
|
|
277
|
-
* - `MM`: Month (01-12)
|
|
278
|
-
* - `M`: 1-Digit Month (1-9)
|
|
279
|
-
* - `DD`: Day of the month (01-31)
|
|
280
|
-
* - `D`: 1-Digit Day of the month (1-9)
|
|
281
|
-
* - `HH`: Hour (00-23)
|
|
282
|
-
* - `H`: 1-Digit Hour (0-9)
|
|
283
|
-
* - `mm`: Minute (00-59)
|
|
284
|
-
* - `m`: 1-Digit Minute (0-9)
|
|
285
|
-
* - `ss`: Second (00-59)
|
|
286
|
-
* - `s`: 1-Digit Second (0-9)
|
|
287
|
-
*
|
|
288
|
-
* **Example**:
|
|
289
|
-
* ```ts
|
|
290
|
-
* Chronos.parse('23-12-31 15:30:45', 'YY-MM-DD HH:mm:ss');
|
|
291
|
-
* // returns Chronos instance with the parsed date 2023-12-31T15:30:45
|
|
292
|
-
* ```
|
|
293
|
-
*
|
|
294
|
-
* @param dateStr - The date string to be parsed
|
|
295
|
-
* @param format - The format of the date string. Tokens like `YYYY`, `MM`, `DD`, `HH`, `mm`, `ss` are used to specify the structure.
|
|
296
|
-
* @returns A new `Chronos` instance representing the parsed date.
|
|
297
|
-
* @throws `Error` If the date string does not match the format.
|
|
298
|
-
*/
|
|
299
|
-
parse(dateStr: string, format: string): Chronos;
|
|
300
|
-
/**
|
|
301
|
-
* * Creates a new `Chronos` instance with the provided time component(s).
|
|
302
|
-
*
|
|
303
|
-
* @param options - One or more time components to override.
|
|
304
|
-
* @returns A new `Chronos` instance with the provided time components applied.
|
|
305
|
-
*
|
|
306
|
-
* @remarks
|
|
307
|
-
* - Unspecified components are filled with the current time's (`Chronos`) respective values.
|
|
308
|
-
* - For option `month`, value should be number from `1` (January) to `12` (December).
|
|
309
|
-
* - If the `date` component is omitted and the current day is the last day of its month,
|
|
310
|
-
* the resulting instance will also use the last day of the target month.
|
|
311
|
-
* - _This rule does **not** apply if the `date` component is explicitly provided,
|
|
312
|
-
* even if that value exceeds the last day of the target month._
|
|
313
|
-
*
|
|
314
|
-
* @example
|
|
315
|
-
* // Override only the year and month
|
|
316
|
-
* const c = Chronos.with({ year: 2025, month: 12 });
|
|
317
|
-
*/
|
|
318
|
-
with(options: ChronosWithOptions): Chronos;
|
|
319
|
-
/**
|
|
320
|
-
* * Creates a UTC-based Chronos instance.
|
|
321
|
-
* If no date is provided, it uses the current date and time.
|
|
322
|
-
*
|
|
323
|
-
* **This is the base time, meaning conversion in other timezone will consider UTC time as the base time.**
|
|
324
|
-
*
|
|
325
|
-
* @param dateLike Optional input date to base the UTC time on.
|
|
326
|
-
* If omitted, the current system date/time is used.
|
|
327
|
-
* @returns A new Chronos instance representing the UTC equivalent of the input.
|
|
328
|
-
*/
|
|
329
|
-
utc(dateLike?: ChronosInput): Chronos;
|
|
330
|
-
/**
|
|
331
|
-
* * Formats a time-only string into a formatted time string.
|
|
332
|
-
*
|
|
333
|
-
* @param time - Time string to be formatted. Supported formats include:
|
|
334
|
-
* - `HH:mm` → e.g., `'14:50'`
|
|
335
|
-
* - `HH:mm:ss` → e.g., `'14:50:00'`
|
|
336
|
-
* - `HH:mm:ss.SSS` → e.g., `'14:50:00.800'`
|
|
337
|
-
* - `HH:mm+TimeZoneOffset(HH)` → e.g., `'14:50+06'`
|
|
338
|
-
* - `HH:mm:ss+TimeZoneOffset(HH)` → e.g., `'14:50:00+06'`
|
|
339
|
-
* - `HH:mm:ss+TimeZoneOffset(HH:mm)` → e.g., `'14:50:00+05:30'`
|
|
340
|
-
* - `HH:mm:ss.SSS+TimeZoneOffset(HH)` → e.g., `'14:50:00.800+06'`
|
|
341
|
-
* - `HH:mm:ss.SSS+TimeZoneOffset(HH:mm)` → e.g., `'14:50:00.800+06:30'`
|
|
342
|
-
*
|
|
343
|
-
* * *Input will default to today's date and assume local timezone if no offset is provided.*
|
|
344
|
-
*
|
|
345
|
-
* @param format - Format string accepted by `formatStrict()` method (`TimeParts`). Default: `hh:mm:ss a` → 02:33:36 pm.
|
|
346
|
-
* @returns Formatted time string in local (System) time.
|
|
347
|
-
*/
|
|
348
|
-
formatTimePart(time: string, format?: TimeParts): string;
|
|
349
|
-
/**
|
|
350
|
-
* * Returns ISO date strings for each occurrence of a weekday from today, spanning a relative time range.
|
|
351
|
-
*
|
|
352
|
-
* @param day - The weekday to match (e.g., `'Wednesday'`, `'Sunday'`).
|
|
353
|
-
* @param options - Relative range (e.g., 7 days, 4 weeks) and output format (local with timezone or utc).
|
|
354
|
-
* @returns Array of ISO date strings in the specified format. Returns empty array if no matches in the time span.
|
|
355
|
-
*
|
|
356
|
-
* @example
|
|
357
|
-
* Chronos.getDatesForDay('Wednesday', { span: 7, unit: 'day' });
|
|
358
|
-
* //=> [ '2025-05-28T21:16:06.198+06:00', '2025-06-04T21:16:06.198+06:00' ]
|
|
359
|
-
*
|
|
360
|
-
* @example
|
|
361
|
-
* Chronos.getDatesForDay('Wednesday', {
|
|
362
|
-
* span: 7,
|
|
363
|
-
* unit: 'day',
|
|
364
|
-
* format: 'utc'
|
|
365
|
-
* });
|
|
366
|
-
* //=> [ '2025-05-28T15:17:10.812Z', '2025-06-04T15:17:10.812Z' ]
|
|
367
|
-
*/
|
|
368
|
-
getDatesForDay(day: WeekDay, options?: RelativeRangeOptions): string[];
|
|
369
|
-
/**
|
|
370
|
-
* * Returns ISO date strings for each occurrence of a weekday between two fixed dates.
|
|
371
|
-
*
|
|
372
|
-
* @param day - The weekday to match (e.g., `'Monday'`, `'Friday'`).
|
|
373
|
-
* @param options - Absolute date range (e.g. `'2025-06-30'`, ` new Date()`, `new Chronos()` etc.) and output format (local with timezone or utc).
|
|
374
|
-
* @returns Array of ISO date strings in the specified format. Returns empty array if no matches in the range.
|
|
375
|
-
*
|
|
376
|
-
* @example
|
|
377
|
-
* Chronos.getDatesForDay('Monday', {
|
|
378
|
-
* from: '2025-05-28',
|
|
379
|
-
* to: '2025-06-30',
|
|
380
|
-
* format: 'local'
|
|
381
|
-
* });
|
|
382
|
-
* //=> [ '2025-01-06T...', '2025-01-13T...', ... ]
|
|
383
|
-
*/
|
|
384
|
-
getDatesForDay(day: WeekDay, options?: DateRangeOptions): string[];
|
|
385
|
-
/**
|
|
386
|
-
* * Returns ISO date strings for each occurrence of a weekday.
|
|
387
|
-
*
|
|
388
|
-
* @param day - The weekday to match (e.g., `'Wednesday'`, `'Sunday'`).
|
|
389
|
-
* @param options - Relative range (e.g., 7 days, 4 weeks) or Absolute date range and output format.
|
|
390
|
-
* @returns Array of ISO date strings in the specified format.
|
|
391
|
-
*/
|
|
392
|
-
getDatesForDay(day: WeekDay, options?: WeekdayOptions): string[];
|
|
393
|
-
/**
|
|
394
|
-
* * Returns earliest Chronos
|
|
395
|
-
* @param dates Date inputs.
|
|
396
|
-
*/
|
|
397
|
-
min(...dates: ChronosInput[]): Chronos;
|
|
398
|
-
/**
|
|
399
|
-
* * Returns latest Chronos
|
|
400
|
-
* @param dates Date inputs.
|
|
401
|
-
*/
|
|
402
|
-
max(...dates: ChronosInput[]): Chronos;
|
|
403
|
-
/**
|
|
404
|
-
* * Checks if the year in the date string or year (from 0 - 9999) is a leap year.
|
|
405
|
-
* - A year is a leap year if it is divisible by 4, but not divisible by 100, unless it is also divisible by 400.
|
|
406
|
-
* - For example, 2000 and 2400 are leap years, but 1900 and 2100 are not.
|
|
407
|
-
*
|
|
408
|
-
* @description
|
|
409
|
-
* This method accepts different types of date inputs and extracts the year to check if it's a leap year.
|
|
410
|
-
* If the provided date is a `number`, it will be treated as a year (must be a valid year from 0 to 9999).
|
|
411
|
-
* If the year is out of this range (negative or larger than 9999), it will be treated as a Unix timestamp.
|
|
412
|
-
* If the provided date is a string or a `Date` object, it will be parsed and the year will be extracted.
|
|
413
|
-
* If a `Chronos` instance is passed, the year will be directly accessed from the instance.
|
|
414
|
-
*
|
|
415
|
-
* @param date - A `number` (year or Unix timestamp), `string`, `Date`, or `Chronos` instance representing a date.
|
|
416
|
-
* @returns `true` if the year is a leap year, `false` otherwise.
|
|
417
|
-
*/
|
|
418
|
-
isLeapYear(date: ChronosInput): boolean;
|
|
419
|
-
/**
|
|
420
|
-
* * Checks if the given value is a valid `Date` object.
|
|
421
|
-
* - A value is considered valid if it is an instance of the built-in `Date` class.
|
|
422
|
-
* - This does not check whether the date itself is valid (e.g., `new Date('invalid')`).
|
|
423
|
-
* @param value - The value to test.
|
|
424
|
-
* @returns `true` if the value is a valid Date object, otherwise `false`.
|
|
425
|
-
*/
|
|
426
|
-
isValidDate(value: unknown): value is Date;
|
|
427
|
-
/**
|
|
428
|
-
* * Checks if the given value is a valid date string.
|
|
429
|
-
* - A value is considered a valid date string if it is a string and can be parsed by `Date.parse()`.
|
|
430
|
-
* - This uses the native JavaScript date parser internally.
|
|
431
|
-
* @param value - The value to test.
|
|
432
|
-
* @returns `true` if the value is a valid date string, otherwise `false`.
|
|
433
|
-
*/
|
|
434
|
-
isDateString(value: unknown): value is string;
|
|
435
|
-
/**
|
|
436
|
-
* * Checks if the given value is an instance of `Chronos`.
|
|
437
|
-
* - Useful for verifying Chronos objects in type guards or validations.
|
|
438
|
-
* @param value - The value to test.
|
|
439
|
-
* @returns `true` if the value is an instance of `Chronos`, otherwise `false`.
|
|
440
|
-
*/
|
|
441
|
-
isValidChronos(value: unknown): value is Chronos;
|
|
442
|
-
}
|
|
443
157
|
/** Names of time-zones */
|
|
444
158
|
export type TimeZone = keyof typeof TIME_ZONES;
|
|
445
159
|
/** Positive UTC hours */
|
|
@@ -639,9 +353,9 @@ export interface DateLike {
|
|
|
639
353
|
* * Options for `Chronos` _static_ method `with()`
|
|
640
354
|
*
|
|
641
355
|
* @remarks
|
|
642
|
-
* -
|
|
356
|
+
* - Should provide at least one property.
|
|
643
357
|
*/
|
|
644
|
-
export type ChronosWithOptions =
|
|
358
|
+
export type ChronosWithOptions = Partial<{
|
|
645
359
|
/** The full year (e.g., 2025). Years 0–99 are interpreted as 1900–1999. */
|
|
646
360
|
year: number;
|
|
647
361
|
/** Month number from 1 (January) to 12 (December). */
|
|
@@ -656,5 +370,4 @@ export type ChronosWithOptions = RequireAtLeast<{
|
|
|
656
370
|
second: Enumerate<60>;
|
|
657
371
|
/** Milliseconds of the second, from 0 to 999. */
|
|
658
372
|
millisecond: Milliseconds;
|
|
659
|
-
}
|
|
660
|
-
export {};
|
|
373
|
+
}>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ConvertedObject, ConvertObjectOptions, GenericObject, NumericDotKey } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* * Converts the values of specified keys in an object to numbers.
|
|
4
4
|
* * Supports nested objects using dot-notation keys.
|
|
@@ -9,10 +9,7 @@ import type { DotNotationKey, GenericObject, Numberified, Stringified } from './
|
|
|
9
9
|
* - `convertTo`: The target type, either `"string"` or `"number"`.
|
|
10
10
|
* @returns The modified object with the converted values as `"string"` or `"number"`.
|
|
11
11
|
*/
|
|
12
|
-
export declare function convertObjectValues<T extends GenericObject, C extends 'string' | 'number'>(data: T, options:
|
|
13
|
-
keys: DotNotationKey<T>[];
|
|
14
|
-
convertTo: C;
|
|
15
|
-
}): C extends 'string' ? Stringified<T> : C extends 'number' ? Numberified<T> : never;
|
|
12
|
+
export declare function convertObjectValues<T extends GenericObject, Key extends NumericDotKey<T>, C extends 'string' | 'number'>(data: T, options: ConvertObjectOptions<T, Key, C>): ConvertedObject<T, Key, C>;
|
|
16
13
|
/**
|
|
17
14
|
* * Converts the values of specified keys in an array of objects to numbers or strings.
|
|
18
15
|
* * Supports nested objects using dot-notation keys.
|
|
@@ -23,10 +20,7 @@ export declare function convertObjectValues<T extends GenericObject, C extends '
|
|
|
23
20
|
* - `convertTo`: The target type, either `"string"` or `"number"`.
|
|
24
21
|
* @returns The modified array of objects with the converted values as `"string"` or `"number"`.
|
|
25
22
|
*/
|
|
26
|
-
export declare function convertObjectValues<T extends GenericObject, C extends 'string' | 'number'>(data: T
|
|
27
|
-
keys: DotNotationKey<T>[];
|
|
28
|
-
convertTo: C;
|
|
29
|
-
}): C extends 'string' ? Stringified<T>[] : C extends 'number' ? Numberified<T>[] : never;
|
|
23
|
+
export declare function convertObjectValues<T extends GenericObject, Key extends NumericDotKey<T>, C extends 'string' | 'number'>(data: Array<T>, options: ConvertObjectOptions<T, Key, C>): Array<ConvertedObject<T, Key, C>>;
|
|
30
24
|
/**
|
|
31
25
|
* * Pick specific fields from an object and create a new object with specified fields.
|
|
32
26
|
*
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { AdvancedTypes, NormalPrimitive } from '../types/index';
|
|
1
|
+
import type { AdvancedTypes, NormalPrimitive, ValidArray } from '../types/index';
|
|
2
|
+
import type { Prettify } from '../utils/types';
|
|
2
3
|
/** - Generic object with `unknown` value */
|
|
3
4
|
export type StrictObject = Record<string, unknown>;
|
|
4
5
|
/** - Generic object but with `any` value */
|
|
@@ -106,12 +107,18 @@ export interface SanitizeOptions<T> {
|
|
|
106
107
|
*/
|
|
107
108
|
requiredKeys?: '*' | DotNotationKey<T>[];
|
|
108
109
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
110
|
+
export interface ConvertObjectOptions<T extends GenericObject, Key extends NumericDotKey<T>, C extends 'string' | 'number'> {
|
|
111
|
+
/** Array of keys (properties) to convert to `number` or `string` */
|
|
112
|
+
keys: ValidArray<Key>;
|
|
113
|
+
/** Convert selected keys to target type: `number` or `string` */
|
|
114
|
+
convertTo: C;
|
|
115
|
+
}
|
|
116
|
+
/** Transform a single property */
|
|
117
|
+
type ConvertProp<V, C extends 'string' | 'number'> = C extends 'string' ? V extends string | number ? string : V : C extends 'number' ? V extends string ? number : V : V;
|
|
118
|
+
/** Extract sub-keys after prefix like `"props."` */
|
|
119
|
+
type SubKey<S extends string, P extends string> = S extends `${P}.${infer Rest}` ? Rest : never;
|
|
120
|
+
/** Transformed shape of the return type of `convertObjectValues` */
|
|
121
|
+
export type ConvertedObject<T, Keys extends string, C extends 'string' | 'number'> = Prettify<{
|
|
122
|
+
[K in Extract<keyof T, string>]: K extends Keys ? ConvertProp<T[K], C> : T[K] extends AdvancedTypes ? T[K] : T[K] extends GenericObject ? ConvertedObject<T[K], SubKey<Keys, K>, C> : T[K];
|
|
123
|
+
}>;
|
|
117
124
|
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -53,11 +53,12 @@ export class Currency {
|
|
|
53
53
|
const url = `https://api.frankfurter.app/latest?amount=${this.#amount}&from=${this.#code}`;
|
|
54
54
|
try {
|
|
55
55
|
const res = await fetch(url, { redirect: 'error' });
|
|
56
|
-
if (!res.ok)
|
|
56
|
+
if (!res.ok) {
|
|
57
57
|
throw new Error(`FrankFurter Error: ${res.status}. "${res.statusText}"`);
|
|
58
|
+
}
|
|
58
59
|
const data = await res.json();
|
|
59
60
|
if (!data.rates?.[to]) {
|
|
60
|
-
throw new Error(`Currency "${to}" not found in FrankFurter Database!`);
|
|
61
|
+
throw new Error(`Currency "${to}" not allowed or not found in FrankFurter Database!`);
|
|
61
62
|
}
|
|
62
63
|
return data.rates[to] / this.#amount;
|
|
63
64
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nhb-toolbox",
|
|
3
|
-
"version": "4.20.
|
|
4
|
-
"description": "A versatile collection of smart, efficient, and reusable utility functions and
|
|
3
|
+
"version": "4.20.50",
|
|
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",
|
|
7
7
|
"types": "dist/dts/index.d.ts",
|
|
@@ -85,11 +85,17 @@
|
|
|
85
85
|
"dayjs",
|
|
86
86
|
"pluralize",
|
|
87
87
|
"pluralizer",
|
|
88
|
+
"singularize",
|
|
89
|
+
"singularizer",
|
|
90
|
+
"verbalize",
|
|
91
|
+
"verbalizer",
|
|
88
92
|
"date",
|
|
89
93
|
"day",
|
|
90
94
|
"chalk",
|
|
91
95
|
"console",
|
|
92
96
|
"style",
|
|
97
|
+
"stylog",
|
|
98
|
+
"log styler",
|
|
93
99
|
"log",
|
|
94
100
|
"status-codes",
|
|
95
101
|
"http-status",
|