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