typeshi 1.7.2 → 1.7.3

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.
@@ -9,11 +9,11 @@
9
9
  * @property {string} UNIX - Unix format (milliseconds since epoch)
10
10
  */
11
11
  export declare enum DateFormatEnum {
12
- /**ISO format (YYYY-MM-DDTHH:mm:ss.sssZ) */
12
+ /**ISO format (e.g., "2025-04-16T00:00:00.000Z") */
13
13
  ISO = "ISO",
14
- /**UTC format (YYYY-MM-DDTHH:mm:ss.sssZ) */
14
+ /**UTC format (e.g., "Sun, 31 Dec 1899 00:00:00 GMT") */
15
15
  UTC = "UTC",
16
- /**Local format (YYYY-MM-DDTHH:mm:ss.sssZ) */
16
+ /**Locale format (e.g., "4/21/2025, 4:22:45 PM") */
17
17
  LOCALE = "LOCALE",
18
18
  /** ```/\d{13}/``` if milliseconds, ```/\d{10}/``` if seconds */
19
19
  UNIX = "UNIX"
@@ -34,20 +34,31 @@ export declare enum TimeUnitEnum {
34
34
  DAYS = "days"
35
35
  }
36
36
  /**
37
- * `re = /^\d{4}-\d{2}-\d{2}$/`
38
- * @description Regular expression pattern for ISO date format (YYYY-MM-DD)
37
+ * `re = /\d{4}(-|\/)\d{2}(-|\/)\d{2}(T\d{2}:\d{2}:\d{2}(.\d{3})Z)?/`
38
+ * @description Regular expression pattern for ISO date format (YYYY-MM-DD or YYYY/MM/DD) + optional time (THH:mm:ss.sssZ)
39
39
  * @example "2025-04-16"
40
40
  * */
41
41
  export declare const ISO_PATTERN: RegExp;
42
+ /**
43
+ * `re = /(Mon|Tue|Wed|Thu|Fri|Sat|Sun), \d{2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{4}( \d{2}:\d{2}:\d{2} GMT)?/`
44
+ * @description Regular expression pattern for UTC date format (e.g., "Sun, 31 Dec 1899 00:00:00 GMT")
45
+ */
46
+ export declare const UTC_PATTERN: RegExp;
47
+ /**
48
+ * `re = /\d{1,2}[-/]\d{1,2}[-/]\d{4}(, \d{1,2}:\d{2}:\d{2} (AM|PM))?/`
49
+ * @description Regular expression pattern for Locale date format (M/D/YYYY or M-D-YYYY) + optional time (hh:mm:ss AM/PM)
50
+ * @example "4/21/2025, 4:22:45 PM"
51
+ */
52
+ export declare const LOCALE_PATTERN: RegExp;
42
53
  /**
43
54
  * - defaultValue: string = `"en-US"`
44
- * @description set as first param, locales, in {@link Date}.toLocaleString(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions)
55
+ * @description set as first param, locales, in {@link Date}`.toLocaleString(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions)`
45
56
  * @reference ~\node_modules\typescript\lib\lib.es2020.date.d.ts @see {@link Date}
46
57
  */
47
58
  export declare const DEFAULT_LOCALE = "en-US";
48
59
  /**
49
60
  * - defaultValue: string = `"America/Los_Angeles"`
50
- * @description set as second param, options, in {@link Date}.toLocaleString(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions)
61
+ * @description set as second param, options, in {@link Date}`.toLocaleString(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions)`
51
62
  * @reference ~\node_modules\typescript\lib\lib.es2020.date.d.ts @see {@link Date}
52
63
  */
53
64
  export declare const DEFAULT_TIMEZONE = "America/Los_Angeles";
@@ -81,7 +92,7 @@ export declare function getUnixTimestampFromISO(dateString: string): number | nu
81
92
  * @param dateStr Date string in locale format (e.g., '4/21/2025, 4:22:45 PM')
82
93
  * @returns {Date} **`date`** {@link Date} object
83
94
  */
84
- export declare function parseLocaleStringToDate(dateStr: string): Date;
95
+ export declare function localeStringToDate(dateStr: string): Date;
85
96
  /**
86
97
  * @description Gets the current date and time in Pacific Time in Locale format
87
98
  * @returns {string} The current date and time in Pacific Time in Locale format
@@ -96,19 +107,57 @@ export declare function getCurrentPacificTime(): string;
96
107
  export declare function toPacificTime(initialDateString: string): string;
97
108
  export declare const Milliseconds: {
98
109
  readonly from: {
110
+ /**
111
+ * @param n `number`
112
+ * @returns `n * (1000 * 60 * 60 * 24)` number of milliseconds in `n` days
113
+ */
114
+ readonly days: (n: number) => number;
115
+ /**
116
+ * @param n `number`
117
+ * @returns `n * (1000 * 60 * 60)` number of milliseconds in `n` hours
118
+ */
99
119
  readonly hours: (n: number) => number;
120
+ /**
121
+ * @param n `number`
122
+ * @returns `n * (1000 * 60)` number of milliseconds in `n` minutes
123
+ */
100
124
  readonly minutes: (n: number) => number;
125
+ /**
126
+ * @param n `number`
127
+ * @returns `n * (1000)` number of milliseconds in `n` seconds
128
+ */
101
129
  readonly seconds: (n: number) => number;
102
130
  /**
103
131
  * @param d `Date` object
104
- * @returns `number` milliseconds since epoch
132
+ * @returns `number` = `d.getTime()` = milliseconds since epoch
105
133
  */
106
134
  readonly date: (d: Date) => number;
107
- readonly localeString: (s: string) => number | null;
135
+ /**
136
+ * @param s `string` date string to pass into Date Constructor (e.g. ISO, UTC, Locale, etc.)
137
+ * @returns `number` milliseconds since epoch or `null` if invalid (i.e. Date constructor can't parse it)
138
+ */
139
+ readonly string: (s: string) => number | null;
108
140
  };
