@alextheman/utility 3.5.8 → 3.5.10
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/index.cjs +84 -2
- package/dist/index.d.cts +88 -3
- package/dist/index.d.ts +88 -3
- package/dist/index.js +84 -2
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -51,9 +51,12 @@ var appendSemicolon_default = appendSemicolon;
|
|
|
51
51
|
*
|
|
52
52
|
* If the callback returns at least one Promise, the entire result will be wrapped
|
|
53
53
|
* in a `Promise` and resolved with `Promise.all`. Otherwise, a plain array is returned.
|
|
54
|
+
*
|
|
54
55
|
* @template ItemType
|
|
56
|
+
*
|
|
55
57
|
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
56
|
-
* @param
|
|
58
|
+
* @param length - The desired length of the resulting array.
|
|
59
|
+
*
|
|
57
60
|
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
58
61
|
*/
|
|
59
62
|
function fillArray(callback, length = 1) {
|
|
@@ -74,10 +77,13 @@ var fillArray_default = fillArray;
|
|
|
74
77
|
*
|
|
75
78
|
* If `secondArray` is shorter than `firstArray`, the second position in the tuple
|
|
76
79
|
* will be `undefined`. Iteration always uses the length of the first array.
|
|
80
|
+
*
|
|
77
81
|
* @template FirstArrayItem
|
|
78
82
|
* @template SecondArrayItem
|
|
83
|
+
*
|
|
79
84
|
* @param firstArray - The first array. Each item in this will take up the first tuple spot.
|
|
80
85
|
* @param secondArray - The second array. Each item in this will take up the second tuple spot.
|
|
86
|
+
*
|
|
81
87
|
* @returns An array of `[firstItem, secondItem]` tuples for each index in `firstArray`.
|
|
82
88
|
*/
|
|
83
89
|
function paralleliseArrays(firstArray, secondArray) {
|
|
@@ -113,6 +119,15 @@ var getRandomNumber_default = getRandomNumber;
|
|
|
113
119
|
|
|
114
120
|
//#endregion
|
|
115
121
|
//#region src/functions/arrayHelpers/randomiseArray.ts
|
|
122
|
+
/**
|
|
123
|
+
* Randomises the order of a given array and returns the result in a new array without mutating the original.
|
|
124
|
+
*
|
|
125
|
+
* @template ItemType - The type of the array items.
|
|
126
|
+
*
|
|
127
|
+
* @param array - The array to randomise.
|
|
128
|
+
*
|
|
129
|
+
* @returns A new array with the items randomised.
|
|
130
|
+
*/
|
|
116
131
|
function randomiseArray(array) {
|
|
117
132
|
const mutableArray = [...array];
|
|
118
133
|
const outputArray = [];
|
|
@@ -126,6 +141,21 @@ var randomiseArray_default = randomiseArray;
|
|
|
126
141
|
|
|
127
142
|
//#endregion
|
|
128
143
|
//#region src/functions/arrayHelpers/range.ts
|
|
144
|
+
/**
|
|
145
|
+
* Creates an array of numbers within a given range.
|
|
146
|
+
*
|
|
147
|
+
* The range is inclusive of `start` and exclusive of `stop`.
|
|
148
|
+
* The sign of `step` must match the direction of the range.
|
|
149
|
+
*
|
|
150
|
+
* @param start - The number to start at (inclusive).
|
|
151
|
+
* @param stop - The number to stop at (exclusive).
|
|
152
|
+
* @param step - The step size between numbers, defaulting to 1.
|
|
153
|
+
*
|
|
154
|
+
* @throws {Error} If `step` is `0`.
|
|
155
|
+
* @throws {Error} If `step` direction does not match the order of `start` and `stop`.
|
|
156
|
+
*
|
|
157
|
+
* @returns An array of numbers satisfying the range provided.
|
|
158
|
+
*/
|
|
129
159
|
function range(start, stop, step = 1) {
|
|
130
160
|
const numbers = [];
|
|
131
161
|
if (step === 0) throw new Error("ZERO_STEP_SIZE_NOT_ALLOWED");
|
|
@@ -234,8 +264,16 @@ var createTemplateStringsArray_default = createTemplateStringsArray;
|
|
|
234
264
|
|
|
235
265
|
//#endregion
|
|
236
266
|
//#region src/functions/date/addDaysToDate.ts
|
|
267
|
+
/**
|
|
268
|
+
* Adds a given number of days to the provided date, returning the result as a new instance of Date.
|
|
269
|
+
*
|
|
270
|
+
* @param currentDate - The starting date (defaults to today).
|
|
271
|
+
* @param dayIncrement - The amount of days you want to add (defaults to 1)
|
|
272
|
+
*
|
|
273
|
+
* @returns A new Date instance with the number of days added to the initially provided date.
|
|
274
|
+
*/
|
|
237
275
|
function addDaysToDate(currentDate = /* @__PURE__ */ new Date(), dayIncrement = 1) {
|
|
238
|
-
const newDate = currentDate;
|
|
276
|
+
const newDate = new Date(currentDate);
|
|
239
277
|
newDate.setDate(newDate.getDate() + dayIncrement);
|
|
240
278
|
return newDate;
|
|
241
279
|
}
|
|
@@ -243,6 +281,14 @@ var addDaysToDate_default = addDaysToDate;
|
|
|
243
281
|
|
|
244
282
|
//#endregion
|
|
245
283
|
//#region src/functions/date/isSameDate.ts
|
|
284
|
+
/**
|
|
285
|
+
* Checks if the provided dates happen on the exact same day, month, and year.
|
|
286
|
+
*
|
|
287
|
+
* @param firstDate - The first date to compare.
|
|
288
|
+
* @param secondDate - The second date to compare.
|
|
289
|
+
*
|
|
290
|
+
* @returns True if the provided dates occur on exactly the same day, month, and year, and returns false otherwise.
|
|
291
|
+
*/
|
|
246
292
|
function isSameDate(firstDate, secondDate) {
|
|
247
293
|
return firstDate.getDate() === secondDate.getDate() && firstDate.getMonth() === secondDate.getMonth() && firstDate.getFullYear() === secondDate.getFullYear();
|
|
248
294
|
}
|
|
@@ -250,6 +296,17 @@ var isSameDate_default = isSameDate;
|
|
|
250
296
|
|
|
251
297
|
//#endregion
|
|
252
298
|
//#region src/functions/date/formatDateAndTime.ts
|
|
299
|
+
/**
|
|
300
|
+
* Creates a human-readable string with information about the input date.
|
|
301
|
+
*
|
|
302
|
+
* @param inputDate - The date to base the string on.
|
|
303
|
+
*
|
|
304
|
+
* @returns A new string with information about the given date.
|
|
305
|
+
*
|
|
306
|
+
* - If the date given is today, the output will be something like `Today at HH:MM`
|
|
307
|
+
* - If the date given happened yesterday, the output will be something like `Yesterday at HH:MM`
|
|
308
|
+
* - For any other date, the output will be something like `DD/MM/YYYY, HH:MM`
|
|
309
|
+
*/
|
|
253
310
|
function formatDateAndTime(inputDate) {
|
|
254
311
|
const yesterday = addDaysToDate_default(/* @__PURE__ */ new Date(), -1);
|
|
255
312
|
const today = /* @__PURE__ */ new Date();
|
|
@@ -262,6 +319,15 @@ var formatDateAndTime_default = formatDateAndTime;
|
|
|
262
319
|
|
|
263
320
|
//#endregion
|
|
264
321
|
//#region src/functions/date/isLeapYear.ts
|
|
322
|
+
/**
|
|
323
|
+
* Checks if the provided year is a leap year.
|
|
324
|
+
*
|
|
325
|
+
* @param year - The year to check as a number.
|
|
326
|
+
*
|
|
327
|
+
* @throws {TypeError} If the year provided is not an integer.
|
|
328
|
+
*
|
|
329
|
+
* @returns True if the year is a leap year, and false otherwise.
|
|
330
|
+
*/
|
|
265
331
|
function isLeapYear(year) {
|
|
266
332
|
const parsedYear = parseIntStrict_default(`${year}`);
|
|
267
333
|
return parsedYear % 4 === 0 && parsedYear % 100 !== 0 || parsedYear % 400 === 0;
|
|
@@ -274,6 +340,14 @@ function checkLeapYear(firstDate, secondDate) {
|
|
|
274
340
|
if (isLeapYear_default(firstDate.getFullYear()) && firstDate.getMonth() === 1 && secondDate.getMonth() === 1) return firstDate.getDate() === 29 && secondDate.getDate() === 28;
|
|
275
341
|
return false;
|
|
276
342
|
}
|
|
343
|
+
/**
|
|
344
|
+
* Checks if the provided dates are exactly a whole number of years apart.
|
|
345
|
+
*
|
|
346
|
+
* @param firstDate - The first date to compare.
|
|
347
|
+
* @param secondDate - The second date to compare.
|
|
348
|
+
*
|
|
349
|
+
* @returns True if the provided dates are exactly a whole number of years apart, and false otherwise.
|
|
350
|
+
*/
|
|
277
351
|
function isAnniversary(firstDate, secondDate) {
|
|
278
352
|
if (checkLeapYear(firstDate, secondDate) || checkLeapYear(secondDate, firstDate)) return true;
|
|
279
353
|
return firstDate.getDate() === secondDate.getDate() && firstDate.getMonth() === secondDate.getMonth();
|
|
@@ -312,6 +386,14 @@ function leapYearFebruaryChecks(firstDate, secondDate) {
|
|
|
312
386
|
}
|
|
313
387
|
return false;
|
|
314
388
|
}
|
|
389
|
+
/**
|
|
390
|
+
* Checks if the provided dates are exactly a whole number of months apart.
|
|
391
|
+
*
|
|
392
|
+
* @param firstDate - The first date to compare.
|
|
393
|
+
* @param secondDate - The second date to compare.
|
|
394
|
+
*
|
|
395
|
+
* @returns True if the provided dates are exactly a whole number of months apart, and false otherwise.
|
|
396
|
+
*/
|
|
315
397
|
function isMonthlyMultiple(firstDate, secondDate) {
|
|
316
398
|
if (endOfMonthChecksButNotFebruary(firstDate, secondDate) || endOfMonthChecksButNotFebruary(secondDate, firstDate)) return true;
|
|
317
399
|
if (nonLeapYearFebruaryChecks(firstDate, secondDate) || nonLeapYearFebruaryChecks(secondDate, firstDate)) return true;
|
package/dist/index.d.cts
CHANGED
|
@@ -12,9 +12,12 @@ declare function appendSemicolon(stringToAppendTo: string): string;
|
|
|
12
12
|
*
|
|
13
13
|
* The callback will be invoked once for each index from `0` to `length - 1`.
|
|
14
14
|
* If no length is provided, a single-element array will be produced.
|
|
15
|
+
*
|
|
15
16
|
* @template ItemType
|
|
17
|
+
*
|
|
16
18
|
* @param callback - An asynchronous function invoked with the current index.
|
|
17
|
-
* @param
|
|
19
|
+
* @param length - The desired length of the resulting array.
|
|
20
|
+
*
|
|
18
21
|
* @returns A Promise resolving to an array of the callback results.
|
|
19
22
|
*/
|
|
20
23
|
declare function fillArray<ItemType$1>(callback: (index: number) => Promise<ItemType$1>, length?: number): Promise<ItemType$1[]>;
|
|
@@ -23,9 +26,12 @@ declare function fillArray<ItemType$1>(callback: (index: number) => Promise<Item
|
|
|
23
26
|
*
|
|
24
27
|
* The callback will be invoked once for each index from `0` to `length - 1`.
|
|
25
28
|
* If no length is provided, a single-element array will be produced.
|
|
29
|
+
*
|
|
26
30
|
* @template ItemType
|
|
31
|
+
*
|
|
27
32
|
* @param callback - A synchronous function invoked with the current index.
|
|
28
|
-
* @param
|
|
33
|
+
* @param length - The desired length of the resulting array.
|
|
34
|
+
*
|
|
29
35
|
* @returns An array of the callback results.
|
|
30
36
|
*/
|
|
31
37
|
declare function fillArray<ItemType$1>(callback: (index: number) => ItemType$1, length?: number): ItemType$1[];
|
|
@@ -37,18 +43,45 @@ type ParallelTuple<A, B> = [A, B | undefined];
|
|
|
37
43
|
*
|
|
38
44
|
* If `secondArray` is shorter than `firstArray`, the second position in the tuple
|
|
39
45
|
* will be `undefined`. Iteration always uses the length of the first array.
|
|
46
|
+
*
|
|
40
47
|
* @template FirstArrayItem
|
|
41
48
|
* @template SecondArrayItem
|
|
49
|
+
*
|
|
42
50
|
* @param firstArray - The first array. Each item in this will take up the first tuple spot.
|
|
43
51
|
* @param secondArray - The second array. Each item in this will take up the second tuple spot.
|
|
52
|
+
*
|
|
44
53
|
* @returns An array of `[firstItem, secondItem]` tuples for each index in `firstArray`.
|
|
45
54
|
*/
|
|
46
55
|
declare function paralleliseArrays<FirstArrayItem, SecondArrayItem>(firstArray: readonly FirstArrayItem[], secondArray: readonly SecondArrayItem[]): ParallelTuple<FirstArrayItem, SecondArrayItem>[];
|
|
47
56
|
//#endregion
|
|
48
57
|
//#region src/functions/arrayHelpers/randomiseArray.d.ts
|
|
49
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Randomises the order of a given array and returns the result in a new array without mutating the original.
|
|
60
|
+
*
|
|
61
|
+
* @template ItemType - The type of the array items.
|
|
62
|
+
*
|
|
63
|
+
* @param array - The array to randomise.
|
|
64
|
+
*
|
|
65
|
+
* @returns A new array with the items randomised.
|
|
66
|
+
*/
|
|
67
|
+
declare function randomiseArray<ItemType$1>(array: ItemType$1[]): ItemType$1[];
|
|
50
68
|
//#endregion
|
|
51
69
|
//#region src/functions/arrayHelpers/range.d.ts
|
|
70
|
+
/**
|
|
71
|
+
* Creates an array of numbers within a given range.
|
|
72
|
+
*
|
|
73
|
+
* The range is inclusive of `start` and exclusive of `stop`.
|
|
74
|
+
* The sign of `step` must match the direction of the range.
|
|
75
|
+
*
|
|
76
|
+
* @param start - The number to start at (inclusive).
|
|
77
|
+
* @param stop - The number to stop at (exclusive).
|
|
78
|
+
* @param step - The step size between numbers, defaulting to 1.
|
|
79
|
+
*
|
|
80
|
+
* @throws {Error} If `step` is `0`.
|
|
81
|
+
* @throws {Error} If `step` direction does not match the order of `start` and `stop`.
|
|
82
|
+
*
|
|
83
|
+
* @returns An array of numbers satisfying the range provided.
|
|
84
|
+
*/
|
|
52
85
|
declare function range(start: number, stop: number, step?: number): number[];
|
|
53
86
|
//#endregion
|
|
54
87
|
//#region src/functions/camelToKebab.d.ts
|
|
@@ -143,21 +176,73 @@ declare function createFormData<T extends Record<RecordKey, unknown>>(data: T, o
|
|
|
143
176
|
declare function createTemplateStringsArray(strings: readonly string[]): TemplateStringsArray;
|
|
144
177
|
//#endregion
|
|
145
178
|
//#region src/functions/date/addDaysToDate.d.ts
|
|
179
|
+
/**
|
|
180
|
+
* Adds a given number of days to the provided date, returning the result as a new instance of Date.
|
|
181
|
+
*
|
|
182
|
+
* @param currentDate - The starting date (defaults to today).
|
|
183
|
+
* @param dayIncrement - The amount of days you want to add (defaults to 1)
|
|
184
|
+
*
|
|
185
|
+
* @returns A new Date instance with the number of days added to the initially provided date.
|
|
186
|
+
*/
|
|
146
187
|
declare function addDaysToDate(currentDate?: Date, dayIncrement?: number): Date;
|
|
147
188
|
//#endregion
|
|
148
189
|
//#region src/functions/date/formatDateAndTime.d.ts
|
|
190
|
+
/**
|
|
191
|
+
* Creates a human-readable string with information about the input date.
|
|
192
|
+
*
|
|
193
|
+
* @param inputDate - The date to base the string on.
|
|
194
|
+
*
|
|
195
|
+
* @returns A new string with information about the given date.
|
|
196
|
+
*
|
|
197
|
+
* - If the date given is today, the output will be something like `Today at HH:MM`
|
|
198
|
+
* - If the date given happened yesterday, the output will be something like `Yesterday at HH:MM`
|
|
199
|
+
* - For any other date, the output will be something like `DD/MM/YYYY, HH:MM`
|
|
200
|
+
*/
|
|
149
201
|
declare function formatDateAndTime(inputDate: Date): string;
|
|
150
202
|
//#endregion
|
|
151
203
|
//#region src/functions/date/isAnniversary.d.ts
|
|
204
|
+
/**
|
|
205
|
+
* Checks if the provided dates are exactly a whole number of years apart.
|
|
206
|
+
*
|
|
207
|
+
* @param firstDate - The first date to compare.
|
|
208
|
+
* @param secondDate - The second date to compare.
|
|
209
|
+
*
|
|
210
|
+
* @returns True if the provided dates are exactly a whole number of years apart, and false otherwise.
|
|
211
|
+
*/
|
|
152
212
|
declare function isAnniversary(firstDate: Date, secondDate: Date): boolean;
|
|
153
213
|
//#endregion
|
|
154
214
|
//#region src/functions/date/isLeapYear.d.ts
|
|
215
|
+
/**
|
|
216
|
+
* Checks if the provided year is a leap year.
|
|
217
|
+
*
|
|
218
|
+
* @param year - The year to check as a number.
|
|
219
|
+
*
|
|
220
|
+
* @throws {TypeError} If the year provided is not an integer.
|
|
221
|
+
*
|
|
222
|
+
* @returns True if the year is a leap year, and false otherwise.
|
|
223
|
+
*/
|
|
155
224
|
declare function isLeapYear(year: number): boolean;
|
|
156
225
|
//#endregion
|
|
157
226
|
//#region src/functions/date/isMonthlyMultiple.d.ts
|
|
227
|
+
/**
|
|
228
|
+
* Checks if the provided dates are exactly a whole number of months apart.
|
|
229
|
+
*
|
|
230
|
+
* @param firstDate - The first date to compare.
|
|
231
|
+
* @param secondDate - The second date to compare.
|
|
232
|
+
*
|
|
233
|
+
* @returns True if the provided dates are exactly a whole number of months apart, and false otherwise.
|
|
234
|
+
*/
|
|
158
235
|
declare function isMonthlyMultiple(firstDate: Date, secondDate: Date): boolean;
|
|
159
236
|
//#endregion
|
|
160
237
|
//#region src/functions/date/isSameDate.d.ts
|
|
238
|
+
/**
|
|
239
|
+
* Checks if the provided dates happen on the exact same day, month, and year.
|
|
240
|
+
*
|
|
241
|
+
* @param firstDate - The first date to compare.
|
|
242
|
+
* @param secondDate - The second date to compare.
|
|
243
|
+
*
|
|
244
|
+
* @returns True if the provided dates occur on exactly the same day, month, and year, and returns false otherwise.
|
|
245
|
+
*/
|
|
161
246
|
declare function isSameDate(firstDate: Date, secondDate: Date): boolean;
|
|
162
247
|
//#endregion
|
|
163
248
|
//#region src/functions/deepCopy.d.ts
|
package/dist/index.d.ts
CHANGED
|
@@ -12,9 +12,12 @@ declare function appendSemicolon(stringToAppendTo: string): string;
|
|
|
12
12
|
*
|
|
13
13
|
* The callback will be invoked once for each index from `0` to `length - 1`.
|
|
14
14
|
* If no length is provided, a single-element array will be produced.
|
|
15
|
+
*
|
|
15
16
|
* @template ItemType
|
|
17
|
+
*
|
|
16
18
|
* @param callback - An asynchronous function invoked with the current index.
|
|
17
|
-
* @param
|
|
19
|
+
* @param length - The desired length of the resulting array.
|
|
20
|
+
*
|
|
18
21
|
* @returns A Promise resolving to an array of the callback results.
|
|
19
22
|
*/
|
|
20
23
|
declare function fillArray<ItemType$1>(callback: (index: number) => Promise<ItemType$1>, length?: number): Promise<ItemType$1[]>;
|
|
@@ -23,9 +26,12 @@ declare function fillArray<ItemType$1>(callback: (index: number) => Promise<Item
|
|
|
23
26
|
*
|
|
24
27
|
* The callback will be invoked once for each index from `0` to `length - 1`.
|
|
25
28
|
* If no length is provided, a single-element array will be produced.
|
|
29
|
+
*
|
|
26
30
|
* @template ItemType
|
|
31
|
+
*
|
|
27
32
|
* @param callback - A synchronous function invoked with the current index.
|
|
28
|
-
* @param
|
|
33
|
+
* @param length - The desired length of the resulting array.
|
|
34
|
+
*
|
|
29
35
|
* @returns An array of the callback results.
|
|
30
36
|
*/
|
|
31
37
|
declare function fillArray<ItemType$1>(callback: (index: number) => ItemType$1, length?: number): ItemType$1[];
|
|
@@ -37,18 +43,45 @@ type ParallelTuple<A, B> = [A, B | undefined];
|
|
|
37
43
|
*
|
|
38
44
|
* If `secondArray` is shorter than `firstArray`, the second position in the tuple
|
|
39
45
|
* will be `undefined`. Iteration always uses the length of the first array.
|
|
46
|
+
*
|
|
40
47
|
* @template FirstArrayItem
|
|
41
48
|
* @template SecondArrayItem
|
|
49
|
+
*
|
|
42
50
|
* @param firstArray - The first array. Each item in this will take up the first tuple spot.
|
|
43
51
|
* @param secondArray - The second array. Each item in this will take up the second tuple spot.
|
|
52
|
+
*
|
|
44
53
|
* @returns An array of `[firstItem, secondItem]` tuples for each index in `firstArray`.
|
|
45
54
|
*/
|
|
46
55
|
declare function paralleliseArrays<FirstArrayItem, SecondArrayItem>(firstArray: readonly FirstArrayItem[], secondArray: readonly SecondArrayItem[]): ParallelTuple<FirstArrayItem, SecondArrayItem>[];
|
|
47
56
|
//#endregion
|
|
48
57
|
//#region src/functions/arrayHelpers/randomiseArray.d.ts
|
|
49
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Randomises the order of a given array and returns the result in a new array without mutating the original.
|
|
60
|
+
*
|
|
61
|
+
* @template ItemType - The type of the array items.
|
|
62
|
+
*
|
|
63
|
+
* @param array - The array to randomise.
|
|
64
|
+
*
|
|
65
|
+
* @returns A new array with the items randomised.
|
|
66
|
+
*/
|
|
67
|
+
declare function randomiseArray<ItemType$1>(array: ItemType$1[]): ItemType$1[];
|
|
50
68
|
//#endregion
|
|
51
69
|
//#region src/functions/arrayHelpers/range.d.ts
|
|
70
|
+
/**
|
|
71
|
+
* Creates an array of numbers within a given range.
|
|
72
|
+
*
|
|
73
|
+
* The range is inclusive of `start` and exclusive of `stop`.
|
|
74
|
+
* The sign of `step` must match the direction of the range.
|
|
75
|
+
*
|
|
76
|
+
* @param start - The number to start at (inclusive).
|
|
77
|
+
* @param stop - The number to stop at (exclusive).
|
|
78
|
+
* @param step - The step size between numbers, defaulting to 1.
|
|
79
|
+
*
|
|
80
|
+
* @throws {Error} If `step` is `0`.
|
|
81
|
+
* @throws {Error} If `step` direction does not match the order of `start` and `stop`.
|
|
82
|
+
*
|
|
83
|
+
* @returns An array of numbers satisfying the range provided.
|
|
84
|
+
*/
|
|
52
85
|
declare function range(start: number, stop: number, step?: number): number[];
|
|
53
86
|
//#endregion
|
|
54
87
|
//#region src/functions/camelToKebab.d.ts
|
|
@@ -143,21 +176,73 @@ declare function createFormData<T extends Record<RecordKey, unknown>>(data: T, o
|
|
|
143
176
|
declare function createTemplateStringsArray(strings: readonly string[]): TemplateStringsArray;
|
|
144
177
|
//#endregion
|
|
145
178
|
//#region src/functions/date/addDaysToDate.d.ts
|
|
179
|
+
/**
|
|
180
|
+
* Adds a given number of days to the provided date, returning the result as a new instance of Date.
|
|
181
|
+
*
|
|
182
|
+
* @param currentDate - The starting date (defaults to today).
|
|
183
|
+
* @param dayIncrement - The amount of days you want to add (defaults to 1)
|
|
184
|
+
*
|
|
185
|
+
* @returns A new Date instance with the number of days added to the initially provided date.
|
|
186
|
+
*/
|
|
146
187
|
declare function addDaysToDate(currentDate?: Date, dayIncrement?: number): Date;
|
|
147
188
|
//#endregion
|
|
148
189
|
//#region src/functions/date/formatDateAndTime.d.ts
|
|
190
|
+
/**
|
|
191
|
+
* Creates a human-readable string with information about the input date.
|
|
192
|
+
*
|
|
193
|
+
* @param inputDate - The date to base the string on.
|
|
194
|
+
*
|
|
195
|
+
* @returns A new string with information about the given date.
|
|
196
|
+
*
|
|
197
|
+
* - If the date given is today, the output will be something like `Today at HH:MM`
|
|
198
|
+
* - If the date given happened yesterday, the output will be something like `Yesterday at HH:MM`
|
|
199
|
+
* - For any other date, the output will be something like `DD/MM/YYYY, HH:MM`
|
|
200
|
+
*/
|
|
149
201
|
declare function formatDateAndTime(inputDate: Date): string;
|
|
150
202
|
//#endregion
|
|
151
203
|
//#region src/functions/date/isAnniversary.d.ts
|
|
204
|
+
/**
|
|
205
|
+
* Checks if the provided dates are exactly a whole number of years apart.
|
|
206
|
+
*
|
|
207
|
+
* @param firstDate - The first date to compare.
|
|
208
|
+
* @param secondDate - The second date to compare.
|
|
209
|
+
*
|
|
210
|
+
* @returns True if the provided dates are exactly a whole number of years apart, and false otherwise.
|
|
211
|
+
*/
|
|
152
212
|
declare function isAnniversary(firstDate: Date, secondDate: Date): boolean;
|
|
153
213
|
//#endregion
|
|
154
214
|
//#region src/functions/date/isLeapYear.d.ts
|
|
215
|
+
/**
|
|
216
|
+
* Checks if the provided year is a leap year.
|
|
217
|
+
*
|
|
218
|
+
* @param year - The year to check as a number.
|
|
219
|
+
*
|
|
220
|
+
* @throws {TypeError} If the year provided is not an integer.
|
|
221
|
+
*
|
|
222
|
+
* @returns True if the year is a leap year, and false otherwise.
|
|
223
|
+
*/
|
|
155
224
|
declare function isLeapYear(year: number): boolean;
|
|
156
225
|
//#endregion
|
|
157
226
|
//#region src/functions/date/isMonthlyMultiple.d.ts
|
|
227
|
+
/**
|
|
228
|
+
* Checks if the provided dates are exactly a whole number of months apart.
|
|
229
|
+
*
|
|
230
|
+
* @param firstDate - The first date to compare.
|
|
231
|
+
* @param secondDate - The second date to compare.
|
|
232
|
+
*
|
|
233
|
+
* @returns True if the provided dates are exactly a whole number of months apart, and false otherwise.
|
|
234
|
+
*/
|
|
158
235
|
declare function isMonthlyMultiple(firstDate: Date, secondDate: Date): boolean;
|
|
159
236
|
//#endregion
|
|
160
237
|
//#region src/functions/date/isSameDate.d.ts
|
|
238
|
+
/**
|
|
239
|
+
* Checks if the provided dates happen on the exact same day, month, and year.
|
|
240
|
+
*
|
|
241
|
+
* @param firstDate - The first date to compare.
|
|
242
|
+
* @param secondDate - The second date to compare.
|
|
243
|
+
*
|
|
244
|
+
* @returns True if the provided dates occur on exactly the same day, month, and year, and returns false otherwise.
|
|
245
|
+
*/
|
|
161
246
|
declare function isSameDate(firstDate: Date, secondDate: Date): boolean;
|
|
162
247
|
//#endregion
|
|
163
248
|
//#region src/functions/deepCopy.d.ts
|
package/dist/index.js
CHANGED
|
@@ -22,9 +22,12 @@ var appendSemicolon_default = appendSemicolon;
|
|
|
22
22
|
*
|
|
23
23
|
* If the callback returns at least one Promise, the entire result will be wrapped
|
|
24
24
|
* in a `Promise` and resolved with `Promise.all`. Otherwise, a plain array is returned.
|
|
25
|
+
*
|
|
25
26
|
* @template ItemType
|
|
27
|
+
*
|
|
26
28
|
* @param callback - A function invoked with the current index. May return a value or a Promise.
|
|
27
|
-
* @param
|
|
29
|
+
* @param length - The desired length of the resulting array.
|
|
30
|
+
*
|
|
28
31
|
* @returns An array of the callback results, or a Promise resolving to one if the callback is async.
|
|
29
32
|
*/
|
|
30
33
|
function fillArray(callback, length = 1) {
|
|
@@ -45,10 +48,13 @@ var fillArray_default = fillArray;
|
|
|
45
48
|
*
|
|
46
49
|
* If `secondArray` is shorter than `firstArray`, the second position in the tuple
|
|
47
50
|
* will be `undefined`. Iteration always uses the length of the first array.
|
|
51
|
+
*
|
|
48
52
|
* @template FirstArrayItem
|
|
49
53
|
* @template SecondArrayItem
|
|
54
|
+
*
|
|
50
55
|
* @param firstArray - The first array. Each item in this will take up the first tuple spot.
|
|
51
56
|
* @param secondArray - The second array. Each item in this will take up the second tuple spot.
|
|
57
|
+
*
|
|
52
58
|
* @returns An array of `[firstItem, secondItem]` tuples for each index in `firstArray`.
|
|
53
59
|
*/
|
|
54
60
|
function paralleliseArrays(firstArray, secondArray) {
|
|
@@ -84,6 +90,15 @@ var getRandomNumber_default = getRandomNumber;
|
|
|
84
90
|
|
|
85
91
|
//#endregion
|
|
86
92
|
//#region src/functions/arrayHelpers/randomiseArray.ts
|
|
93
|
+
/**
|
|
94
|
+
* Randomises the order of a given array and returns the result in a new array without mutating the original.
|
|
95
|
+
*
|
|
96
|
+
* @template ItemType - The type of the array items.
|
|
97
|
+
*
|
|
98
|
+
* @param array - The array to randomise.
|
|
99
|
+
*
|
|
100
|
+
* @returns A new array with the items randomised.
|
|
101
|
+
*/
|
|
87
102
|
function randomiseArray(array) {
|
|
88
103
|
const mutableArray = [...array];
|
|
89
104
|
const outputArray = [];
|
|
@@ -97,6 +112,21 @@ var randomiseArray_default = randomiseArray;
|
|
|
97
112
|
|
|
98
113
|
//#endregion
|
|
99
114
|
//#region src/functions/arrayHelpers/range.ts
|
|
115
|
+
/**
|
|
116
|
+
* Creates an array of numbers within a given range.
|
|
117
|
+
*
|
|
118
|
+
* The range is inclusive of `start` and exclusive of `stop`.
|
|
119
|
+
* The sign of `step` must match the direction of the range.
|
|
120
|
+
*
|
|
121
|
+
* @param start - The number to start at (inclusive).
|
|
122
|
+
* @param stop - The number to stop at (exclusive).
|
|
123
|
+
* @param step - The step size between numbers, defaulting to 1.
|
|
124
|
+
*
|
|
125
|
+
* @throws {Error} If `step` is `0`.
|
|
126
|
+
* @throws {Error} If `step` direction does not match the order of `start` and `stop`.
|
|
127
|
+
*
|
|
128
|
+
* @returns An array of numbers satisfying the range provided.
|
|
129
|
+
*/
|
|
100
130
|
function range(start, stop, step = 1) {
|
|
101
131
|
const numbers = [];
|
|
102
132
|
if (step === 0) throw new Error("ZERO_STEP_SIZE_NOT_ALLOWED");
|
|
@@ -205,8 +235,16 @@ var createTemplateStringsArray_default = createTemplateStringsArray;
|
|
|
205
235
|
|
|
206
236
|
//#endregion
|
|
207
237
|
//#region src/functions/date/addDaysToDate.ts
|
|
238
|
+
/**
|
|
239
|
+
* Adds a given number of days to the provided date, returning the result as a new instance of Date.
|
|
240
|
+
*
|
|
241
|
+
* @param currentDate - The starting date (defaults to today).
|
|
242
|
+
* @param dayIncrement - The amount of days you want to add (defaults to 1)
|
|
243
|
+
*
|
|
244
|
+
* @returns A new Date instance with the number of days added to the initially provided date.
|
|
245
|
+
*/
|
|
208
246
|
function addDaysToDate(currentDate = /* @__PURE__ */ new Date(), dayIncrement = 1) {
|
|
209
|
-
const newDate = currentDate;
|
|
247
|
+
const newDate = new Date(currentDate);
|
|
210
248
|
newDate.setDate(newDate.getDate() + dayIncrement);
|
|
211
249
|
return newDate;
|
|
212
250
|
}
|
|
@@ -214,6 +252,14 @@ var addDaysToDate_default = addDaysToDate;
|
|
|
214
252
|
|
|
215
253
|
//#endregion
|
|
216
254
|
//#region src/functions/date/isSameDate.ts
|
|
255
|
+
/**
|
|
256
|
+
* Checks if the provided dates happen on the exact same day, month, and year.
|
|
257
|
+
*
|
|
258
|
+
* @param firstDate - The first date to compare.
|
|
259
|
+
* @param secondDate - The second date to compare.
|
|
260
|
+
*
|
|
261
|
+
* @returns True if the provided dates occur on exactly the same day, month, and year, and returns false otherwise.
|
|
262
|
+
*/
|
|
217
263
|
function isSameDate(firstDate, secondDate) {
|
|
218
264
|
return firstDate.getDate() === secondDate.getDate() && firstDate.getMonth() === secondDate.getMonth() && firstDate.getFullYear() === secondDate.getFullYear();
|
|
219
265
|
}
|
|
@@ -221,6 +267,17 @@ var isSameDate_default = isSameDate;
|
|
|
221
267
|
|
|
222
268
|
//#endregion
|
|
223
269
|
//#region src/functions/date/formatDateAndTime.ts
|
|
270
|
+
/**
|
|
271
|
+
* Creates a human-readable string with information about the input date.
|
|
272
|
+
*
|
|
273
|
+
* @param inputDate - The date to base the string on.
|
|
274
|
+
*
|
|
275
|
+
* @returns A new string with information about the given date.
|
|
276
|
+
*
|
|
277
|
+
* - If the date given is today, the output will be something like `Today at HH:MM`
|
|
278
|
+
* - If the date given happened yesterday, the output will be something like `Yesterday at HH:MM`
|
|
279
|
+
* - For any other date, the output will be something like `DD/MM/YYYY, HH:MM`
|
|
280
|
+
*/
|
|
224
281
|
function formatDateAndTime(inputDate) {
|
|
225
282
|
const yesterday = addDaysToDate_default(/* @__PURE__ */ new Date(), -1);
|
|
226
283
|
const today = /* @__PURE__ */ new Date();
|
|
@@ -233,6 +290,15 @@ var formatDateAndTime_default = formatDateAndTime;
|
|
|
233
290
|
|
|
234
291
|
//#endregion
|
|
235
292
|
//#region src/functions/date/isLeapYear.ts
|
|
293
|
+
/**
|
|
294
|
+
* Checks if the provided year is a leap year.
|
|
295
|
+
*
|
|
296
|
+
* @param year - The year to check as a number.
|
|
297
|
+
*
|
|
298
|
+
* @throws {TypeError} If the year provided is not an integer.
|
|
299
|
+
*
|
|
300
|
+
* @returns True if the year is a leap year, and false otherwise.
|
|
301
|
+
*/
|
|
236
302
|
function isLeapYear(year) {
|
|
237
303
|
const parsedYear = parseIntStrict_default(`${year}`);
|
|
238
304
|
return parsedYear % 4 === 0 && parsedYear % 100 !== 0 || parsedYear % 400 === 0;
|
|
@@ -245,6 +311,14 @@ function checkLeapYear(firstDate, secondDate) {
|
|
|
245
311
|
if (isLeapYear_default(firstDate.getFullYear()) && firstDate.getMonth() === 1 && secondDate.getMonth() === 1) return firstDate.getDate() === 29 && secondDate.getDate() === 28;
|
|
246
312
|
return false;
|
|
247
313
|
}
|
|
314
|
+
/**
|
|
315
|
+
* Checks if the provided dates are exactly a whole number of years apart.
|
|
316
|
+
*
|
|
317
|
+
* @param firstDate - The first date to compare.
|
|
318
|
+
* @param secondDate - The second date to compare.
|
|
319
|
+
*
|
|
320
|
+
* @returns True if the provided dates are exactly a whole number of years apart, and false otherwise.
|
|
321
|
+
*/
|
|
248
322
|
function isAnniversary(firstDate, secondDate) {
|
|
249
323
|
if (checkLeapYear(firstDate, secondDate) || checkLeapYear(secondDate, firstDate)) return true;
|
|
250
324
|
return firstDate.getDate() === secondDate.getDate() && firstDate.getMonth() === secondDate.getMonth();
|
|
@@ -283,6 +357,14 @@ function leapYearFebruaryChecks(firstDate, secondDate) {
|
|
|
283
357
|
}
|
|
284
358
|
return false;
|
|
285
359
|
}
|
|
360
|
+
/**
|
|
361
|
+
* Checks if the provided dates are exactly a whole number of months apart.
|
|
362
|
+
*
|
|
363
|
+
* @param firstDate - The first date to compare.
|
|
364
|
+
* @param secondDate - The second date to compare.
|
|
365
|
+
*
|
|
366
|
+
* @returns True if the provided dates are exactly a whole number of months apart, and false otherwise.
|
|
367
|
+
*/
|
|
286
368
|
function isMonthlyMultiple(firstDate, secondDate) {
|
|
287
369
|
if (endOfMonthChecksButNotFebruary(firstDate, secondDate) || endOfMonthChecksButNotFebruary(secondDate, firstDate)) return true;
|
|
288
370
|
if (nonLeapYearFebruaryChecks(firstDate, secondDate) || nonLeapYearFebruaryChecks(secondDate, firstDate)) return true;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alextheman/utility",
|
|
3
|
-
"version": "3.5.
|
|
3
|
+
"version": "3.5.10",
|
|
4
4
|
"description": "Helpful utility functions",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"zod": "^4.1.13"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@alextheman/eslint-plugin": "^4.
|
|
22
|
+
"@alextheman/eslint-plugin": "^4.8.0",
|
|
23
23
|
"@types/node": "^25.0.1",
|
|
24
24
|
"dotenv-cli": "^11.0.0",
|
|
25
25
|
"eslint": "^9.39.1",
|