@trackunit/shared-utils 0.0.78 → 0.0.79

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/index.cjs.js CHANGED
@@ -1,205 +1,6 @@
1
1
  'use strict';
2
2
 
3
- exports.HoursAndMinutesFormat = void 0;
4
- (function (HoursAndMinutesFormat) {
5
- HoursAndMinutesFormat["HOURS_MIN_SEC_LONG"] = "HOURS_MIN_SEC_LONG";
6
- HoursAndMinutesFormat["HOURS_MIN_SEC"] = "HOURS_MIN_SEC";
7
- HoursAndMinutesFormat["HOURS_MIN_LONG"] = "HOURS_MIN_LONG";
8
- HoursAndMinutesFormat["HOURS_MIN"] = "HOURS_MIN";
9
- HoursAndMinutesFormat["HOURS_LONG"] = "HOURS_LONG";
10
- })(exports.HoursAndMinutesFormat || (exports.HoursAndMinutesFormat = {}));
11
- const DateTimeFormat = {
12
- DATE: {
13
- selectFormat: "dateOnly",
14
- dateFormat: "short",
15
- },
16
- DATE_LONG: {
17
- selectFormat: "dateOnly",
18
- dateFormat: "long",
19
- },
20
- DATE_LONG_TIME: {
21
- dateFormat: "long",
22
- timeFormat: "short",
23
- },
24
- DATE_SHORT: {
25
- selectFormat: "dateOnly",
26
- dateFormat: "medium",
27
- },
28
- DATE_SHORT_TIME: {
29
- dateFormat: "medium",
30
- timeFormat: "short",
31
- },
32
- TIME_SHORT: {
33
- selectFormat: "timeOnly",
34
- timeFormat: "short",
35
- },
36
- TIME_LONG: {
37
- selectFormat: "timeOnly",
38
- timeFormat: "medium",
39
- },
40
- };
41
-
42
- /**
43
- * @description Converts a date to a string in ISO format.
44
- * @param date A date object or a date string.
45
- * @returns {string | null} A string in ISO format or null if the input is invalid.
46
- * @example getISOStringFromDate(new Date("2021-05-19T12:34:56")) // "2021-05-19T12:34:56.000Z"
47
- */
48
- const getISOStringFromDate = (date) => {
49
- try {
50
- if (!date) {
51
- return null;
52
- }
53
- return new Date(date).toISOString();
54
- }
55
- catch (err) {
56
- return null;
57
- }
58
- };
59
- /**
60
- * @description Returns the difference between two dates in the specified unit.
61
- * @param date1 The first date.
62
- * @param date2 The second date.
63
- * @param unit The unit to return the difference in.
64
- * @returns {number} The difference between the two dates in the specified unit.
65
- */
66
- const getDifferenceBetweenDates = (date1, date2, unit) => {
67
- const diffInMs = Math.abs(date2.getTime() - date1.getTime());
68
- switch (unit) {
69
- case "days":
70
- return Math.floor(diffInMs / (1000 * 60 * 60 * 24));
71
- case "hours":
72
- return Math.floor(diffInMs / (1000 * 60 * 60));
73
- case "minutes":
74
- return Math.floor(diffInMs / (1000 * 60));
75
- case "seconds":
76
- return Math.floor(diffInMs / 1000);
77
- default:
78
- throw new Error("Invalid unit parameter");
79
- }
80
- };
81
- /**
82
- * @description Returns the start of the day for a given date.
83
- * @param date A date object or a date string.
84
- * @returns {Date} The start of the day for the given date.
85
- * @example getStartOfDay(new Date("2021-05-19T12:34:56")) // "2021-05-19T00:00:00"
86
- */
87
- const getStartOfDay = (date) => {
88
- const selectedDate = new Date(date);
89
- selectedDate.setHours(0, 0, 0, 0);
90
- return selectedDate;
91
- };
92
- /**
93
- * @description Returns the end of the day for a given date.
94
- * @param date A date object or a date string.
95
- * @returns {Date} The end of the day for the given date.
96
- * @example getEndOfDay(new Date("2021-05-19T12:34:56")) // "2021-05-19T23:59:59"
97
- */
98
- const getEndOfDay = (date) => {
99
- const selectedDate = new Date(date);
100
- selectedDate.setHours(23, 59, 59, 999);
101
- return selectedDate;
102
- };
103
-
104
- /**
105
- * @description Extracts a point coordinate from a GeoJSON object.
106
- * @param geoObject A GeoJSON object.
107
- * @returns {PointCoordinate} A point coordinate.
108
- */
109
- const getPointCoordinateFromGeoJsonObject = (geoObject) => {
110
- if (!geoObject) {
111
- return undefined;
112
- }
113
- else if ("geometry" in geoObject) {
114
- return getPointCoordinateFromGeoJsonObject(geoObject.geometry);
115
- }
116
- else if ("coordinates" in geoObject &&
117
- Array.isArray(geoObject.coordinates) &&
118
- typeof geoObject.coordinates[0] === "number" &&
119
- typeof geoObject.coordinates[1] === "number") {
120
- return { longitude: geoObject.coordinates[0], latitude: geoObject.coordinates[1] };
121
- }
122
- else {
123
- throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
124
- }
125
- };
126
- /**
127
- * @description Extracts multiple point coordinates from a GeoJSON object.
128
- * @param geoObject A GeoJSON object.
129
- * @returns {PointCoordinate[]} An array of point coordinates.
130
- * @example getMultipleCoordinatesFromGeoJsonObject({ type: "Point", coordinates: [1, 2] }) // [{ longitude: 1, latitude: 2 }]
131
- */
132
- const getMultipleCoordinatesFromGeoJsonObject = (geoObject) => {
133
- if (!geoObject) {
134
- return undefined;
135
- }
136
- else if ("geometry" in geoObject) {
137
- return getMultipleCoordinatesFromGeoJsonObject(geoObject.geometry);
138
- }
139
- else if ("coordinates" in geoObject &&
140
- Array.isArray(geoObject.coordinates) &&
141
- Array.isArray(geoObject.coordinates[0])) {
142
- // @ts-ignore - suppressImplicitAnyIndexErrors
143
- // We would not be here if element was not an array.
144
- return geoObject.coordinates.map(element => ({ longitude: element[0], latitude: element[1] }));
145
- }
146
- else {
147
- throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
148
- }
149
- };
150
-
151
- /**
152
- * Group data that has smaller count then 0.5% (smallestCount) of total count if there is more than 3% (limitToStartGrouping) of them to "Others" group.
153
- * It can help charts to look better.
154
- *
155
- * @param data Input data
156
- * @param smallestCount Breaking point to start considering the item as candidate to "Others" group [in percentage] - default 0,5%
157
- * @param limitToStartGrouping Lowest limit of candidates for grouping to "Others" [in percentage] - default 3%
158
- */
159
- function groupTinyDataToOthers(data, smallestCount = 0.5, limitToStartGrouping = 3) {
160
- if (!data || data.length < 2) {
161
- return data;
162
- }
163
- const breakingPoint = (data.reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count })).count * smallestCount) / 100;
164
- if (data.filter(x => x.count <= breakingPoint).length < 2) {
165
- return data;
166
- }
167
- const others = data
168
- .filter(x => x.count <= breakingPoint)
169
- .reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count }));
170
- const canGroupToOthers = others.count / data.reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count })).count >
171
- limitToStartGrouping / 100;
172
- if (!canGroupToOthers) {
173
- return data;
174
- }
175
- const temp = data.filter(x => x.count > breakingPoint);
176
- return [...temp, others].sort((a, b) => a.count - b.count);
177
- }
178
-
179
- /**
180
- * Converts meters to yards
181
- *
182
- * @param value The value to convert
183
- * @returns {number | undefined} The converted value
184
- */
185
- const convertMetersToYards = (value) => {
186
- if (isNaN(value)) {
187
- return;
188
- }
189
- return Math.round(value * 1.09361);
190
- };
191
- /**
192
- * Converts yards to meters
193
- *
194
- * @param value The value to convert
195
- * @returns {number | undefined} The converted value
196
- */
197
- const convertYardsToMeters = (value) => {
198
- if (isNaN(value)) {
199
- return;
200
- }
201
- return Math.round(value * 0.9144);
202
- };
3
+ var uuid = require('uuid');
203
4
 