109
141
  readonly to: {
142
+ /**
143
+ * @param n `number`
144
+ * @returns `number` days in `n` milliseconds
145
+ */
146
+ readonly days: (n: number) => number;
147
+ /**
148
+ * @param n `number`
149
+ * @returns `number` hours in `n` milliseconds
150
+ */
110
151
  readonly hours: (n: number) => number;
152
+ /**
153
+ * @param n `number`
154
+ * @returns `number` minutes in `n` milliseconds
155
+ */
111
156
  readonly minutes: (n: number) => number;
157
+ /**
158
+ * @param n `number`
159
+ * @returns `number` seconds in `n` milliseconds
160
+ */
112
161
  readonly seconds: (n: number) => number;
113
162
  /**
114
163
  * interprets `n` as milliseconds since epoch
@@ -117,11 +166,12 @@ export declare const Milliseconds: {
117
166
  */
118
167
  readonly date: (n: number) => Date;
119
168
  /**
120
- * @param n `number` milliseconds since epoch
121
- * @param locale `string` default = `'en-US'`
122
- * @param timeZone `string` default = `'America/Los_Angeles'`
123
- * @returns `string` locale date string
169
+ * @param n `number`
170
+ * @param format {@link DateFormatEnum} default = {@link DateFormatEnum.ISO}
171
+ * @param locale `string` default = `'en-US'` (only used if format = {@link DateFormatEnum.LOCALE})
172
+ * @param timeZone `string` default = `'America/Los_Angeles'` (only used if format = {@link DateFormatEnum.LOCALE})
173
+ * @returns `string` formatted date string or empty string if error
124
174
  */
125
- readonly localeString: (n: number, locale?: string, timeZone?: string) => string;
175
+ readonly string: (n: number, format?: DateFormatEnum, locale?: string, timeZone?: string) => string;
126
176
  };
127
177
  };
@@ -3,11 +3,11 @@
3
3
  * @file src/utils/io/dateTime.ts
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Milliseconds = exports.DEFAULT_TIMEZONE = exports.DEFAULT_LOCALE = exports.ISO_PATTERN = exports.TimeUnitEnum = exports.DateFormatEnum = void 0;
6
+ exports.Milliseconds = exports.DEFAULT_TIMEZONE = exports.DEFAULT_LOCALE = exports.LOCALE_PATTERN = exports.UTC_PATTERN = exports.ISO_PATTERN = exports.TimeUnitEnum = exports.DateFormatEnum = void 0;
7
7
  exports.getDateFromUnixTimestamp = getDateFromUnixTimestamp;
