typeshi 2.0.2 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/argumentValidation.d.ts +1 -1
- package/dist/utils/argumentValidation.js +32 -17
- package/dist/utils/io/dateTime.d.ts +37 -20
- package/dist/utils/io/dateTime.js +142 -120
- package/dist/utils/io/logging.d.ts +4 -0
- package/dist/utils/io/logging.js +20 -14
- package/dist/utils/io/reading.d.ts +8 -132
- package/dist/utils/io/reading.js +111 -641
- package/dist/utils/io/types/Io.d.ts +1 -24
- package/dist/utils/io/types/Io.js +3 -0
- package/dist/utils/io/writing.d.ts +4 -5
- package/dist/utils/io/writing.js +10 -11
- package/dist/utils/regex/Str.d.ts +2 -1
- package/dist/utils/regex/Str.js +2 -1
- package/dist/utils/regex/cleaning.d.ts +1 -55
- package/dist/utils/regex/cleaning.js +4 -130
- package/dist/utils/regex/configureParameters.d.ts +4 -5
- package/dist/utils/regex/configureParameters.js +9 -15
- package/dist/utils/regex/email.js +1 -6
- package/dist/utils/regex/entity.js +4 -4
- package/dist/utils/regex/misc.d.ts +4 -1
- package/dist/utils/regex/misc.js +4 -1
- package/dist/utils/regex/phone.js +7 -3
- package/dist/utils/regex/stringOperations.js +7 -10
- package/dist/utils/regex/types/StringOptions.TypeGuards.d.ts +1 -9
- package/dist/utils/regex/types/StringOptions.TypeGuards.js +4 -15
- package/dist/utils/regex/types/StringOptions.d.ts +0 -52
- package/dist/utils/typeValidation.d.ts +18 -30
- package/dist/utils/typeValidation.js +39 -54
- package/package.json +1 -1
|
@@ -152,7 +152,7 @@ export declare function objectArgument(source: string,
|
|
|
152
152
|
labeledArgs: ObjectArgumentOptions | {
|
|
153
153
|
[label: string]: any | ((value: any) => boolean);
|
|
154
154
|
}, allowEmpty?: boolean): void;
|
|
155
|
-
type EnumObject = Record<string, string> | Record<string, number>;
|
|
155
|
+
type EnumObject = Record<string, string> | Record<string, string | number>;
|
|
156
156
|
/**
|
|
157
157
|
* Object with 2 entries:
|
|
158
158
|
* 1. `valueLabel` mapped to `valueToCheck`
|
|
@@ -52,6 +52,7 @@ exports.enumArgument = enumArgument;
|
|
|
52
52
|
exports.existingPathArgument = existingPathArgument;
|
|
53
53
|
/**
|
|
54
54
|
* @file src/utils/argumentValidation.ts
|
|
55
|
+
* @note these functions can be useful for sanity checks
|
|
55
56
|
* @description moved the content of parameter type checks at the start of
|
|
56
57
|
* functions to here. use these when you want your function to throw a fit when
|
|
57
58
|
* it receives bad input.
|
|
@@ -61,8 +62,6 @@ exports.existingPathArgument = existingPathArgument;
|
|
|
61
62
|
* - maybe add a configurable value that the validation functions should return if the validation test fails
|
|
62
63
|
* - change the validation functions such that they return the validated value, if possible?
|
|
63
64
|
* - or maybe have them return boolean type predicates ?
|
|
64
|
-
* - -> maybe have to make a class
|
|
65
|
-
* - research the thingy where a type is after the function name and before parens
|
|
66
65
|
*/
|
|
67
66
|
const typeValidation_1 = require("./typeValidation");
|
|
68
67
|
const setupLog_1 = require("../config/setupLog");
|
|
@@ -98,6 +97,9 @@ function stringArgument(source, arg2, value) {
|
|
|
98
97
|
label = keys[0];
|
|
99
98
|
value = arg2[label];
|
|
100
99
|
}
|
|
100
|
+
else if ((0, typeValidation_1.isNonEmptyString)(arg2)) {
|
|
101
|
+
label = arg2;
|
|
102
|
+
}
|
|
101
103
|
if (!(0, typeValidation_1.isNonEmptyString)(value)) {
|
|
102
104
|
let msg = [`${source} Invalid argument: '${label}'`,
|
|
103
105
|
`Expected '${label}' to be: non-empty string`,
|
|
@@ -229,6 +231,9 @@ function numberArgument(source, arg2, arg3, requireInteger = false) {
|
|
|
229
231
|
requireInteger = arg3;
|
|
230
232
|
}
|
|
231
233
|
}
|
|
234
|
+
else if ((0, typeValidation_1.isNonEmptyString)(arg2)) {
|
|
235
|
+
label = arg2;
|
|
236
|
+
}
|
|
232
237
|
if (typeof value !== 'number' || isNaN(value)) {
|
|
233
238
|
let msg = [`${source} Invalid argument: '${label}'`,
|
|
234
239
|
`Expected '${label}' to be: number`,
|
|
@@ -570,12 +575,20 @@ allowEmpty) {
|
|
|
570
575
|
// reached end -> value is a valid object
|
|
571
576
|
return;
|
|
572
577
|
}
|
|
573
|
-
|
|
578
|
+
// @TODO be more rigorous and make check String(v) mapped to k
|
|
579
|
+
function isStringEnum(value) {
|
|
574
580
|
return ((0, typeValidation_1.isObject)(value)
|
|
575
|
-
&& Object.
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
581
|
+
&& Object.values(value).every(v => typeof v === 'string'));
|
|
582
|
+
}
|
|
583
|
+
// because enums also store String(v) mapped to k
|
|
584
|
+
// @TODO be more rigorous and make check String(v) mapped to k
|
|
585
|
+
function isNumberEnum(value) {
|
|
586
|
+
return ((0, typeValidation_1.isObject)(value) && Object.keys(value).every(k => (0, typeValidation_1.isNumeric)(k, true, true)
|
|
587
|
+
? (0, typeValidation_1.isNonEmptyString)(value[k])
|
|
588
|
+
: !Number.isNaN(value[k])));
|
|
589
|
+
}
|
|
590
|
+
function isEnumObject(value) {
|
|
591
|
+
return isStringEnum(value) || isNumberEnum(value);
|
|
579
592
|
}
|
|
580
593
|
function isEnumArgumentOptions(value) {
|
|
581
594
|
if (!(0, typeValidation_1.isObject)(value)) {
|
|
@@ -654,7 +667,7 @@ function enumArgument(source, arg2, value, enumLabel, enumObject) {
|
|
|
654
667
|
if (!enumLabel) {
|
|
655
668
|
let msg = [`${source} -> ${vSource} Invalid parameter: arg2 as EnumArgumentOptions`,
|
|
656
669
|
`EnumArgumentOptions does not contain an entry with a value that is an EnumObject or isEnumFunction`,
|
|
657
|
-
`Expected arg2 to have single entry of format [label: string]: EnumObject | Function`
|
|
670
|
+
`Expected: arg2 to have single entry of format [label: string]: EnumObject | Function`
|
|
658
671
|
].join(setupLog_1.INDENT_LOG_LINE);
|
|
659
672
|
setupLog_1.typeshiLogger.error(msg);
|
|
660
673
|
throw new Error(msg);
|
|
@@ -663,25 +676,26 @@ function enumArgument(source, arg2, value, enumLabel, enumObject) {
|
|
|
663
676
|
}
|
|
664
677
|
else {
|
|
665
678
|
let msg = [`${source} -> ${vSource} Invalid parameter 'arg2'`,
|
|
666
|
-
`Expected 'arg2' to be either label (string) | labeledArgs (EnumArgumentOptions)`,
|
|
667
|
-
`Received ${typeof arg2} = ${arg2}`
|
|
679
|
+
`Expected: 'arg2' to be either label (string) | labeledArgs (EnumArgumentOptions)`,
|
|
680
|
+
`Received: ${typeof arg2} = ${arg2}`
|
|
668
681
|
].join(setupLog_1.INDENT_LOG_LINE);
|
|
669
682
|
setupLog_1.typeshiLogger.error(msg);
|
|
670
683
|
throw new Error(msg);
|
|
671
684
|
}
|
|
672
685
|
if (!isEnumObject(enumObject)) {
|
|
673
686
|
let msg = [`${source} -> ${vSource}.verbose: Invalid EnumObject for '${enumLabel}'`,
|
|
674
|
-
`Expected non-empty object Record<string, number> | Record<string, string>`,
|
|
687
|
+
`Expected: non-empty object Record<string, number> | Record<string, string>`,
|
|
688
|
+
`Received: ${typeof enumObject} = ${enumObject} = ${JSON.stringify(enumObject)}`
|
|
675
689
|
].join(setupLog_1.INDENT_LOG_LINE);
|
|
676
690
|
setupLog_1.typeshiLogger.error(msg);
|
|
677
691
|
throw new Error(msg);
|
|
678
692
|
}
|
|
679
693
|
const enumKeys = Object.keys(enumObject);
|
|
680
694
|
const enumValues = Object.values(enumObject);
|
|
681
|
-
const
|
|
682
|
-
const
|
|
695
|
+
const isStringEnumObject = isStringEnum(enumObject);
|
|
696
|
+
const isNumberEnumObject = isNumberEnum(enumObject);
|
|
683
697
|
let matchedValue;
|
|
684
|
-
if (
|
|
698
|
+
if (isStringEnumObject) {
|
|
685
699
|
// For string enums, check both keys and values with case-insensitive matching
|
|
686
700
|
if (typeof valueToCheck === 'string') {
|
|
687
701
|
const lowerValueToCheck = valueToCheck.toLowerCase();
|
|
@@ -703,7 +717,7 @@ function enumArgument(source, arg2, value, enumLabel, enumObject) {
|
|
|
703
717
|
}
|
|
704
718
|
}
|
|
705
719
|
}
|
|
706
|
-
else if (
|
|
720
|
+
else if (isNumberEnumObject) {
|
|
707
721
|
// For number enums, check if value is a number or string key
|
|
708
722
|
if (typeof valueToCheck === 'number' && enumValues.includes(valueToCheck)) {
|
|
709
723
|
matchedValue = valueToCheck;
|
|
@@ -717,9 +731,10 @@ function enumArgument(source, arg2, value, enumLabel, enumObject) {
|
|
|
717
731
|
}
|
|
718
732
|
if (matchedValue === undefined) {
|
|
719
733
|
let msg = [`${source} Invalid argument: '${valueLabel}'`,
|
|
720
|
-
`Expected '${valueLabel}' to be: valid ${enumLabel} enum ${
|
|
734
|
+
`Expected '${valueLabel}' to be: valid ${enumLabel} enum ${isStringEnumObject
|
|
721
735
|
? 'key (string) or value (string)' : 'key (string) or value (number)'}`,
|
|
722
|
-
`Received '${valueLabel}'
|
|
736
|
+
`Received '${valueLabel}' (${typeof valueToCheck}) = ${valueToCheck}`,
|
|
737
|
+
``
|
|
723
738
|
].join(setupLog_1.INDENT_LOG_LINE);
|
|
724
739
|
setupLog_1.typeshiLogger.error(msg);
|
|
725
740
|
throw new Error(msg);
|
|
@@ -72,7 +72,7 @@ export declare const DEFAULT_TIMEZONE = "America/Los_Angeles";
|
|
|
72
72
|
* @example "2025-04-16T00:00:00.000Z"
|
|
73
73
|
* @note dateFormat === DateFormatEnum.LOCALE -> use {@link DEFAULT_LOCALE} and {@link DEFAULT_TIMEZONE}
|
|
74
74
|
*/
|
|
75
|
-
export declare function
|
|
75
|
+
export declare function getDateStringFromUnixTimestamp(unixTimestamp: number, dateFormat: DateFormatEnum): string | null;
|
|
76
76
|
/**
|
|
77
77
|
* @param ds1 first date `string` `(required)` - must be in {@link DateFormatEnum.ISO} format or {@link DateFormatEnum.LOCALE} format. @TODO test other formats
|
|
78
78
|
* @param ds2 second date `string` `(optional)` - defaults to current date and time in Pacific Time
|
|
@@ -107,73 +107,90 @@ export declare function getCurrentPacificTime(): string;
|
|
|
107
107
|
* @returns {string} The date string in Pacific Time
|
|
108
108
|
*/
|
|
109
109
|
export declare function toPacificTime(initialDateString: string): string;
|
|
110
|
-
export declare
|
|
111
|
-
readonly from: {
|
|
110
|
+
export declare class Milliseconds {
|
|
111
|
+
static readonly from: {
|
|
112
|
+
/**
|
|
113
|
+
* @param n `number`
|
|
114
|
+
* @param withLeapTime `boolean (optional)` `default = true`
|
|
115
|
+
* @returns `n * (1000 * 60 * 60 * 24) * (withLeapTime ? 365.25 : 365)`
|
|
116
|
+
*/
|
|
117
|
+
years: (n: number, withLeapTime?: boolean) => number;
|
|
112
118
|
/**
|
|
113
119
|
* @param n `number`
|
|
114
120
|
* @returns `n * (1000 * 60 * 60 * 24)` number of milliseconds in `n` days
|
|
115
121
|
*/
|
|
116
|
-
|
|
122
|
+
days: (n: number) => number;
|
|
117
123
|
/**
|
|
118
124
|
* @param n `number`
|
|
119
125
|
* @returns `n * (1000 * 60 * 60)` number of milliseconds in `n` hours
|
|
120
126
|
*/
|
|
121
|
-
|
|
127
|
+
hours: (n: number) => number;
|
|
122
128
|
/**
|
|
123
129
|
* @param n `number`
|
|
124
130
|
* @returns `n * (1000 * 60)` number of milliseconds in `n` minutes
|
|
125
131
|
*/
|
|
126
|
-
|
|
132
|
+
minutes: (n: number) => number;
|
|
127
133
|
/**
|
|
128
134
|
* @param n `number`
|
|
129
135
|
* @returns `n * (1000)` number of milliseconds in `n` seconds
|
|
130
136
|
*/
|
|
131
|
-
|
|
137
|
+
seconds: (n: number) => number;
|
|
132
138
|
/**
|
|
133
139
|
* @param d `Date` object
|
|
134
140
|
* @returns `number` = `d.getTime()` = milliseconds since epoch
|
|
135
141
|
*/
|
|
136
|
-
|
|
142
|
+
date: (d: Date) => number;
|
|
137
143
|
/**
|
|
138
144
|
* @param s `string` date string to pass into Date Constructor (e.g. ISO, UTC, Locale, etc.)
|
|
139
145
|
* @returns `number` milliseconds since epoch or `null` if invalid (i.e. Date constructor can't parse it)
|
|
140
146
|
*/
|
|
141
|
-
|
|
147
|
+
string: (s: string) => number | null;
|
|
142
148
|
};
|
|
143
|
-
readonly to: {
|
|
149
|
+
static readonly to: {
|
|
150
|
+
/**
|
|
151
|
+
* @param n `number`
|
|
152
|
+
* @param withLeapTime `boolean (optional)` `default = true`
|
|
153
|
+
* @returns `number` years in `n` milliseconds
|
|
154
|
+
* = `n / (1000 * 60 * 60 * 24 * (withLeapTime ? 365.25 : 365))`
|
|
155
|
+
*/
|
|
156
|
+
years: (n: number, withLeapTime?: boolean) => number;
|
|
144
157
|
/**
|
|
145
158
|
* @param n `number`
|
|
146
159
|
* @returns `number` days in `n` milliseconds
|
|
160
|
+
* = `n / (1000 * 60 * 60 * 24)`
|
|
147
161
|
*/
|
|
148
|
-
|
|
162
|
+
days: (n: number) => number;
|
|
149
163
|
/**
|
|
150
164
|
* @param n `number`
|
|
151
165
|
* @returns `number` hours in `n` milliseconds
|
|
166
|
+
* = `n / (1000 * 60 * 60)`
|
|
152
167
|
*/
|
|
153
|
-
|
|
168
|
+
hours: (n: number) => number;
|
|
154
169
|
/**
|
|
155
170
|
* @param n `number`
|
|
156
171
|
* @returns `number` minutes in `n` milliseconds
|
|
172
|
+
* = `n / (1000 * 60)`
|
|
157
173
|
*/
|
|
158
|
-
|
|
174
|
+
minutes: (n: number) => number;
|
|
159
175
|
/**
|
|
160
176
|
* @param n `number`
|
|
161
177
|
* @returns `number` seconds in `n` milliseconds
|
|
178
|
+
* = `n / (1000)`
|
|
162
179
|
*/
|
|
163
|
-
|
|
180
|
+
seconds: (n: number) => number;
|
|
164
181
|
/**
|
|
165
182
|
* interprets `n` as milliseconds since epoch
|
|
166
183
|
* @param n `number` milliseconds
|
|
167
184
|
* @returns `Date` object
|
|
168
185
|
*/
|
|
169
|
-
|
|
186
|
+
date: (n: number) => Date;
|
|
170
187
|
/**
|
|
171
188
|
* @param n `number`
|
|
172
|
-
* @param format {@link DateFormatEnum} default = {@link DateFormatEnum.ISO}
|
|
173
|
-
* @param locale `string` default = `'en-US'` (only used if format =
|
|
174
|
-
* @param timeZone `string` default = `'America/Los_Angeles'` (only used if format =
|
|
189
|
+
* @param format {@link DateFormatEnum} `default` = {@link DateFormatEnum.ISO}
|
|
190
|
+
* @param locale `string` `default` = `'en-US'` (only used if `format` = `DateFormatEnum.LOCALE`)
|
|
191
|
+
* @param timeZone `string` `default` = `'America/Los_Angeles'` (only used if `format` = `DateFormatEnum.LOCALE`)
|
|
175
192
|
* @returns `string` formatted date string or empty string if error
|
|
176
193
|
*/
|
|
177
|
-
|
|
194
|
+
string: (n: number, format?: DateFormatEnum, locale?: string, timeZone?: string) => string;
|
|
178
195
|
};
|
|
179
|
-
}
|
|
196
|
+
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
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
|
-
exports.
|
|
7
|
+
exports.getDateStringFromUnixTimestamp = getDateStringFromUnixTimestamp;
|
|
8
8
|
exports.calculateDifferenceOfDateStrings = calculateDifferenceOfDateStrings;
|
|
9
9
|
exports.getUnixTimestampFromISO = getUnixTimestampFromISO;
|
|
10
10
|
exports.localeStringToDate = localeStringToDate;
|
|
@@ -79,7 +79,7 @@ exports.DEFAULT_TIMEZONE = 'America/Los_Angeles';
|
|
|
79
79
|
* @example "2025-04-16T00:00:00.000Z"
|
|
80
80
|
* @note dateFormat === DateFormatEnum.LOCALE -> use {@link DEFAULT_LOCALE} and {@link DEFAULT_TIMEZONE}
|
|
81
81
|
*/
|
|
82
|
-
function
|
|
82
|
+
function getDateStringFromUnixTimestamp(unixTimestamp, dateFormat) {
|
|
83
83
|
if (!unixTimestamp) {
|
|
84
84
|
console.error('No unixTimestamp provided');
|
|
85
85
|
return null;
|
|
@@ -127,13 +127,13 @@ function calculateDifferenceOfDateStrings(ds1, ds2 = getCurrentPacificTime(), un
|
|
|
127
127
|
case exports.TimeUnitEnum.MILLISECONDS:
|
|
128
128
|
return diffInMs;
|
|
129
129
|
case exports.TimeUnitEnum.SECONDS:
|
|
130
|
-
return
|
|
130
|
+
return Milliseconds.from.seconds(diffInMs);
|
|
131
131
|
case exports.TimeUnitEnum.MINUTES:
|
|
132
|
-
return
|
|
132
|
+
return Milliseconds.from.minutes(diffInMs);
|
|
133
133
|
case exports.TimeUnitEnum.HOURS:
|
|
134
|
-
return
|
|
134
|
+
return Milliseconds.from.hours(diffInMs);
|
|
135
135
|
case exports.TimeUnitEnum.DAYS:
|
|
136
|
-
return
|
|
136
|
+
return Milliseconds.from.days(diffInMs);
|
|
137
137
|
default:
|
|
138
138
|
console.error('Invalid time unit specified. Use TimeUnitEnum.MILLISECONDS, TimeUnitEnum.SECONDS, TimeUnitEnum.MINUTES, TimeUnitEnum.HOURS, or TimeUnitEnum.DAYS');
|
|
139
139
|
return null;
|
|
@@ -208,122 +208,144 @@ function toPacificTime(initialDateString) {
|
|
|
208
208
|
const pacificTime = initialDate.toLocaleString(exports.DEFAULT_LOCALE, { timeZone: exports.DEFAULT_TIMEZONE });
|
|
209
209
|
return pacificTime;
|
|
210
210
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
* @returns `number` = `d.getTime()` = milliseconds since epoch
|
|
244
|
-
*/
|
|
245
|
-
date: (d) => {
|
|
246
|
-
return d.getTime();
|
|
247
|
-
},
|
|
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) => {
|
|
253
|
-
try {
|
|
254
|
-
const date = new Date(s);
|
|
255
|
-
if (isNaN(date.getTime())) {
|
|
256
|
-
throw new Error(`Invalid date string: '${s}'`);
|
|
257
|
-
}
|
|
258
|
-
return date.getTime();
|
|
259
|
-
}
|
|
260
|
-
catch (error) {
|
|
261
|
-
console.error(`Failed to parse date string: '${s}'`, error);
|
|
262
|
-
return null;
|
|
263
|
-
}
|
|
264
|
-
},
|
|
211
|
+
class Milliseconds {
|
|
212
|
+
}
|
|
213
|
+
exports.Milliseconds = Milliseconds;
|
|
214
|
+
Milliseconds.from = {
|
|
215
|
+
/**
|
|
216
|
+
* @param n `number`
|
|
217
|
+
* @param withLeapTime `boolean (optional)` `default = true`
|
|
218
|
+
* @returns `n * (1000 * 60 * 60 * 24) * (withLeapTime ? 365.25 : 365)`
|
|
219
|
+
*/
|
|
220
|
+
years: (n, withLeapTime = true) => {
|
|
221
|
+
return n * (1000 * 60 * 60 * 24) * (withLeapTime ? 365.25 : 365);
|
|
222
|
+
},
|
|
223
|
+
/**
|
|
224
|
+
* @param n `number`
|
|
225
|
+
* @returns `n * (1000 * 60 * 60 * 24)` number of milliseconds in `n` days
|
|
226
|
+
*/
|
|
227
|
+
days: (n) => {
|
|
228
|
+
return n * (1000 * 60 * 60 * 24);
|
|
229
|
+
},
|
|
230
|
+
/**
|
|
231
|
+
* @param n `number`
|
|
232
|
+
* @returns `n * (1000 * 60 * 60)` number of milliseconds in `n` hours
|
|
233
|
+
*/
|
|
234
|
+
hours: (n) => {
|
|
235
|
+
return n * (1000 * 60 * 60);
|
|
236
|
+
},
|
|
237
|
+
/**
|
|
238
|
+
* @param n `number`
|
|
239
|
+
* @returns `n * (1000 * 60)` number of milliseconds in `n` minutes
|
|
240
|
+
*/
|
|
241
|
+
minutes: (n) => {
|
|
242
|
+
return n * (1000 * 60);
|
|
265
243
|
},
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
},
|
|
288
|
-
/**
|
|
289
|
-
* @param n `number`
|
|
290
|
-
* @returns `number` seconds in `n` milliseconds
|
|
291
|
-
*/
|
|
292
|
-
seconds: (n) => {
|
|
293
|
-
return n / (1000);
|
|
294
|
-
},
|
|
295
|
-
/**
|
|
296
|
-
* interprets `n` as milliseconds since epoch
|
|
297
|
-
* @param n `number` milliseconds
|
|
298
|
-
* @returns `Date` object
|
|
299
|
-
*/
|
|
300
|
-
date: (n) => {
|
|
301
|
-
return new Date(n);
|
|
302
|
-
},
|
|
303
|
-
/**
|
|
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
|
|
309
|
-
*/
|
|
310
|
-
string: (n, format = exports.DateFormatEnum.ISO, locale = exports.DEFAULT_LOCALE, timeZone = exports.DEFAULT_TIMEZONE) => {
|
|
311
|
-
const date = new Date(n);
|
|
244
|
+
/**
|
|
245
|
+
* @param n `number`
|
|
246
|
+
* @returns `n * (1000)` number of milliseconds in `n` seconds
|
|
247
|
+
*/
|
|
248
|
+
seconds: (n) => {
|
|
249
|
+
return n * (1000);
|
|
250
|
+
},
|
|
251
|
+
/**
|
|
252
|
+
* @param d `Date` object
|
|
253
|
+
* @returns `number` = `d.getTime()` = milliseconds since epoch
|
|
254
|
+
*/
|
|
255
|
+
date: (d) => {
|
|
256
|
+
return d.getTime();
|
|
257
|
+
},
|
|
258
|
+
/**
|
|
259
|
+
* @param s `string` date string to pass into Date Constructor (e.g. ISO, UTC, Locale, etc.)
|
|
260
|
+
* @returns `number` milliseconds since epoch or `null` if invalid (i.e. Date constructor can't parse it)
|
|
261
|
+
*/
|
|
262
|
+
string: (s) => {
|
|
263
|
+
try {
|
|
264
|
+
const date = new Date(s);
|
|
312
265
|
if (isNaN(date.getTime())) {
|
|
313
|
-
|
|
314
|
-
return ``;
|
|
266
|
+
throw new Error(`Invalid date string: '${s}'`);
|
|
315
267
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
268
|
+
return date.getTime();
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
console.error(`[Milliseconds.from.string()] Failed to parse date string: '${s}'`, error);
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
};
|
|
276
|
+
Milliseconds.to = {
|
|
277
|
+
/**
|
|
278
|
+
* @param n `number`
|
|
279
|
+
* @param withLeapTime `boolean (optional)` `default = true`
|
|
280
|
+
* @returns `number` years in `n` milliseconds
|
|
281
|
+
* = `n / (1000 * 60 * 60 * 24 * (withLeapTime ? 365.25 : 365))`
|
|
282
|
+
*/
|
|
283
|
+
years: (n, withLeapTime = true) => {
|
|
284
|
+
return n / (1000 * 60 * 60 * 24 * (withLeapTime ? 365.25 : 365));
|
|
285
|
+
},
|
|
286
|
+
/**
|
|
287
|
+
* @param n `number`
|
|
288
|
+
* @returns `number` days in `n` milliseconds
|
|
289
|
+
* = `n / (1000 * 60 * 60 * 24)`
|
|
290
|
+
*/
|
|
291
|
+
days: (n) => {
|
|
292
|
+
return n / (1000 * 60 * 60 * 24);
|
|
293
|
+
},
|
|
294
|
+
/**
|
|
295
|
+
* @param n `number`
|
|
296
|
+
* @returns `number` hours in `n` milliseconds
|
|
297
|
+
* = `n / (1000 * 60 * 60)`
|
|
298
|
+
*/
|
|
299
|
+
hours: (n) => {
|
|
300
|
+
return n / (1000 * 60 * 60);
|
|
301
|
+
},
|
|
302
|
+
/**
|
|
303
|
+
* @param n `number`
|
|
304
|
+
* @returns `number` minutes in `n` milliseconds
|
|
305
|
+
* = `n / (1000 * 60)`
|
|
306
|
+
*/
|
|
307
|
+
minutes: (n) => {
|
|
308
|
+
return n / (1000 * 60);
|
|
309
|
+
},
|
|
310
|
+
/**
|
|
311
|
+
* @param n `number`
|
|
312
|
+
* @returns `number` seconds in `n` milliseconds
|
|
313
|
+
* = `n / (1000)`
|
|
314
|
+
*/
|
|
315
|
+
seconds: (n) => {
|
|
316
|
+
return n / (1000);
|
|
317
|
+
},
|
|
318
|
+
/**
|
|
319
|
+
* interprets `n` as milliseconds since epoch
|
|
320
|
+
* @param n `number` milliseconds
|
|
321
|
+
* @returns `Date` object
|
|
322
|
+
*/
|
|
323
|
+
date: (n) => {
|
|
324
|
+
return new Date(n);
|
|
325
|
+
},
|
|
326
|
+
/**
|
|
327
|
+
* @param n `number`
|
|
328
|
+
* @param format {@link DateFormatEnum} `default` = {@link DateFormatEnum.ISO}
|
|
329
|
+
* @param locale `string` `default` = `'en-US'` (only used if `format` = `DateFormatEnum.LOCALE`)
|
|
330
|
+
* @param timeZone `string` `default` = `'America/Los_Angeles'` (only used if `format` = `DateFormatEnum.LOCALE`)
|
|
331
|
+
* @returns `string` formatted date string or empty string if error
|
|
332
|
+
*/
|
|
333
|
+
string: (n, format = exports.DateFormatEnum.ISO, locale = exports.DEFAULT_LOCALE, timeZone = exports.DEFAULT_TIMEZONE) => {
|
|
334
|
+
const date = new Date(n);
|
|
335
|
+
if (isNaN(date.getTime())) {
|
|
336
|
+
console.error(`[Milliseconds.to.string()] Invalid milliseconds value: '${n}'`);
|
|
337
|
+
return ``;
|
|
338
|
+
}
|
|
339
|
+
switch (format) {
|
|
340
|
+
case exports.DateFormatEnum.ISO:
|
|
341
|
+
return date.toISOString();
|
|
342
|
+
case exports.DateFormatEnum.UTC:
|
|
343
|
+
return date.toUTCString();
|
|
344
|
+
case exports.DateFormatEnum.LOCALE:
|
|
345
|
+
return date.toLocaleString(locale, { timeZone });
|
|
346
|
+
default:
|
|
347
|
+
console.error(`[Milliseconds.to.string()] Invalid date format specified: '${format}'.`, `Use DateFormatEnum.ISO, DateFormatEnum.UTC, or DateFormatEnum.LOCALE`);
|
|
348
|
+
return ``;
|
|
349
|
+
}
|
|
328
350
|
},
|
|
329
351
|
};
|
|
@@ -9,6 +9,7 @@ import { ILogObj, ILogObjMeta } from "tslog";
|
|
|
9
9
|
*/
|
|
10
10
|
export declare function getSourceString(fileName: string, func: string | Function, funcInfo?: any, startLine?: number, endLine?: number): string;
|
|
11
11
|
/**
|
|
12
|
+
* @deprecated
|
|
12
13
|
* Auto-formats debug logs at the end of application execution.
|
|
13
14
|
* Call this function when your main application is finishing.
|
|
14
15
|
* @param filePaths `string[]` - optional, specific file paths to format.
|
|
@@ -17,6 +18,7 @@ export declare function getSourceString(fileName: string, func: string | Functio
|
|
|
17
18
|
*/
|
|
18
19
|
export declare function autoFormatLogsOnExit(filePaths?: string[]): void;
|
|
19
20
|
/**
|
|
21
|
+
* @deprecated
|
|
20
22
|
* Formats a debug log file from JSON format to a more readable text format.
|
|
21
23
|
* Removes the numeric keys and properly handles escape sequences.
|
|
22
24
|
* @param inputPath `string` - path to the input log file (e.g., DEBUG.txt)
|
|
@@ -26,6 +28,7 @@ export declare function autoFormatLogsOnExit(filePaths?: string[]): void;
|
|
|
26
28
|
*/
|
|
27
29
|
export declare function formatDebugLogFile(inputPath: string, outputPath?: string): void;
|
|
28
30
|
/**
|
|
31
|
+
* @deprecated
|
|
29
32
|
* Formats all debug log files in the log directory.
|
|
30
33
|
* Looks for .txt files and creates .FORMATTED.txt versions.
|
|
31
34
|
* @param logDirectory `string` - optional, path to the log directory.
|
|
@@ -34,6 +37,7 @@ export declare function formatDebugLogFile(inputPath: string, outputPath?: strin
|
|
|
34
37
|
*/
|
|
35
38
|
export declare function formatAllDebugLogs(logDirectory: string): void;
|
|
36
39
|
/**
|
|
40
|
+
* @deprecated
|
|
37
41
|
* reduce metadata to two entries, then return stringified `logObj`
|
|
38
42
|
* @param logObj {@link ILogObj}
|
|
39
43
|
* @returns `string`
|
package/dist/utils/io/logging.js
CHANGED
|
@@ -71,6 +71,7 @@ function getSourceString(fileName, func, funcInfo, startLine, endLine) {
|
|
|
71
71
|
return `[${fileName}.${funcName}(${(0, typeValidation_1.isNonEmptyString)(funcInfo) ? ` ${funcInfo} ` : ''})${lineNumberText}]`;
|
|
72
72
|
}
|
|
73
73
|
/**
|
|
74
|
+
* @deprecated
|
|
74
75
|
* Auto-formats debug logs at the end of application execution.
|
|
75
76
|
* Call this function when your main application is finishing.
|
|
76
77
|
* @param filePaths `string[]` - optional, specific file paths to format.
|
|
@@ -99,6 +100,7 @@ function autoFormatLogsOnExit(filePaths) {
|
|
|
99
100
|
}
|
|
100
101
|
}
|
|
101
102
|
/**
|
|
103
|
+
* @deprecated
|
|
102
104
|
* Formats a debug log file from JSON format to a more readable text format.
|
|
103
105
|
* Removes the numeric keys and properly handles escape sequences.
|
|
104
106
|
* @param inputPath `string` - path to the input log file (e.g., DEBUG.txt)
|
|
@@ -125,6 +127,7 @@ function formatDebugLogFile(inputPath, outputPath) {
|
|
|
125
127
|
}
|
|
126
128
|
}
|
|
127
129
|
/**
|
|
130
|
+
* @deprecated
|
|
128
131
|
* Formats the content of a debug log file from JSON objects to readable text.
|
|
129
132
|
* @param content `string` - the raw content of the log file
|
|
130
133
|
* @returns `string` - the formatted content
|
|
@@ -179,6 +182,7 @@ function formatLogContent(content) {
|
|
|
179
182
|
return formattedLines.join('\n');
|
|
180
183
|
}
|
|
181
184
|
/**
|
|
185
|
+
* @deprecated
|
|
182
186
|
* Formats a single log entry object into readable text.
|
|
183
187
|
* @param logObj `Record<string, any>` - the parsed log object
|
|
184
188
|
* @returns `string` - the formatted log entry
|
|
@@ -209,20 +213,7 @@ function formatSingleLogEntry(logObj) {
|
|
|
209
213
|
return lines.join(''); // lines.join('\n');
|
|
210
214
|
}
|
|
211
215
|
/**
|
|
212
|
-
*
|
|
213
|
-
* @param s `string` - the string with escape sequences
|
|
214
|
-
* @returns `string` - the unescaped string
|
|
215
|
-
*/
|
|
216
|
-
function unescapeString(s) {
|
|
217
|
-
return String(s) // coerce passed value to string
|
|
218
|
-
.replace(/\\n/g, '\n') // Replace \n with actual newlines
|
|
219
|
-
.replace(/\\t/g, '\t') // Replace \t with actual tabs
|
|
220
|
-
.replace(/\\r/g, '\r') // Replace \r with carriage returns
|
|
221
|
-
.replace(/\\"/g, '"') // Replace \" with actual quotes
|
|
222
|
-
.replace(/\\\\/g, '\\') // Replace \\ with single backslash
|
|
223
|
-
.replace(/\\'/g, "'"); // Replace \' with actual single quotes
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
216
|
+
* @deprecated
|
|
226
217
|
* Formats all debug log files in the log directory.
|
|
227
218
|
* Looks for .txt files and creates .FORMATTED.txt versions.
|
|
228
219
|
* @param logDirectory `string` - optional, path to the log directory.
|
|
@@ -259,6 +250,7 @@ function formatAllDebugLogs(logDirectory) {
|
|
|
259
250
|
}
|
|
260
251
|
}
|
|
261
252
|
/**
|
|
253
|
+
* @deprecated
|
|
262
254
|
* reduce metadata to two entries, then return stringified `logObj`
|
|
263
255
|
* @param logObj {@link ILogObj}
|
|
264
256
|
* @returns `string`
|
|
@@ -279,3 +271,17 @@ function formatLogObj(logObj) {
|
|
|
279
271
|
}
|
|
280
272
|
return JSON.stringify(logObj, null, 4) + "\n";
|
|
281
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* Unescapes a string by replacing escape sequences with their actual characters.
|
|
276
|
+
* @param s `string` - the string with escape sequences
|
|
277
|
+
* @returns `string` - the unescaped string
|
|
278
|
+
*/
|
|
279
|
+
function unescapeString(s) {
|
|
280
|
+
return String(s) // coerce passed value to string
|
|
281
|
+
.replace(/\\n/g, '\n') // Replace \n with actual newlines
|
|
282
|
+
.replace(/\\t/g, '\t') // Replace \t with actual tabs
|
|
283
|
+
.replace(/\\r/g, '\r') // Replace \r with carriage returns
|
|
284
|
+
.replace(/\\"/g, '"') // Replace \" with actual quotes
|
|
285
|
+
.replace(/\\\\/g, '\\') // Replace \\ with single backslash
|
|
286
|
+
.replace(/\\'/g, "'"); // Replace \' with actual single quotes
|
|
287
|
+
}
|