204
5
  /**
205
6
  * @description Formats an address into a single string.
@@ -365,6 +166,107 @@ const size = {
365
166
  LARGE: "large",
366
167
  };
367
168
 
169
+ exports.HoursAndMinutesFormat = void 0;
170
+ (function (HoursAndMinutesFormat) {
171
+ HoursAndMinutesFormat["HOURS_MIN_SEC_LONG"] = "HOURS_MIN_SEC_LONG";
172
+ HoursAndMinutesFormat["HOURS_MIN_SEC"] = "HOURS_MIN_SEC";
173
+ HoursAndMinutesFormat["HOURS_MIN_LONG"] = "HOURS_MIN_LONG";
174
+ HoursAndMinutesFormat["HOURS_MIN"] = "HOURS_MIN";
175
+ HoursAndMinutesFormat["HOURS_LONG"] = "HOURS_LONG";
176
+ })(exports.HoursAndMinutesFormat || (exports.HoursAndMinutesFormat = {}));
177
+ const DateTimeFormat = {
178
+ DATE: {
179
+ selectFormat: "dateOnly",
180
+ dateFormat: "short",
181
+ },
182
+ DATE_LONG: {
183
+ selectFormat: "dateOnly",
184
+ dateFormat: "long",
185
+ },
186
+ DATE_LONG_TIME: {
187
+ dateFormat: "long",
188
+ timeFormat: "short",
189
+ },
190
+ DATE_SHORT: {
191
+ selectFormat: "dateOnly",
192
+ dateFormat: "medium",
193
+ },
194
+ DATE_SHORT_TIME: {
195
+ dateFormat: "medium",
196
+ timeFormat: "short",
197
+ },
198
+ TIME_SHORT: {
199
+ selectFormat: "timeOnly",
200
+ timeFormat: "short",
201
+ },
202
+ TIME_LONG: {
203
+ selectFormat: "timeOnly",
204
+ timeFormat: "medium",
205
+ },
206
+ };
207
+
208
+ /**
209
+ * @description Converts a date to a string in ISO format.
210
+ * @param date A date object or a date string.
211
+ * @returns {string | null} A string in ISO format or null if the input is invalid.
212
+ * @example getISOStringFromDate(new Date("2021-05-19T12:34:56")) // "2021-05-19T12:34:56.000Z"
213
+ */
214
+ const getISOStringFromDate = (date) => {
215
+ try {
216
+ if (!date) {
217
+ return null;
218
+ }
219
+ return new Date(date).toISOString();
220
+ }
221
+ catch (err) {
222
+ return null;
223
+ }
224
+ };
225
+ /**
226
+ * @description Returns the difference between two dates in the specified unit.
227
+ * @param date1 The first date.
228
+ * @param date2 The second date.
229
+ * @param unit The unit to return the difference in.
230
+ * @returns {number} The difference between the two dates in the specified unit.
231
+ */
232
+ const getDifferenceBetweenDates = (date1, date2, unit) => {
233
+ const diffInMs = Math.abs(date2.getTime() - date1.getTime());
234
+ switch (unit) {
235
+ case "days":
236
+ return Math.floor(diffInMs / (1000 * 60 * 60 * 24));
237
+ case "hours":
238
+ return Math.floor(diffInMs / (1000 * 60 * 60));
239
+ case "minutes":
240
+ return Math.floor(diffInMs / (1000 * 60));
241
+ case "seconds":
242
+ return Math.floor(diffInMs / 1000);
243
+ default:
244
+ throw new Error("Invalid unit parameter");
245
+ }
246
+ };
247
+ /**
248
+ * @description Returns the start of the day for a given date.
249
+ * @param date A date object or a date string.
250
+ * @returns {Date} The start of the day for the given date.
251
+ * @example getStartOfDay(new Date("2021-05-19T12:34:56")) // "2021-05-19T00:00:00"
252
+ */
253
+ const getStartOfDay = (date) => {
254
+ const selectedDate = new Date(date);
255
+ selectedDate.setHours(0, 0, 0, 0);
256
+ return selectedDate;
257
+ };
258
+ /**
259
+ * @description Returns the end of the day for a given date.
260
+ * @param date A date object or a date string.
261
+ * @returns {Date} The end of the day for the given date.
262
+ * @example getEndOfDay(new Date("2021-05-19T12:34:56")) // "2021-05-19T23:59:59"
263
+ */
264
+ const getEndOfDay = (date) => {
265
+ const selectedDate = new Date(date);
266
+ selectedDate.setHours(23, 59, 59, 999);
267
+ return selectedDate;
268
+ };
269
+
368
270
  /**
369
271
  * Do nothing usefull for creating mocks without dependency on jest or anything else.
370
272
  */