8
8
  exports.calculateDifferenceOfDateStrings = calculateDifferenceOfDateStrings;
9
9
  exports.getUnixTimestampFromISO = getUnixTimestampFromISO;
10
- exports.parseLocaleStringToDate = parseLocaleStringToDate;
10
+ exports.localeStringToDate = localeStringToDate;
11
11
  exports.getCurrentPacificTime = getCurrentPacificTime;
12
12
  exports.toPacificTime = toPacificTime;
13
13
  /**
@@ -19,11 +19,11 @@ exports.toPacificTime = toPacificTime;
19
19
  */
20
20
  var DateFormatEnum;
21
21
  (function (DateFormatEnum) {
22
- /**ISO format (YYYY-MM-DDTHH:mm:ss.sssZ) */
22
+ /**ISO format (e.g., "2025-04-16T00:00:00.000Z") */
23
23
  DateFormatEnum["ISO"] = "ISO";
24
- /**UTC format (YYYY-MM-DDTHH:mm:ss.sssZ) */
24
+ /**UTC format (e.g., "Sun, 31 Dec 1899 00:00:00 GMT") */
25
25
  DateFormatEnum["UTC"] = "UTC";
26
- /**Local format (YYYY-MM-DDTHH:mm:ss.sssZ) */
26
+ /**Locale format (e.g., "4/21/2025, 4:22:45 PM") */
27
27
  DateFormatEnum["LOCALE"] = "LOCALE";
28
28
  /** ```/\d{13}/``` if milliseconds, ```/\d{10}/``` if seconds */
29
29
  DateFormatEnum["UNIX"] = "UNIX";
@@ -46,20 +46,31 @@ var TimeUnitEnum;
46
46
  TimeUnitEnum["DAYS"] = "days";
47
47
  })(TimeUnitEnum || (exports.TimeUnitEnum = TimeUnitEnum = {}));
48
48
  /**
49
- * `re = /^\d{4}-\d{2}-\d{2}$/`
50
- * @description Regular expression pattern for ISO date format (YYYY-MM-DD)
49
+ * `re = /\d{4}(-|\/)\d{2}(-|\/)\d{2}(T\d{2}:\d{2}:\d{2}(.\d{3})Z)?/`
50
+ * @description Regular expression pattern for ISO date format (YYYY-MM-DD or YYYY/MM/DD) + optional time (THH:mm:ss.sssZ)
51
51
  * @example "2025-04-16"
52
52
  * */
