@akadenia/helpers 1.2.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Akadenia
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.MD ADDED
@@ -0,0 +1,476 @@
1
+ ## Getting Started
2
+
3
+ - [DateHelpers](#datehelpers)
4
+ - [`DateHelpers.getReadableDateTime(datetime)`](#datehelpersgetreadabledatetimedatetime)
5
+ - [`DateHelpers.getReadableDate(datetime)`](#datehelpersgetreadabledatedatetime)
6
+ - [`DateHelpers.getDateString(date)`](#datehelpersgetdatestringdate)
7
+ - [`DateHelpers.parseDate(date)`](#datehelpersparsedatedate)
8
+ - [`DateHelpers.getShortOrdinalDate(date)`](#datehelpersgetshortordinaldatedate)
9
+ - [MapHelpers](#maphelpers)
10
+ - [`MapHelpers.getDistanceBetweenPoints(point1, point2)`](#maphelpersgetdistancebetweenpointspoint1-point2)
11
+ - [`MapHelpers.compareLocations(location1, location2, precision)`](#maphelperscomparelocationslocation1-location2-precision)
12
+ - [`MapHelpers.getBearingToCoordinate({startCoordinate, endCoordinate})`](#maphelpersgetbearingtocoordinatestartcoordinate-endcoordinate)
13
+ - [TextHelpers](#texthelpers)
14
+ - [`TextHelpers.uuidv4()`](#texthelpersuuidv4)
15
+ - [`TextHelpers.formatPosition(position)`](#texthelpersformatpositionposition)
16
+ - [`TextHelpers.truncateText(text, characterLimit)`](#texthelperstruncatetexttext-characterlimit)
17
+ - [`TextHelpers.fileNameFromPath(path)`](#texthelpersfilenamefrompathpath)
18
+ - [`TextHelpers.replaceSpacesWithUnderscore(s)`](#texthelpersreplacespaceswithunderscores)
19
+ - [`TextHelpers.replaceUnderscoreWithSpaces(s)`](#texthelpersreplaceunderscorewithspacess)
20
+ - [`TextHelpers.pluralizeOnCondition(word, condition)`](#texthelperspluralizeonconditionword-condition)
21
+ - [`TextHelpers.convertSnakeToCamelCase(data)`](#texthelpersconvertsnaketocamelcasedata)
22
+ - [`TextHelpers.convertCamelToSnakeCase(data)`](#texthelpersconvertcameltosnakecasedata)
23
+ - [`TextHelpers.convertCamelToKebabCase(data)`](#texthelpersconvertcameltokebabcasedata)
24
+ - [`TextHelpers.convertKebabToCamelCase(data)`](#texthelpersconvertkebabtocamelcasedata)
25
+ - [`TextHelpers.isAcronym(data)`](#texthelpersisacronymdata)
26
+ - [`TextHelpers.acronymToKebabCase(data)`](#texthelpersacronymtokebabcasedata)
27
+ - [`TextHelpers.handleNullDisplay(value, defaultValue="N/A")`](#texthelpershandlenulldisplayvalue-defaultvaluena)
28
+ - [`TextHelpers.enforceCharacterLimit({text, characterLimit, onCharacterLimit})`](#texthelpersenforcecharacterlimittext-characterlimit-oncharacterlimit)
29
+ - [`TextHelpers.isValidEmail(email)`](#texthelpersisvalidemailemail)
30
+ - [`TextHelpers.generateIdFromWord(word)`](#texthelpersgenerateidfromwordword)
31
+ - [`TextHelpers.generateWordFromId(id)`](#texthelpersgeneratewordfromidid)
32
+ - [ObjectHelpers](#objecthelpers)
33
+ - [`ObjectHelpers.isPureObject(any)`](#objecthelpersispureobjectany)
34
+ - [`ObjectHelpers.parseCookie(str)`](#objecthelpersparsecookiestr)
35
+ - [`ObjectHelpers.filterObjectsByProperty ( array, propertyName, propertyValue)`](#objecthelpersfilterobjectsbyproperty--array-propertyname-propertyvalue)
36
+ - [`ObjectHelpers.containsSubObject(mainObject, subObject)`](#objecthelperscontainssubobjectmainobject-subobject)
37
+ - [`ObjectHelpers.findObjectBySubObject(array, subObject)`](#objecthelpersfindobjectbysubobjectarray-subobject)
38
+ - [`ObjectHelpers.filterObjectsBySubObject(array, subObject)`](#objecthelpersfilterobjectsbysubobjectarray-subobject)
39
+ - [`ObjectHelpers.objectPropHasValue({object, key, value)}`](#objecthelpersobjectkeyhasvalueobject-key-value)
40
+ - [`ObjectHelpers.findEntry(array, predicate)`](#objecthelpersfindentryarray-predicate)
41
+ - [GenericHelpers](#generichelpers)
42
+ - [`GenericHelpers.delay(ms)`](#generichelpersdelayms)
43
+ - [`GenericHelpers.getPreferredUriScheme(host)`](#generichelpersgetpreferredurischemehost)
44
+ - [FileHelpers](#filehelpers)
45
+ - [`FileHelpers.checkFileExtension(filePath, validExtensions)`](#filehelperscheckfileextensionfilepath-validextensions)
46
+
47
+ # DateHelpers
48
+
49
+ Import the DateHelpers utility.
50
+
51
+ `import { DateHelpers } from "@akadenia/helpers`
52
+
53
+ ## `DateHelpers.getReadableDateTime(datetime)`
54
+
55
+ Returns the date in `yyyy-mm-dd hh:mm:ss` format.
56
+
57
+ Arguments
58
+ |Name|Type|Required|Description|
59
+ |--|--|--|--|
60
+ |`datetime`|`Date`|`false`|The date to convert to readable date time. Default is current datetime. i.e. `new Date()`|
61
+
62
+ ## `DateHelpers.getReadableDate(datetime)`
63
+
64
+ Returns the date in `yyyy-mm-dd` format.
65
+
66
+ Arguments
67
+ |Name|Type|Required|Description|
68
+ |--|--|--|--|
69
+ |`datetime`|`Date`|`false`|The date to convert to readable date time. Default is current datetime. i.e. `new Date()`|
70
+
71
+ ## `DateHelpers.getDateString(date)`
72
+
73
+ Returns a string representing the date in the user's local timezone.
74
+
75
+ Arguments
76
+ |Name|Type|Required|Description|
77
+ |--|--|--|--|
78
+ |`date`|`string` or `Date`|`true`|The date string to convert to the string in the user's local timezone|
79
+
80
+ ## `DateHelpers.parseDate(date)`
81
+
82
+ Returns a date representing the new date object.
83
+
84
+ Arguments
85
+ |Name|Type|Required|Description|
86
+ |--|--|--|--|
87
+ |`date`|`string`|`true`|The date string that needs to be returned as a date object|
88
+
89
+ ## `DateHelpers.getShortOrdinalDate(date)`
90
+
91
+ Returns a string representing the date in short ordinal format.
92
+
93
+ Arguments
94
+ |Name|Type|Required|Description|
95
+ |--|--|--|--|
96
+ |`date`|`string`|`true`|The date string that needs to be formatted|
97
+
98
+ # MapHelpers
99
+
100
+ Import the MapHelpers utility.
101
+
102
+ `import { MapHelpers } from "@akadenia/helpers`
103
+
104
+ ## `MapHelpers.getDistanceBetweenPoints(point1, point2)`
105
+
106
+ Returns the distance between two points in meters. Returns `null` if either point is invalid.
107
+
108
+ Arguments
109
+ |Name|Type|Required|Description|
110
+ |--|--|--|--|
111
+ |`point1`|`Array<number>`|false| The coordinates of the first point. i.e. `[lat, lng]` |
112
+ |`point2`|`Array<number>`|false|The coordinates of the second point. i.e. `[lat, lng]`|
113
+
114
+ ## `MapHelpers.compareLocations(location1, location2, precision)`
115
+
116
+ Returns a boolean value that indicates whether or not the two locations are the same based on precision provided.
117
+
118
+ Arguments
119
+ |Name|Type|Required|Description|
120
+ |--|--|--|--|
121
+ |`location1`|`Array<number>`|false| The coordinates of the first location. i.e. `[lat, lng]` |
122
+ |`location2`|`Array<number>`|false|The coordinates of the second location. i.e. `[lat, lng]`|
123
+ |`precision`|`number`|false|The precision to compare the locations to. Default is `6`|
124
+
125
+ ## `MapHelpers.getBearingToCoordinate({startCoordinate, endCoordinate})`
126
+
127
+ Returns the bearing to the end coordinate from the start coordinate in degrees.
128
+
129
+ Arguments
130
+ |Name|Type|Required|Description|
131
+ |--|--|--|--|
132
+ |`coordinates`|`object`|`true`|An object containing the start and end coordinates. i.e. `{startCoordinate: [lat, lng], endCoordinate: [lat, lng]}`|
133
+
134
+ # TextHelpers
135
+
136
+ Import the TextHelpers utility.
137
+
138
+ `import { TextHelpers } from "@akadenia/helpers`
139
+
140
+ ## `TextHelpers.uuidv4()`
141
+
142
+ Returns a randomly generated string.
143
+
144
+ Arguments
145
+ |Name|Type|Required|Description|
146
+ |--|--|--|--|
147
+
148
+ ## `TextHelpers.formatPosition(position)`
149
+
150
+ Returns a string position for a provided number. eg. `"2nd"`, `"3rd"` etc.
151
+
152
+ Arguments
153
+ |Name|Type|Required|Description|
154
+ |--|--|--|--|
155
+ |`position`|`number`|`true`|A position number to add the appropriate suffix to|
156
+
157
+ ## `TextHelpers.truncateText(text, characterLimit)`
158
+
159
+ Returns a string ending in an ellipsis `...` or returns the input string if maximum character limit isn't met.
160
+
161
+ Arguments
162
+ |Name|Type|Required|Description|
163
+ |--|--|--|--|
164
+ |`text`|`string`|`true`|The text to truncate|
165
+ |`characterLimit`|`number`|`true`|The target number of characters to truncate text to|
166
+
167
+ ## `TextHelpers.fileNameFromPath(path)`
168
+
169
+ Returns a file name.
170
+
171
+ Arguments
172
+ |Name|Type|Required|Description|
173
+ |--|--|--|--|
174
+ |`path`|`string`|`true`|The path to extract the file name from|
175
+
176
+ ## `TextHelpers.replaceSpacesWithUnderscore(s)`
177
+
178
+ Returns a string where the spaces have been replaced with underscores.
179
+
180
+ Arguments
181
+ |Name|Type|Required|Description|
182
+ |--|--|--|--|
183
+ |`s`|`string`|`false`|The text to replace spaces with underscores in|
184
+
185
+ ## `TextHelpers.replaceUnderscoreWithSpaces(s)`
186
+
187
+ Returns a string where the underscores have been replaced with spaces.
188
+
189
+ Arguments
190
+ |Name|Type|Required|Description|
191
+ |--|--|--|--|
192
+ |`s`|`string`|`false`|The text to replace underscores with spaces in|
193
+
194
+ ## `TextHelpers.pluralizeOnCondition(word, condition)`
195
+
196
+ Returns a pluralised version of the input string or returns the same string.
197
+
198
+ Arguments
199
+ |Name|Type|Required|Description|
200
+ |--|--|--|--|
201
+ |`word`|`string`|`true`|The word to pluralise|
202
+ |`condition`|`boolean`|`true`|The condition that determines whether to plurize the input word|
203
+
204
+ ## `TextHelpers.convertSnakeToCamelCase(data)`
205
+
206
+ Returns the input object or array with all object keys recursively transformed to camelCase. Returns input object if type is invalid.
207
+
208
+ Arguments
209
+ |Name|Type|Required|Description|
210
+ |--|--|--|--|
211
+ |`data`|`Object` or `Array<Object>`|`true`|The object whose keys to transform|
212
+
213
+ ## `TextHelpers.convertCamelToSnakeCase(data)`
214
+
215
+ Returns the input object or array with all object keys recursively transformed to snake_case. Returns input object if type is invalid.
216
+
217
+ Arguments
218
+ |Name|Type|Required|Description|
219
+ |--|--|--|--|
220
+ |`data`|`Object` or `Array<Object>`|`true`|The object whose keys to transform|
221
+
222
+ ## `TextHelpers.convertCamelToKebabCase(data)`
223
+
224
+ Convert camel case to kebab case
225
+ The word returned as kebab case
226
+
227
+ Arguments
228
+ |Name|Type|Required|Description|
229
+ |--|--|--|--|
230
+ |`data`|`string`|The word needed to be converted to kebab case from camel case|
231
+
232
+ ## `TextHelpers.convertKebabToCamelCase(data)`
233
+
234
+ Convert kebab case to camel case
235
+ The word returned as camel case
236
+
237
+ Arguments
238
+ |Name|Type|Required|Description|
239
+ |--|--|--|--|
240
+ |`data`|`string`|The word needed to be converted from kebab case to camel case|
241
+
242
+
243
+ ## `TextHelpers.isAcronym(data)`
244
+
245
+ Validate if a word is acronym
246
+ The boolean value when the condition is met
247
+
248
+ Arguments
249
+ |Name|Type|Required|Description|
250
+ |--|--|--|--|
251
+ |`data`|`string`|The word that is being validated as acronym|
252
+
253
+
254
+ ## `TextHelpers.acronymToKebabCase(data)`
255
+
256
+ Convert acronym to kebab case
257
+ The word returned as camel case
258
+
259
+ Arguments
260
+ |Name|Type|Required|Description|
261
+ |--|--|--|--|
262
+ |`data`|`string`|The acronym to be converted to kebab case|
263
+
264
+
265
+ ## `TextHelpers.handleNullDisplay(value, defaultValue="N/A")`
266
+
267
+ Returns the input string or the default string if the input value is null or undefined.
268
+ Arguments
269
+ |Name|Type|Required|Description|
270
+ |--|--|--|--|
271
+ |`value`|`string`|`true`|The string to display in the ui|
272
+ |`defaultValue`|`string`|`false`|The default string to display if the input value is null or undefined defaults to `N/A`|
273
+
274
+ ## `TextHelpers.enforceCharacterLimit({text, characterLimit, onCharacterLimit})`
275
+
276
+ Returns the input string if the character limit is not exceeded or returns the truncated string when the character limit is exceeded. Calls the `onCharacterLimit` callback when the character limit is exceeded.
277
+ Arguments
278
+ |Name|Type|Required|Description|
279
+ |--|--|--|--|
280
+ |`text`|`string`|`true`|The string to truncate|
281
+ |`characterLimit`|`number`|`true`|The target number of characters to truncate text to|
282
+ |`onCharacterLimit`|`function`|`true`|The callback to call when the character limit is exceeded|
283
+
284
+ ## `TextHelpers.isValidEmail(email)`
285
+
286
+ Returns true when the email is a proper email and returns false when the email is not a proper email
287
+ Arguments
288
+ |Name|Type|Required|Description|
289
+ |--|--|--|--|
290
+ |`email`|`string`|`true`|The email to be validated|
291
+
292
+ ## `TextHelpers.generateIdFromWord(word)`
293
+
294
+ Generate ID from word
295
+ The generated ID from the word
296
+
297
+ Arguments
298
+ |Name|Type|Required|Description|
299
+ |--|--|--|--|
300
+ |`word`|`string`|`true`|The word needed to generate ID from|
301
+
302
+ ## `TextHelpers.generateWordFromId(word)`
303
+
304
+ Get the word from the generated ID
305
+ The word that is got from the id
306
+
307
+ Arguments
308
+ |Name|Type|Required|Description|
309
+ |--|--|--|--|
310
+ |`word`|`string`|`true`|The id that is needed to get the word from|
311
+ |`customList`|`Record<string, string>`|`false`|The custom lists that has the id with the custom list of words|
312
+
313
+
314
+ # ObjectHelpers
315
+
316
+ Import the ObjectHelpers utility.
317
+
318
+ `import { ObjectHelpers } from "@akadenia/helpers`
319
+
320
+ ## `ObjectHelpers.isPureObject(any)`
321
+
322
+ Returns a boolean indicating whether or not the input is a pure object.
323
+
324
+ Arguments
325
+ |Name|Type|Required|Description|
326
+ |--|--|--|--|
327
+ |`object`|`any`|`true`|The object to test for pureness|
328
+
329
+ ## `ObjectHelpers.parseCookie(str)`
330
+
331
+ Returns an object containing the key-value pairs of the parsed cookies.
332
+
333
+ Arguments
334
+ |Name|Type|Required|Description|
335
+ |--|--|--|--|
336
+ |`str`|`string`|`true`|The string of cookies to parse|
337
+
338
+ ## `ObjectHelpers.filterObjectsByProperty ( array, propertyName, propertyValue)`
339
+
340
+ Filter an array of objects based on a specific property and its corresponding value.
341
+ Returns an array containing all objects that have the specified property with the given value.
342
+
343
+ Template
344
+ T extends Record<string, any>
345
+
346
+ Arguments
347
+ |Name|Type|Required|Description|
348
+ |--|--|--|--|
349
+ |`array`|`T[]`|`true`|An array of objects to be searched|
350
+ |`propertyName`|`keyof T`|`true`|The name of the property to match against|
351
+ |`propertyValue`|`T[keyof T]`|`true`|The value that the property should have to be considered|
352
+
353
+ ## `ObjectHelpers.containsSubObject(mainObject, subObject)`
354
+
355
+ It compares the key-value pairs of the sub-object with the corresponding key-value pairs in the main object.
356
+ Returns true If all key-value pairs in the sub-object are found in the main object; otherwise, it returns false.
357
+
358
+ Template
359
+ T extends Record<string, any>
360
+
361
+ Arguments
362
+ |Name|Type|Required|Description|
363
+ |--|--|--|--|
364
+ |`mainObject`|`T`|`true`|The main object to be checked|
365
+ |`subObject`|`Partial<T>`|`true`|The sub-object to be searched for|
366
+
367
+ ## `ObjectHelpers.findObjectBySubObject(array, subObject)`
368
+
369
+ Searches an array of objects for an object that contains a specific sub-object.
370
+ Returns the first object found in the array that contains the sub-object or null
371
+ if no match is found.
372
+
373
+ Template
374
+ T extends Record<string, any>
375
+
376
+ Arguments
377
+ |Name|Type|Required|Description|
378
+ |--|--|--|--|
379
+ |`array`|`T[]`|`true`|An array of objects to be searched|
380
+ |`array`|`Partial<T>`|`true`|The sub-object to be searched for in the array|
381
+
382
+ ## `ObjectHelpers.filterObjectsBySubObject(array, subObject)`
383
+
384
+ Filters an array of objects based on the presence of a specific sub-object within each object.
385
+ Returns an array containing all objects from the input array that contain the specified sub-object.
386
+
387
+ Template
388
+ T extends Record<string, any>
389
+
390
+ Arguments
391
+ |Name|Type|Required|Description|
392
+ |--|--|--|--|
393
+ |`array`|`T[]`|`true`|An array of objects to be filtered|
394
+ |`array`|`Partial<T>`|`true`|The sub-object to be searched for in the array|
395
+ ## `ObjectHelpers.objectPropHasValue({object, key, value)}`
396
+ Checks if the object has the key with the value.
397
+
398
+ Arguments
399
+ |Name|Type|Required|Description|
400
+ |--|--|--|--|
401
+ |`object`|`Object`|`true`|The object to check|
402
+ |`key`|`string`|`true`|The key to check|
403
+ |`value`|`any`|`true`|The value to check|
404
+
405
+ ## `ObjectHelpers.findEntry(array, predicate)`
406
+ Finds the first entry in the array that satisfies the predicate.
407
+
408
+ Arguments
409
+ |Name|Type|Required|Description|
410
+ |--|--|--|--|
411
+ |`array`|`Array<any>`|`true`|The array of entries to search|
412
+ |`predicate`|`(item: any) => boolean`|`true`|The predicate function to use to search the array|
413
+
414
+ # GenericHelpers
415
+
416
+ Import the GenericHelpers utility.
417
+
418
+ `import { GenericHelpers } from "@akadenia/helpers`
419
+
420
+ ## `GenericHelpers.delay(ms)`
421
+
422
+ A "modern" sleep statement.
423
+
424
+ Arguments
425
+ |Name|Type|Required|Description|
426
+ |--|--|--|--|
427
+ |`ms`|`number`|`true`|The number of milliseconds to wait|
428
+
429
+ ## `GenericHelpers.getPreferredUriScheme(host)`
430
+
431
+ Get the preferred scheme for a host
432
+
433
+ Arguments
434
+ |Name|Type|Required|Description|
435
+ |--|--|--|--|
436
+ |`host`|`string`|`true`|An IP address or a domain name|
437
+
438
+ # FileHelpers
439
+
440
+ ## `FileHelpers.checkFileExtension(filePath, validExtensions)`
441
+
442
+ Check if a file path has a valid extension
443
+
444
+ Arguments
445
+ |Name|Type|Required|Description|
446
+ |--|--|--|--|
447
+ |`filePath`|`string`|`true`|The file path to check|
448
+ |`validExtensions`|`string[]`|`true`|The valid extensions|
449
+
450
+
451
+ ## Contributing to this package
452
+
453
+ This package follows the commitlint and conventional commits standards. As such the commit messages should follow the following format:
454
+
455
+ ```text
456
+ <type>(optional scope): <description> eg: feat: add new feature OR fix(map-loading): fix rendering on ipad devices
457
+ ```
458
+
459
+ Common types according to [commitlint-config-conventional (based on the Angular convention)](https://github.com/conventional-changelog/commitlint/tree/master/@commitlint/config-conventional#type-enum) can be:
460
+
461
+ - build
462
+ - chore
463
+ - ci
464
+ - docs
465
+ - feat
466
+ - fix
467
+ - perf
468
+ - refactor
469
+ - revert
470
+ - style
471
+ - test
472
+ - BREAKING CHANGE
473
+
474
+ ## License
475
+
476
+ MIT
package/dist/date.d.ts ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ *
3
+ * @function
4
+ * @param {Date} datetime - The datetime that needs to be formatted.
5
+ * @returns {string} - A string representing the datetime in the format "yyyy-mm-dd hh:mm:ss"
6
+ */
7
+ export declare const getReadableDateTime: (datetime?: Date) => string;
8
+ /**
9
+ *
10
+ * @function
11
+ * @param {Date} datetime - The datetime that needs to be formatted.
12
+ * @returns {string} - A string representing the date in the format "yyyy-mm-dd"
13
+ */
14
+ export declare const getReadableDate: (datetime?: Date) => string;
15
+ /**
16
+ *
17
+ * @function
18
+ * @param {string | Date} date - The date string that needs to be formatted
19
+ * @returns {string} - A string representing the date in the user's local timezone
20
+ */
21
+ export declare const getDateString: (date: string | Date) => string;
22
+ /**
23
+ *
24
+ * @function
25
+ * @param {string | Date} date - The date string that needs to be formatted
26
+ * @param {Intl.DateTimeFormatOptions} options - The javascript date formatting options to be used
27
+ * @param {string | Intl.Locales | undefined} localTimezone - The local timezone to be used
28
+ * @returns {string} - A string representing the date in the user's local timezone
29
+ */
30
+ export declare const formatDateTime: (date: string | Date, options: Intl.DateTimeFormatOptions, localTimezone?: string | Intl.Locale) => string;
31
+ /**
32
+ *
33
+ * @function
34
+ * @param {string | Date} date - The date string that needs to be returned as a Date object.
35
+ * @returns {Date} - A date representing the new date object.
36
+ */
37
+ export declare function parseDate(date: string | Date): Date;
38
+ /**
39
+ *
40
+ * @function
41
+ * @param {string | Date} date - The date string that needs to be formatted
42
+ * @returns {string} - A string representing the date in the short ordinal date
43
+ */
44
+ export declare function getShortOrdinalDate(date: string | Date): string;
package/dist/date.js ADDED
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getShortOrdinalDate = exports.parseDate = exports.formatDateTime = exports.getDateString = exports.getReadableDate = exports.getReadableDateTime = void 0;
4
+ /**
5
+ *
6
+ * @function
7
+ * @param {Date} datetime - The datetime that needs to be formatted.
8
+ * @returns {string} - A string representing the datetime in the format "yyyy-mm-dd hh:mm:ss"
9
+ */
10
+ const getReadableDateTime = (datetime = new Date()) => {
11
+ const dateString = new Date(datetime.getTime() - datetime.getTimezoneOffset() * 60000);
12
+ return dateString.toISOString().replace("T", " ").substring(0, 19);
13
+ };
14
+ exports.getReadableDateTime = getReadableDateTime;
15
+ /**
16
+ *
17
+ * @function
18
+ * @param {Date} datetime - The datetime that needs to be formatted.
19
+ * @returns {string} - A string representing the date in the format "yyyy-mm-dd"
20
+ */
21
+ const getReadableDate = (datetime = new Date()) => (0, exports.getReadableDateTime)(datetime).substring(0, 10);
22
+ exports.getReadableDate = getReadableDate;
23
+ /**
24
+ *
25
+ * @function
26
+ * @param {string | Date} date - The date string that needs to be formatted
27
+ * @returns {string} - A string representing the date in the user's local timezone
28
+ */
29
+ const getDateString = (date) => new Date(date).toLocaleDateString();
30
+ exports.getDateString = getDateString;
31
+ /**
32
+ *
33
+ * @function
34
+ * @param {string | Date} date - The date string that needs to be formatted
35
+ * @param {Intl.DateTimeFormatOptions} options - The javascript date formatting options to be used
36
+ * @param {string | Intl.Locales | undefined} localTimezone - The local timezone to be used
37
+ * @returns {string} - A string representing the date in the user's local timezone
38
+ */
39
+ const formatDateTime = (date, options, localTimezone = "en-US") => new Date(date).toLocaleDateString(localTimezone, options);
40
+ exports.formatDateTime = formatDateTime;
41
+ /**
42
+ *
43
+ * @function
44
+ * @param {string | Date} date - The date string that needs to be returned as a Date object.
45
+ * @returns {Date} - A date representing the new date object.
46
+ */
47
+ function parseDate(date) {
48
+ const outputDate = typeof date === "string" ? new Date(date) : date;
49
+ if (isNaN(outputDate.getTime())) {
50
+ throw new Error(`Cannot parse passed date: ${JSON.stringify(date)}`);
51
+ }
52
+ return outputDate;
53
+ }
54
+ exports.parseDate = parseDate;
55
+ /**
56
+ *
57
+ * @function
58
+ * @param {string | Date} date - The date string that needs to be formatted
59
+ * @returns {string} - A string representing the date in the short ordinal date
60
+ */
61
+ function getShortOrdinalDate(date) {
62
+ var _a;
63
+ const newDate = parseDate(date);
64
+ const day = newDate.getDate();
65
+ const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
66
+ const monthIndex = newDate.getMonth();
67
+ const year = newDate.getFullYear();
68
+ const hours = newDate.getHours().toString().padStart(2, "0");
69
+ const minutes = newDate.getMinutes().toString().padStart(2, "0");
70
+ const seconds = newDate.getSeconds().toString().padStart(2, "0");
71
+ const ordinalEndings = ["th", "st", "nd", "rd"];
72
+ let suffix = ordinalEndings[0];
73
+ if (day <= 3 && day > 0) {
74
+ suffix = ordinalEndings[day];
75
+ }
76
+ else {
77
+ // Determine the ordinal endings based on the last digit of the day
78
+ suffix = (_a = ordinalEndings[day % 10]) !== null && _a !== void 0 ? _a : suffix;
79
+ }
80
+ return `${day}${suffix}, ${monthNames[monthIndex]} ${year} ${hours}:${minutes}:${seconds}`;
81
+ }
82
+ exports.getShortOrdinalDate = getShortOrdinalDate;
package/dist/file.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Check if a file path has a valid extension
3
+ * @param filePath The file path to check
4
+ * @param validExtensions The valid extensions
5
+ * @returns true if the file path has a valid extension
6
+ * @example
7
+ * checkFileExtension("test.geojson", ["geojson", "json"]) // true
8
+ * checkFileExtension("test.txt", ["md"]) // false
9
+ */
10
+ export declare const checkFileExtension: (filePath: string, validExtensions: string[]) => boolean;
package/dist/file.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkFileExtension = void 0;
4
+ /**
5
+ * Check if a file path has a valid extension
6
+ * @param filePath The file path to check
7
+ * @param validExtensions The valid extensions
8
+ * @returns true if the file path has a valid extension
9
+ * @example
10
+ * checkFileExtension("test.geojson", ["geojson", "json"]) // true
11
+ * checkFileExtension("test.txt", ["md"]) // false
12
+ */
13
+ const checkFileExtension = (filePath, validExtensions) => {
14
+ const fileExtension = filePath.split(".").pop();
15
+ return !!fileExtension && validExtensions.includes(fileExtension);
16
+ };
17
+ exports.checkFileExtension = checkFileExtension;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * A "modern" sleep statement.
3
+ * @param ms The number of milliseconds to wait.
4
+ */
5
+ export declare const delay: (ms: number) => Promise<unknown>;
6
+ /**
7
+ * Get the preferred scheme for a host
8
+ * @param host An IP address or a domain name
9
+ * @return 'http' or 'https'
10
+ */
11
+ export declare const getPreferredUriScheme: (host: string) => "http" | "https";
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPreferredUriScheme = exports.delay = void 0;
4
+ /**
5
+ * A "modern" sleep statement.
6
+ * @param ms The number of milliseconds to wait.
7
+ */
8
+ const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
9
+ exports.delay = delay;
10
+ /**
11
+ * Get the preferred scheme for a host
12
+ * @param host An IP address or a domain name
13
+ * @return 'http' or 'https'
14
+ */
15
+ const getPreferredUriScheme = (host) => {
16
+ if (/^(127\.0\.0\.1|10\.\d{1,3}\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3}|172\.(1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}|localhost)(:\d+)?$/.test(host)) {
17
+ return "http";
18
+ }
19
+ return "https";
20
+ };
21
+ exports.getPreferredUriScheme = getPreferredUriScheme;