@@ -511,6 +413,53 @@ searchTerm) => {
511
413
  .every(term => filterableProps(asset).some(value => value.toLowerCase().includes(term.toLowerCase()))));
512
414
  };
513
415
 
416
+ /**
417
+ * @description Extracts a point coordinate from a GeoJSON object.
418
+ * @param geoObject A GeoJSON object.
419
+ * @returns {PointCoordinate} A point coordinate.
420
+ */
421
+ const getPointCoordinateFromGeoJsonObject = (geoObject) => {
422
+ if (!geoObject) {
423
+ return undefined;
424
+ }
425
+ else if ("geometry" in geoObject) {
426
+ return getPointCoordinateFromGeoJsonObject(geoObject.geometry);
427
+ }
428
+ else if ("coordinates" in geoObject &&
429
+ Array.isArray(geoObject.coordinates) &&
430
+ typeof geoObject.coordinates[0] === "number" &&
431
+ typeof geoObject.coordinates[1] === "number") {
432
+ return { longitude: geoObject.coordinates[0], latitude: geoObject.coordinates[1] };
433
+ }
434
+ else {
435
+ throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
436
+ }
437
+ };
438
+ /**
439
+ * @description Extracts multiple point coordinates from a GeoJSON object.
440
+ * @param geoObject A GeoJSON object.
441
+ * @returns {PointCoordinate[]} An array of point coordinates.
442
+ * @example getMultipleCoordinatesFromGeoJsonObject({ type: "Point", coordinates: [1, 2] }) // [{ longitude: 1, latitude: 2 }]
443
+ */
444
+ const getMultipleCoordinatesFromGeoJsonObject = (geoObject) => {
445
+ if (!geoObject) {
446
+ return undefined;
447
+ }
448
+ else if ("geometry" in geoObject) {
449
+ return getMultipleCoordinatesFromGeoJsonObject(geoObject.geometry);
450
+ }
451
+ else if ("coordinates" in geoObject &&
452
+ Array.isArray(geoObject.coordinates) &&
453
+ Array.isArray(geoObject.coordinates[0])) {
454
+ // @ts-ignore - suppressImplicitAnyIndexErrors
455
+ // We would not be here if element was not an array.
456
+ return geoObject.coordinates.map(element => ({ longitude: element[0], latitude: element[1] }));
457
+ }
458
+ else {
459
+ throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
460
+ }
461
+ };
462
+
514
463
  /**
515
464
  * Group an array of items by a key.
516
465
  *
@@ -538,6 +487,34 @@ function groupBy(list, getKey) {
538
487
  return groupedMap;
539
488
  }
540
489
 
490
+ /**
491
+ * Group data that has smaller count then 0.5% (smallestCount) of total count if there is more than 3% (limitToStartGrouping) of them to "Others" group.
492
+ * It can help charts to look better.
493
+ *
494
+ * @param data Input data
495
+ * @param smallestCount Breaking point to start considering the item as candidate to "Others" group [in percentage] - default 0,5%
496
+ * @param limitToStartGrouping Lowest limit of candidates for grouping to "Others" [in percentage] - default 3%
497
+ */
498
+ function groupTinyDataToOthers(data, smallestCount = 0.5, limitToStartGrouping = 3) {
499
+ if (!data || data.length < 2) {
500
+ return data;
501
+ }
502
+ const breakingPoint = (data.reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count })).count * smallestCount) / 100;
503
+ if (data.filter(x => x.count <= breakingPoint).length < 2) {
504
+ return data;
505
+ }
506
+ const others = data
507
+ .filter(x => x.count <= breakingPoint)
508
+ .reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count }));
509
+ const canGroupToOthers = others.count / data.reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count })).count >
510
+ limitToStartGrouping / 100;
511
+ if (!canGroupToOthers) {
512
+ return data;
513
+ }
514
+ const temp = data.filter(x => x.count > breakingPoint);
515
+ return [...temp, others].sort((a, b) => a.count - b.count);
516
+ }
517
+
541
518
  const UUID_HYPHEN_POSITIONS = [8, 12, 16, 20];