53
- exports.ISO_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
53
+ exports.ISO_PATTERN = /\d{4}[-/]\d{2}[-/]\d{2}(T\d{2}:\d{2}:\d{2}(.\d{3})Z)?/;
54
+ /**
55
+ * `re = /(Mon|Tue|Wed|Thu|Fri|Sat|Sun), \d{2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{4}( \d{2}:\d{2}:\d{2} GMT)?/`
56
+ * @description Regular expression pattern for UTC date format (e.g., "Sun, 31 Dec 1899 00:00:00 GMT")
57
+ */
58
+ exports.UTC_PATTERN = /(Mon|Tue|Wed|Thu|Fri|Sat|Sun), \d{2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{4}( \d{2}:\d{2}:\d{2} GMT)?/;
59
+ /**
60
+ * `re = /\d{1,2}[-/]\d{1,2}[-/]\d{4}(, \d{1,2}:\d{2}:\d{2} (AM|PM))?/`
61
+ * @description Regular expression pattern for Locale date format (M/D/YYYY or M-D-YYYY) + optional time (hh:mm:ss AM/PM)
62
+ * @example "4/21/2025, 4:22:45 PM"
63
+ */
64
+ exports.LOCALE_PATTERN = /\d{1,2}[-/]\d{1,2}[-/]\d{4}(, \d{1,2}:\d{2}:\d{2} (AM|PM))?/; // e.g., 4/21/2025, 4:22:45 PM
54
65
  /**
55
66
  * - defaultValue: string = `"en-US"`
56
- * @description set as first param, locales, in {@link Date}.toLocaleString(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions)
67
+ * @description set as first param, locales, in {@link Date}`.toLocaleString(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions)`
57
68
  * @reference ~\node_modules\typescript\lib\lib.es2020.date.d.ts @see {@link Date}
58
69
  */
59
70
  exports.DEFAULT_LOCALE = 'en-US';
60
71
  /**
61
72
  * - defaultValue: string = `"America/Los_Angeles"`
62
- * @description set as second param, options, in {@link Date}.toLocaleString(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions)
73
+ * @description set as second param, options, in {@link Date}`.toLocaleString(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions)`
63
74
  * @reference ~\node_modules\typescript\lib\lib.es2020.date.d.ts @see {@link Date}
64
75
  */
65
76
  exports.DEFAULT_TIMEZONE = 'America/Los_Angeles';
@@ -119,13 +130,13 @@ function calculateDifferenceOfDateStrings(ds1, ds2 = getCurrentPacificTime(), un
119
130
  case TimeUnitEnum.MILLISECONDS:
120
131
  return diffInMs;
121
132
  case TimeUnitEnum.SECONDS:
122
- return Math.floor(diffInMs / 1000);
133
+ return exports.Milliseconds.from.seconds(diffInMs);
123
134
  case TimeUnitEnum.MINUTES:
124
- return Math.floor(diffInMs / (1000 * 60));
135
+ return exports.Milliseconds.from.minutes(diffInMs);
125
136
  case TimeUnitEnum.HOURS:
126
- return Math.floor(diffInMs / (1000 * 60 * 60));
137
+ return exports.Milliseconds.from.hours(diffInMs);
127
138
  case TimeUnitEnum.DAYS:
128
- return Math.floor(diffInMs / (1000 * 60 * 60 * 24));
139
+ return exports.Milliseconds.from.days(diffInMs);
129
140
  default:
130
141
  console.error('Invalid time unit specified. Use TimeUnitEnum.MILLISECONDS, TimeUnitEnum.SECONDS, TimeUnitEnum.MINUTES, TimeUnitEnum.HOURS, or TimeUnitEnum.DAYS');
131
142
  return null;
@@ -160,7 +171,7 @@ function getUnixTimestampFromISO(dateString) {
160
171
  * @param dateStr Date string in locale format (e.g., '4/21/2025, 4:22:45 PM')
161
172
  * @returns {Date} **`date`** {@link Date} object
162
173
  */
163
- function parseLocaleStringToDate(dateStr) {
174
+ function localeStringToDate(dateStr) {
164
175
  try {
165
176
  const [datePart, timeWithPeriod] = dateStr.split(', ');
166
177
  const [month, day, year] = datePart.split('/').map(Number);
@@ -177,7 +188,7 @@ function parseLocaleStringToDate(dateStr) {
177
188
  return new Date(year, month - 1, day, hours, minutes, seconds);
178
189
  }
179
190
  catch (error) {
180
- throw new Error(`Failed to parse date string: ${dateStr}. Expected format: 'M/D/YYYY, h:mm:ss AM/PM'`);
191
+ throw new Error(`Failed to parse date string: '${dateStr}'. Expected format: 'MM/DD/YYYY, hh:mm:ss AM/PM'`);
181
192
  }
182
193
  }
183
194
  /**
@@ -202,40 +213,85 @@ function toPacificTime(initialDateString) {
202
213
  }
203
214
  exports.Milliseconds = {
204
215
  from: {
216
+ /**
217
+ * @param n `number`
218
+ * @returns `n * (1000 * 60 * 60 * 24)` number of milliseconds in `n` days
219
+ */
220
+ days: (n) => {
221
+ return n * (1000 * 60 * 60 * 24);
222
+ },
223
+ /**
224
+ * @param n `number`
225
+ * @returns `n * (1000 * 60 * 60)` number of milliseconds in `n` hours
226
+ */
205
227
  hours: (n) => {
206
228
  return n * (1000 * 60 * 60);
207
229
  },
230
+ /**
231
+ * @param n `number`
232
+ * @returns `n * (1000 * 60)` number of milliseconds in `n` minutes
233
+ */
208
234
  minutes: (n) => {
209
235
  return n * (1000 * 60);
210
236
  },
237
+ /**
238
+ * @param n `number`
239
+ * @returns `n * (1000)` number of milliseconds in `n` seconds
240
+ */
211
241
  seconds: (n) => {
212
242
  return n * (1000);
213
243
  },
214
244
  /**
215
245
  * @param d `Date` object
216
- * @returns `number` milliseconds since epoch
246
+ * @returns `number` = `d.getTime()` = milliseconds since epoch
217
247
  */
218
248
  date: (d) => {
219
249
  return d.getTime();
220
250
  },
221
- localeString: (s) => {
251
+ /**
252
+ * @param s `string` date string to pass into Date Constructor (e.g. ISO, UTC, Locale, etc.)
253
+ * @returns `number` milliseconds since epoch or `null` if invalid (i.e. Date constructor can't parse it)
254
+ */
255
+ string: (s) => {
222
256
  try {
223
- const date = parseLocaleStringToDate(s);
257
+ const date = new Date(s);
258
+ if (isNaN(date.getTime())) {
259
+ throw new Error(`Invalid date string: '${s}'`);
260
+ }
224
261
  return date.getTime();
225
262
  }
226
263
  catch (error) {
227
- console.error(error);
264
+ console.error(`Failed to parse date string: '${s}'`, error);
228
265
  return null;
229
266
  }
230
- }
267
+ },
231
268
  },
232
269
  to: {
270
+ /**
271
+ * @param n `number`
272
+ * @returns `number` days in `n` milliseconds
273
+ */
274
+ days: (n) => {
275
+ return n / (1000 * 60 * 60 * 24);
276
+ },
277
+ /**
278
+ * @param n `number`
279
+ * @returns `number` hours in `n` milliseconds
280
+ */
233
281
  hours: (n) => {
234
282
  return n / (1000 * 60 * 60);
235
283
  },
284
+ /**
285
+ * @param n `number`
286
+ * @returns `number` minutes in `n` milliseconds
287
+ */
236
288
  minutes: (n) => {
237
289
  return n / (1000 * 60);
238
290
  },
291
+ /**
292
+ * @param n `number`
293
+ * @returns `number` seconds in `n` milliseconds
294
+ */
239
295
  seconds: (n) => {
240
296
  return n / (1000);
241
297
  },
@@ -248,14 +304,29 @@ exports.Milliseconds = {
248
304
  return new Date(n);
249
305
  },
250
306
  /**
251
- * @param n `number` milliseconds since epoch
252
- * @param locale `string` default = `'en-US'`
253
- * @param timeZone `string` default = `'America/Los_Angeles'`
254
- * @returns `string` locale date string
307
+ * @param n `number`
308
+ * @param format {@link DateFormatEnum} default = {@link DateFormatEnum.ISO}
309
+ * @param locale `string` default = `'en-US'` (only used if format = {@link DateFormatEnum.LOCALE})
310
+ * @param timeZone `string` default = `'America/Los_Angeles'` (only used if format = {@link DateFormatEnum.LOCALE})
311
+ * @returns `string` formatted date string or empty string if error
255
312
  */
256
- localeString: (n, locale = exports.DEFAULT_LOCALE, timeZone = exports.DEFAULT_TIMEZONE) => {
313
+ string: (n, format = DateFormatEnum.ISO, locale = exports.DEFAULT_LOCALE, timeZone = exports.DEFAULT_TIMEZONE) => {
257
314
  const date = new Date(n);
258
- return date.toLocaleString(locale, { timeZone });
259
- }
315
+ if (isNaN(date.getTime())) {
316
+ console.error(`Invalid milliseconds value: '${n}'`);
317
+ return ``;
318
+ }
319
+ switch (format) {
320
+ case DateFormatEnum.ISO:
321
+ return date.toISOString();
322
+ case DateFormatEnum.UTC:
323
+ return date.toUTCString();
324
+ case DateFormatEnum.LOCALE:
325
+ return date.toLocaleString(locale, { timeZone });
326
+ default:
327
+ console.error(`Invalid date format specified: '${format}'.`, `Use DateFormatEnum.ISO, DateFormatEnum.UTC, or DateFormatEnum.LOCALE`);
328
+ return ``;
329
+ }
330
+ },
260
331
  },
261
332
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typeshi",
3
- "version": "1.7.2",
3
+ "version": "1.7.3",
4
4
  "description": "TypeScript utility modules",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",