typeshi 2.1.0 → 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.
|
@@ -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);
|
|
@@ -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
|
+
}
|
|
@@ -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
|
};
|
|
@@ -12,13 +12,6 @@ export declare function isFile(value: string): value is string;
|
|
|
12
12
|
* @throws an error if the file extension is unsupported
|
|
13
13
|
*/
|
|
14
14
|
export declare function getDelimiterFromFilePath(filePath: string): DelimiterCharacterEnum | string;
|
|
15
|
-
/**
|
|
16
|
-
* @param filePath `string`
|
|
17
|
-
* @returns **`jsonData`** — `T extends Record<string, any>` - JSON data as an object
|
|
18
|
-
* @note returns empty object if error occurred while reading `filepath` or parsing json
|
|
19
|
-
* - use {@link readJsonSyncOrThrow} if throwing error is desired behavior
|
|
20
|
-
*/
|
|
21
|
-
export declare const readJsonSync: typeof readJsonFileAsObject;
|
|
22
15
|
/**
|
|
23
16
|
* a.k.a. `readJsonSync`
|
|
24
17
|
* @param filePath `string`
|
|
@@ -26,7 +19,7 @@ export declare const readJsonSync: typeof readJsonFileAsObject;
|
|
|
26
19
|
* @note returns empty object if error occurred while reading `filepath` or parsing json
|
|
27
20
|
* - use {@link readJsonSyncOrThrow} if throwing error is desired behavior
|
|
28
21
|
*/
|
|
29
|
-
export declare function
|
|
22
|
+
export declare function readJsonSync<T extends Record<string, any> = {}>(filePath: string): T;
|
|
30
23
|
/**
|
|
31
24
|
* @param filePath `string`
|
|
32
25
|
* @returns **`jsonData`** — `T extends Record<string, any>`
|
package/dist/utils/io/reading.js
CHANGED
|
@@ -36,11 +36,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.readJsonSync = void 0;
|
|
40
39
|
exports.isDirectory = isDirectory;
|
|
41
40
|
exports.isFile = isFile;
|
|
42
41
|
exports.getDelimiterFromFilePath = getDelimiterFromFilePath;
|
|
43
|
-
exports.
|
|
42
|
+
exports.readJsonSync = readJsonSync;
|
|
44
43
|
exports.readJsonSyncOrThrow = readJsonSyncOrThrow;
|
|
45
44
|
exports.readFileToArraySync = readFileToArraySync;
|
|
46
45
|
exports.coerceFileExtension = coerceFileExtension;
|
|
@@ -100,13 +99,6 @@ function getDelimiterFromFilePath(filePath) {
|
|
|
100
99
|
throw new Error(`[reading.getDelimiterFromFilePath()] Unsupported file extension: ${extension}`);
|
|
101
100
|
}
|
|
102
101
|
}
|
|
103
|
-
/**
|
|
104
|
-
* @param filePath `string`
|
|
105
|
-
* @returns **`jsonData`** — `T extends Record<string, any>` - JSON data as an object
|
|
106
|
-
* @note returns empty object if error occurred while reading `filepath` or parsing json
|
|
107
|
-
* - use {@link readJsonSyncOrThrow} if throwing error is desired behavior
|
|
108
|
-
*/
|
|
109
|
-
exports.readJsonSync = readJsonFileAsObject;
|
|
110
102
|
/**
|
|
111
103
|
* a.k.a. `readJsonSync`
|
|
112
104
|
* @param filePath `string`
|
|
@@ -114,8 +106,8 @@ exports.readJsonSync = readJsonFileAsObject;
|
|
|
114
106
|
* @note returns empty object if error occurred while reading `filepath` or parsing json
|
|
115
107
|
* - use {@link readJsonSyncOrThrow} if throwing error is desired behavior
|
|
116
108
|
*/
|
|
117
|
-
function
|
|
118
|
-
const source = (0, logging_1.getSourceString)(__filename,
|
|
109
|
+
function readJsonSync(filePath) {
|
|
110
|
+
const source = (0, logging_1.getSourceString)(__filename, readJsonSync.name);
|
|
119
111
|
try {
|
|
120
112
|
filePath = coerceFileExtension(filePath, 'json');
|
|
121
113
|
const data = fs_1.default.readFileSync(filePath, 'utf8');
|