542
519
  const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
543
520
  /**
@@ -1074,6 +1051,61 @@ const isSorted = (sortInput) => {
1074
1051
  return originalKeys.every((key, index) => key === sortedKeys[index]);
1075
1052
  };
1076
1053
 
1054
+ /**
1055
+ * Converts meters to yards
1056
+ *
1057
+ * @param value The value to convert
1058
+ * @returns {number | undefined} The converted value
1059
+ */
1060
+ const convertMetersToYards = (value) => {
1061
+ if (isNaN(value)) {
1062
+ return;
1063
+ }
1064
+ return Math.round(value * 1.09361);
1065
+ };
1066
+ /**
1067
+ * Converts yards to meters
1068
+ *
1069
+ * @param value The value to convert
1070
+ * @returns {number | undefined} The converted value
1071
+ */
1072
+ const convertYardsToMeters = (value) => {
1073
+ if (isNaN(value)) {
1074
+ return;
1075
+ }
1076
+ return Math.round(value * 0.9144);
1077
+ };
1078
+
1079
+ /**
1080
+ * Generates a version 3 UUID (namespace with MD5).
1081
+ *
1082
+ * @param {string} name - The name for which to generate the UUID.
1083
+ * @param {string | Buffer} namespace - The namespace for the UUID.
1084
+ * @returns {string} A version 3 UUID.
1085
+ */
1086
+ const uuidv3 = (name, namespace) => uuid.v3(name, namespace);
1087
+ /**
1088
+ * Generates a random version 4 UUID.
1089
+ *
1090
+ * @returns {string} A version 4 UUID.
1091
+ */
1092
+ const uuidv4 = () => {
1093
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
1094
+ return crypto.randomUUID().toString();
1095
+ }
1096
+ else {
1097
+ return uuid.v4();
1098
+ }
1099
+ };
1100
+ /**
1101
+ * Generates a version 5 UUID (namespace with SHA-1).
1102
+ *
1103
+ * @param {string} name - The name for which to generate the UUID.
1104
+ * @param {string | Buffer} namespace - The namespace for the UUID.
1105
+ * @returns {string} A version 5 UUID.
1106
+ */
1107
+ const uuidv5 = (name, namespace) => uuid.v5(name, namespace);
1108
+
1077
1109
  exports.DateTimeFormat = DateTimeFormat;
1078
1110
  exports.align = align;
1079
1111
  exports.alphabeticallySort = alphabeticallySort;
@@ -1137,3 +1169,6 @@ exports.trimIds = trimIds;
1137
1169
  exports.trimPath = trimPath;
1138
1170
  exports.truthy = truthy;
1139
1171
  exports.unionArraysByKey = unionArraysByKey;
1172
+ exports.uuidv3 = uuidv3;
1173
+ exports.uuidv4 = uuidv4;
1174
+ exports.uuidv5 = uuidv5;
package/index.esm.js CHANGED
@@ -1,203 +1,4 @@
1
- var HoursAndMinutesFormat;
2
- (function (HoursAndMinutesFormat) {
3
- HoursAndMinutesFormat["HOURS_MIN_SEC_LONG"] = "HOURS_MIN_SEC_LONG";
4
- HoursAndMinutesFormat["HOURS_MIN_SEC"] = "HOURS_MIN_SEC";
5
- HoursAndMinutesFormat["HOURS_MIN_LONG"] = "HOURS_MIN_LONG";
6
- HoursAndMinutesFormat["HOURS_MIN"] = "HOURS_MIN";
7
- HoursAndMinutesFormat["HOURS_LONG"] = "HOURS_LONG";
8
- })(HoursAndMinutesFormat || (HoursAndMinutesFormat = {}));
9
- const DateTimeFormat = {
10
- DATE: {
11
- selectFormat: "dateOnly",
12
- dateFormat: "short",
13
- },
14
- DATE_LONG: {
15
- selectFormat: "dateOnly",
16
- dateFormat: "long",
17
- },
18
- DATE_LONG_TIME: {
19
- dateFormat: "long",
20
- timeFormat: "short",
21
- },
22
- DATE_SHORT: {
23
- selectFormat: "dateOnly",
24
- dateFormat: "medium",
25
- },
26
- DATE_SHORT_TIME: {
27
- dateFormat: "medium",
28
- timeFormat: "short",
29
- },
30
- TIME_SHORT: {
31
- selectFormat: "timeOnly",
32
- timeFormat: "short",
33
- },
34
- TIME_LONG: {
35
- selectFormat: "timeOnly",
36
- timeFormat: "medium",
37
- },
38
- };
39
-
40
- /**
41
- * @description Converts a date to a string in ISO format.
42
- * @param date A date object or a date string.
43
- * @returns {string | null} A string in ISO format or null if the input is invalid.
44
- * @example getISOStringFromDate(new Date("2021-05-19T12:34:56")) // "2021-05-19T12:34:56.000Z"
45
- */
46
- const getISOStringFromDate = (date) => {
47
- try {
48
- if (!date) {
49
- return null;
50
- }
51
- return new Date(date).toISOString();
52
- }
53
- catch (err) {
54
- return null;
55
- }
56
- };
57
- /**
58
- * @description Returns the difference between two dates in the specified unit.
59
- * @param date1 The first date.
60
- * @param date2 The second date.
61
- * @param unit The unit to return the difference in.
62
- * @returns {number} The difference between the two dates in the specified unit.
63
- */
64
- const getDifferenceBetweenDates = (date1, date2, unit) => {
65
- const diffInMs = Math.abs(date2.getTime() - date1.getTime());
66
- switch (unit) {
67
- case "days":
68
- return Math.floor(diffInMs / (1000 * 60 * 60 * 24));
69
- case "hours":
70
- return Math.floor(diffInMs / (1000 * 60 * 60));
71
- case "minutes":
72
- return Math.floor(diffInMs / (1000 * 60));
73
- case "seconds":
74
- return Math.floor(diffInMs / 1000);
75
- default:
76
- throw new Error("Invalid unit parameter");
77
- }
78
- };
79
- /**
80
- * @description Returns the start of the day for a given date.
81
- * @param date A date object or a date string.
82
- * @returns {Date} The start of the day for the given date.
83
- * @example getStartOfDay(new Date("2021-05-19T12:34:56")) // "2021-05-19T00:00:00"
84
- */
85
- const getStartOfDay = (date) => {
86
- const selectedDate = new Date(date);
87
- selectedDate.setHours(0, 0, 0, 0);
88
- return selectedDate;
89
- };
90
- /**
91
- * @description Returns the end of the day for a given date.
92
- * @param date A date object or a date string.
93
- * @returns {Date} The end of the day for the given date.
94
- * @example getEndOfDay(new Date("2021-05-19T12:34:56")) // "2021-05-19T23:59:59"
95
- */
96
- const getEndOfDay = (date) => {
97
- const selectedDate = new Date(date);
98
- selectedDate.setHours(23, 59, 59, 999);
99
- return selectedDate;
100
- };
101
-
102
- /**
103
- * @description Extracts a point coordinate from a GeoJSON object.
104
- * @param geoObject A GeoJSON object.
105
- * @returns {PointCoordinate} A point coordinate.
106
- */
107
- const getPointCoordinateFromGeoJsonObject = (geoObject) => {
108
- if (!geoObject) {
109
- return undefined;
110
- }
111
- else if ("geometry" in geoObject) {
112
- return getPointCoordinateFromGeoJsonObject(geoObject.geometry);
113
- }
114
- else if ("coordinates" in geoObject &&
115
- Array.isArray(geoObject.coordinates) &&
116
- typeof geoObject.coordinates[0] === "number" &&
117
- typeof geoObject.coordinates[1] === "number") {
118
- return { longitude: geoObject.coordinates[0], latitude: geoObject.coordinates[1] };
119
- }
120
- else {
121
- throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
122
- }
123
- };
124
- /**
125
- * @description Extracts multiple point coordinates from a GeoJSON object.
126
- * @param geoObject A GeoJSON object.
127
- * @returns {PointCoordinate[]} An array of point coordinates.
128
- * @example getMultipleCoordinatesFromGeoJsonObject({ type: "Point", coordinates: [1, 2] }) // [{ longitude: 1, latitude: 2 }]
129
- */
130
- const getMultipleCoordinatesFromGeoJsonObject = (geoObject) => {
131
- if (!geoObject) {
132
- return undefined;
133
- }
134
- else if ("geometry" in geoObject) {
135
- return getMultipleCoordinatesFromGeoJsonObject(geoObject.geometry);
136
- }
137
- else if ("coordinates" in geoObject &&
138
- Array.isArray(geoObject.coordinates) &&
139
- Array.isArray(geoObject.coordinates[0])) {
140
- // @ts-ignore - suppressImplicitAnyIndexErrors
141
- // We would not be here if element was not an array.
142
- return geoObject.coordinates.map(element => ({ longitude: element[0], latitude: element[1] }));
143
- }
144
- else {
145
- throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
146
- }
147
- };
148
-
149
- /**
150
- * Group data that has smaller count then 0.5% (smallestCount) of total count if there is more than 3% (limitToStartGrouping) of them to "Others" group.
151
- * It can help charts to look better.
152
- *
153
- * @param data Input data
154
- * @param smallestCount Breaking point to start considering the item as candidate to "Others" group [in percentage] - default 0,5%
155
- * @param limitToStartGrouping Lowest limit of candidates for grouping to "Others" [in percentage] - default 3%
156
- */
157
- function groupTinyDataToOthers(data, smallestCount = 0.5, limitToStartGrouping = 3) {
158
- if (!data || data.length < 2) {
159
- return data;
160
- }
161
- const breakingPoint = (data.reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count })).count * smallestCount) / 100;
162
- if (data.filter(x => x.count <= breakingPoint).length < 2) {
163
- return data;
164
- }
165
- const others = data
166
- .filter(x => x.count <= breakingPoint)
167
- .reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count }));
168
- const canGroupToOthers = others.count / data.reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count })).count >
169
- limitToStartGrouping / 100;
170
- if (!canGroupToOthers) {
171
- return data;
172
- }
173
- const temp = data.filter(x => x.count > breakingPoint);
174
- return [...temp, others].sort((a, b) => a.count - b.count);
175
- }
176
-
177
- /**
178
- * Converts meters to yards
179
- *
180
- * @param value The value to convert
181
- * @returns {number | undefined} The converted value
182
- */
183
- const convertMetersToYards = (value) => {
184
- if (isNaN(value)) {
185
- return;
186
- }
187
- return Math.round(value * 1.09361);
188
- };
189
- /**
190
- * Converts yards to meters
191
- *
192
- * @param value The value to convert
193
- * @returns {number | undefined} The converted value
194
- */
195
- const convertYardsToMeters = (value) => {
196
- if (isNaN(value)) {
197
- return;
198
- }
199
- return Math.round(value * 0.9144);
200
- };
1
+ import { v3, v4, v5 } from 'uuid';
201
2
 
202
3
  /**
203
4
  * @description Formats an address into a single string.
@@ -363,6 +164,107 @@ const size = {
363
164
  LARGE: "large",
364
165
  };
365
166
 
167
+ var HoursAndMinutesFormat;
168
+ (function (HoursAndMinutesFormat) {
169
+ HoursAndMinutesFormat["HOURS_MIN_SEC_LONG"] = "HOURS_MIN_SEC_LONG";
170
+ HoursAndMinutesFormat["HOURS_MIN_SEC"] = "HOURS_MIN_SEC";
171
+ HoursAndMinutesFormat["HOURS_MIN_LONG"] = "HOURS_MIN_LONG";
172
+ HoursAndMinutesFormat["HOURS_MIN"] = "HOURS_MIN";
173
+ HoursAndMinutesFormat["HOURS_LONG"] = "HOURS_LONG";
174
+ })(HoursAndMinutesFormat || (HoursAndMinutesFormat = {}));
175
+ const DateTimeFormat = {
176
+ DATE: {
177
+ selectFormat: "dateOnly",
178
+ dateFormat: "short",
179
+ },
180
+ DATE_LONG: {
181
+ selectFormat: "dateOnly",
182
+ dateFormat: "long",
183
+ },
184
+ DATE_LONG_TIME: {
185
+ dateFormat: "long",
186
+ timeFormat: "short",
187
+ },
188
+ DATE_SHORT: {
189
+ selectFormat: "dateOnly",
190
+ dateFormat: "medium",
191
+ },
192
+ DATE_SHORT_TIME: {
193
+ dateFormat: "medium",
194
+ timeFormat: "short",
195
+ },
196
+ TIME_SHORT: {
197
+ selectFormat: "timeOnly",
198
+ timeFormat: "short",
199
+ },
200
+ TIME_LONG: {
201
+ selectFormat: "timeOnly",
202
+ timeFormat: "medium",
203
+ },
204
+ };
205
+
206
+ /**
207
+ * @description Converts a date to a string in ISO format.
208
+ * @param date A date object or a date string.
209
+ * @returns {string | null} A string in ISO format or null if the input is invalid.
210
+ * @example getISOStringFromDate(new Date("2021-05-19T12:34:56")) // "2021-05-19T12:34:56.000Z"
211
+ */
212
+ const getISOStringFromDate = (date) => {
213
+ try {
214
+ if (!date) {
215
+ return null;
216
+ }
217
+ return new Date(date).toISOString();
218
+ }
219
+ catch (err) {
220
+ return null;
221
+ }
222
+ };
223
+ /**
224
+ * @description Returns the difference between two dates in the specified unit.
225
+ * @param date1 The first date.
226
+ * @param date2 The second date.
227
+ * @param unit The unit to return the difference in.
228
+ * @returns {number} The difference between the two dates in the specified unit.
229
+ */
230
+ const getDifferenceBetweenDates = (date1, date2, unit) => {
231
+ const diffInMs = Math.abs(date2.getTime() - date1.getTime());
232
+ switch (unit) {
233
+ case "days":
234
+ return Math.floor(diffInMs / (1000 * 60 * 60 * 24));
235
+ case "hours":
236
+ return Math.floor(diffInMs / (1000 * 60 * 60));
237
+ case "minutes":
238
+ return Math.floor(diffInMs / (1000 * 60));
239
+ case "seconds":
240
+ return Math.floor(diffInMs / 1000);
241
+ default:
242
+ throw new Error("Invalid unit parameter");
243
+ }
244
+ };
245
+ /**
246
+ * @description Returns the start of the day for a given date.
247
+ * @param date A date object or a date string.
248
+ * @returns {Date} The start of the day for the given date.
249
+ * @example getStartOfDay(new Date("2021-05-19T12:34:56")) // "2021-05-19T00:00:00"
250
+ */
251
+ const getStartOfDay = (date) => {
252
+ const selectedDate = new Date(date);
253
+ selectedDate.setHours(0, 0, 0, 0);
254
+ return selectedDate;
255
+ };
256
+ /**
257
+ * @description Returns the end of the day for a given date.
258
+ * @param date A date object or a date string.
259
+ * @returns {Date} The end of the day for the given date.
260
+ * @example getEndOfDay(new Date("2021-05-19T12:34:56")) // "2021-05-19T23:59:59"
261
+ */
262
+ const getEndOfDay = (date) => {
263
+ const selectedDate = new Date(date);
264
+ selectedDate.setHours(23, 59, 59, 999);
265
+ return selectedDate;
266
+ };
267
+
366
268
  /**
367
269
  * Do nothing usefull for creating mocks without dependency on jest or anything else.
368
270
  */
@@ -509,6 +411,53 @@ searchTerm) => {
509
411
  .every(term => filterableProps(asset).some(value => value.toLowerCase().includes(term.toLowerCase()))));
510
412
  };
511
413
 
414
+ /**
415
+ * @description Extracts a point coordinate from a GeoJSON object.
416
+ * @param geoObject A GeoJSON object.
417
+ * @returns {PointCoordinate} A point coordinate.
418
+ */
419
+ const getPointCoordinateFromGeoJsonObject = (geoObject) => {
420
+ if (!geoObject) {
421
+ return undefined;
422
+ }
423
+ else if ("geometry" in geoObject) {
424
+ return getPointCoordinateFromGeoJsonObject(geoObject.geometry);
425
+ }
426
+ else if ("coordinates" in geoObject &&
427
+ Array.isArray(geoObject.coordinates) &&
428
+ typeof geoObject.coordinates[0] === "number" &&
429
+ typeof geoObject.coordinates[1] === "number") {
430
+ return { longitude: geoObject.coordinates[0], latitude: geoObject.coordinates[1] };
431
+ }
432
+ else {
433
+ throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
434
+ }
435
+ };
436
+ /**
437
+ * @description Extracts multiple point coordinates from a GeoJSON object.
438
+ * @param geoObject A GeoJSON object.
439
+ * @returns {PointCoordinate[]} An array of point coordinates.
440
+ * @example getMultipleCoordinatesFromGeoJsonObject({ type: "Point", coordinates: [1, 2] }) // [{ longitude: 1, latitude: 2 }]
441
+ */
442
+ const getMultipleCoordinatesFromGeoJsonObject = (geoObject) => {
443
+ if (!geoObject) {
444
+ return undefined;
445
+ }
446
+ else if ("geometry" in geoObject) {
447
+ return getMultipleCoordinatesFromGeoJsonObject(geoObject.geometry);
448
+ }
449
+ else if ("coordinates" in geoObject &&
450
+ Array.isArray(geoObject.coordinates) &&
451
+ Array.isArray(geoObject.coordinates[0])) {
452
+ // @ts-ignore - suppressImplicitAnyIndexErrors
453
+ // We would not be here if element was not an array.
454
+ return geoObject.coordinates.map(element => ({ longitude: element[0], latitude: element[1] }));
455
+ }
456
+ else {
457
+ throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
458
+ }
459
+ };
460
+
512
461
  /**
513
462
  * Group an array of items by a key.
514
463
  *
@@ -536,6 +485,34 @@ function groupBy(list, getKey) {
536
485
  return groupedMap;
537
486
  }
538
487
 
488
+ /**
489
+ * Group data that has smaller count then 0.5% (smallestCount) of total count if there is more than 3% (limitToStartGrouping) of them to "Others" group.
490
+ * It can help charts to look better.
491
+ *
492
+ * @param data Input data
493
+ * @param smallestCount Breaking point to start considering the item as candidate to "Others" group [in percentage] - default 0,5%
494
+ * @param limitToStartGrouping Lowest limit of candidates for grouping to "Others" [in percentage] - default 3%
495
+ */
496
+ function groupTinyDataToOthers(data, smallestCount = 0.5, limitToStartGrouping = 3) {
497
+ if (!data || data.length < 2) {
498
+ return data;
499
+ }
500
+ const breakingPoint = (data.reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count })).count * smallestCount) / 100;
501
+ if (data.filter(x => x.count <= breakingPoint).length < 2) {
502
+ return data;
503
+ }
504
+ const others = data
505
+ .filter(x => x.count <= breakingPoint)
506
+ .reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count }));
507
+ const canGroupToOthers = others.count / data.reduce((x, y) => ({ key: "GroupedOthers", count: x.count + y.count })).count >
508
+ limitToStartGrouping / 100;
509
+ if (!canGroupToOthers) {
510
+ return data;
511
+ }
512
+ const temp = data.filter(x => x.count > breakingPoint);
513
+ return [...temp, others].sort((a, b) => a.count - b.count);
514
+ }
515
+
539
516
  const UUID_HYPHEN_POSITIONS = [8, 12, 16, 20];
540
517
  const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
541
518
  /**
@@ -1072,4 +1049,59 @@ const isSorted = (sortInput) => {
1072
1049
  return originalKeys.every((key, index) => key === sortedKeys[index]);
1073
1050
  };
1074
1051
 
1075
- export { DateTimeFormat, HoursAndMinutesFormat, align, alphabeticallySort, arrayLengthCompare, arrayNotEmpty, booleanCompare, capitalize, convertBlobToBase64, convertMetersToYards, convertYardsToMeters, dateCompare, deleteUndefinedKeys, difference, doNothing, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, filterByMultiple, formatAddress, formatCoordinates, fuzzySearch, getDifferenceBetweenDates, getEndOfDay, getFirstLevelObjectPropertyDifferences, getISOStringFromDate, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getResizedDimensions, getStartOfDay, groupBy, groupTinyDataToOthers, hourIntervals, intersection, isArrayEqual, isSorted, isUUID, isValidImage, nonNullable, numberCompare, numberCompareUnknownAfterHighest, objNotEmpty, objectEntries, objectFromEntries, objectKeys, objectValues, pick, removeLeftPadding, resizeBlob, resizeImage, size, stringCompare, stringCompareFromKey, stringNaturalCompare, stripHiddenCharacters, titleCase, toID, toIDs, toUUID, trimIds, trimPath, truthy, unionArraysByKey };
1052
+ /**
1053
+ * Converts meters to yards
1054
+ *
1055
+ * @param value The value to convert
1056
+ * @returns {number | undefined} The converted value
1057
+ */
1058
+ const convertMetersToYards = (value) => {
1059
+ if (isNaN(value)) {
1060
+ return;
1061
+ }
1062
+ return Math.round(value * 1.09361);
1063
+ };
1064
+ /**
1065
+ * Converts yards to meters
1066
+ *
1067
+ * @param value The value to convert
1068
+ * @returns {number | undefined} The converted value
1069
+ */
1070
+ const convertYardsToMeters = (value) => {
1071
+ if (isNaN(value)) {
1072
+ return;
1073
+ }
1074
+ return Math.round(value * 0.9144);
1075
+ };
1076
+
1077
+ /**
1078
+ * Generates a version 3 UUID (namespace with MD5).
1079
+ *
1080
+ * @param {string} name - The name for which to generate the UUID.
1081
+ * @param {string | Buffer} namespace - The namespace for the UUID.
1082
+ * @returns {string} A version 3 UUID.
1083
+ */
1084
+ const uuidv3 = (name, namespace) => v3(name, namespace);
1085
+ /**
1086
+ * Generates a random version 4 UUID.
1087
+ *
1088
+ * @returns {string} A version 4 UUID.
1089
+ */
1090
+ const uuidv4 = () => {
1091
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
1092
+ return crypto.randomUUID().toString();
1093
+ }
1094
+ else {
1095
+ return v4();
1096
+ }
1097
+ };
1098
+ /**
1099
+ * Generates a version 5 UUID (namespace with SHA-1).
1100
+ *
1101
+ * @param {string} name - The name for which to generate the UUID.
1102
+ * @param {string | Buffer} namespace - The namespace for the UUID.
1103
+ * @returns {string} A version 5 UUID.
1104
+ */
1105
+ const uuidv5 = (name, namespace) => v5(name, namespace);
1106
+
1107
+ export { DateTimeFormat, HoursAndMinutesFormat, align, alphabeticallySort, arrayLengthCompare, arrayNotEmpty, booleanCompare, capitalize, convertBlobToBase64, convertMetersToYards, convertYardsToMeters, dateCompare, deleteUndefinedKeys, difference, doNothing, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, filterByMultiple, formatAddress, formatCoordinates, fuzzySearch, getDifferenceBetweenDates, getEndOfDay, getFirstLevelObjectPropertyDifferences, getISOStringFromDate, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getResizedDimensions, getStartOfDay, groupBy, groupTinyDataToOthers, hourIntervals, intersection, isArrayEqual, isSorted, isUUID, isValidImage, nonNullable, numberCompare, numberCompareUnknownAfterHighest, objNotEmpty, objectEntries, objectFromEntries, objectKeys, objectValues, pick, removeLeftPadding, resizeBlob, resizeImage, size, stringCompare, stringCompareFromKey, stringNaturalCompare, stripHiddenCharacters, titleCase, toID, toIDs, toUUID, trimIds, trimPath, truthy, unionArraysByKey, uuidv3, uuidv4, uuidv5 };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@trackunit/shared-utils",
3
- "version": "0.0.78",
3
+ "version": "0.0.79",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
7
7
  "node": ">=20.x"
8
8
  },
9
9
  "dependencies": {
10
- "uuid": "^9.0.1"
10
+ "uuid": "^10.0.0"
11
11
  },
12
12
  "module": "./index.esm.js",
13
13
  "main": "./index.cjs.js",
package/src/UUID.d.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  */
8
8
  declare const uuidv3: (name: string, namespace: string | Buffer) => string;
9
9
  /**
10
- * Generates a version 4 UUID using the Node.js crypto module.
10
+ * Generates a random version 4 UUID.
11
11
  *
12
12
  * @returns {string} A version 4 UUID.
13
13
  */
package/src/index.d.ts CHANGED
@@ -1,18 +1,17 @@
1
- export * from "./DateTimeFormat";
2
- export * from "./DateUtils";
3
- export * from "./GeoJsonUtils";
4
- export * from "./GroupingUtility";
5
- export * from "./UnitOfMeasurementConverter";
6
1
  export * from "./addressUtils";
7
2
  export * from "./arrayUtils";
8
3
  export * from "./constants";
4
+ export * from "./DateTimeFormat";
5
+ export * from "./DateUtils";
9
6
  export * from "./deepPartial";
10
7
  export * from "./doNothing";
11
8
  export * from "./enumUtils";
12
9
  export * from "./exhaustiveCheck";
13
10
  export * from "./fastArrayOperations";
14
11
  export * from "./filter";
12
+ export * from "./GeoJsonUtils";
15
13
  export * from "./groupBy/groupBy";
14
+ export * from "./GroupingUtility";
16
15
  export * from "./idUtils";
17
16
  export * from "./maybe";
18
17
  export * from "./objectUtils";
@@ -22,3 +21,5 @@ export * from "./sorting/sorting";
22
21
  export * from "./stringUtils";
23
22
  export * from "./translationUtils";
24
23
  export * from "./typeUtils";
24
+ export * from "./UnitOfMeasurementConverter";
25
+ export * from "./UUID";