@trackunit/shared-utils 1.0.3 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs.js +47 -51
- package/index.esm.js +47 -51
- package/package.json +1 -1
- package/src/index.d.ts +1 -1
package/index.cjs.js
CHANGED
|
@@ -114,14 +114,14 @@ Object.values(object);
|
|
|
114
114
|
* @returns A new array with the items from the previous array and the new array, but only if the item's key is unique.
|
|
115
115
|
*/
|
|
116
116
|
const unionArraysByKey = (key, previous, newArray) => {
|
|
117
|
-
const previousIds =
|
|
118
|
-
const newIds =
|
|
117
|
+
const previousIds = previous?.map(edge => edge[key]) || [];
|
|
118
|
+
const newIds = newArray?.map(edge => edge[key]) || [];
|
|
119
119
|
const mergedIds = [...previousIds, ...newIds];
|
|
120
120
|
const uniqueIds = [...new Set(mergedIds)];
|
|
121
121
|
return uniqueIds
|
|
122
122
|
.map(id => {
|
|
123
|
-
const previousEdge = previous
|
|
124
|
-
const newEdge = newArray
|
|
123
|
+
const previousEdge = previous?.find(edge => edge[key] === id);
|
|
124
|
+
const newEdge = newArray?.find(edge => edge[key] === id);
|
|
125
125
|
return newEdge || previousEdge;
|
|
126
126
|
})
|
|
127
127
|
.filter(truthy);
|
|
@@ -529,7 +529,7 @@ const toID = (uuid) => Number(uuid.replace(/-/g, "").replace(/^0+/, ""));
|
|
|
529
529
|
*
|
|
530
530
|
* @param uuid The list of UUIDs that should be converted to a list of valid machine IDs
|
|
531
531
|
*/
|
|
532
|
-
const toIDs = (ids) =>
|
|
532
|
+
const toIDs = (ids) => ids?.filter(id => !isNaN(toID(id))).map(id => toID(id)) ?? [];
|
|
533
533
|
|
|
534
534
|
/**
|
|
535
535
|
* Converts an HTMLImageElement to a PNG image.
|
|
@@ -544,9 +544,8 @@ const toIDs = (ids) => { var _a; return (_a = ids === null || ids === void 0 ? v
|
|
|
544
544
|
* @throws Error if unable to get the canvas context.
|
|
545
545
|
*/
|
|
546
546
|
const toPNG = ({ image, dimensions, maxHeight, maxWidth, idealArea, }) => {
|
|
547
|
-
|
|
548
|
-
let
|
|
549
|
-
let height = (_b = dimensions === null || dimensions === void 0 ? void 0 : dimensions.height) !== null && _b !== void 0 ? _b : image.height;
|
|
547
|
+
let width = dimensions?.width ?? image.width;
|
|
548
|
+
let height = dimensions?.height ?? image.height;
|
|
550
549
|
const scale = calculateImageScaleRatio(width, height, maxWidth, maxHeight, idealArea);
|
|
551
550
|
width = Math.round(width * scale);
|
|
552
551
|
height = Math.round(height * scale);
|
|
@@ -751,11 +750,10 @@ const trimIds = (str) => {
|
|
|
751
750
|
};
|
|
752
751
|
|
|
753
752
|
const dataURItoBlob = (dataURI) => {
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
const mime = (_e = (_d = dataURI.split(",")[0]) === null || _d === void 0 ? void 0 : _d.split(":")[1]) === null || _e === void 0 ? void 0 : _e.split(";")[0];
|
|
753
|
+
const bytes = (dataURI.split(",")[0] ?? "").indexOf("base64") >= 0
|
|
754
|
+
? atob(dataURI.split(",")[1] ?? "")
|
|
755
|
+
: unescape(dataURI.split(",")[1] ?? "");
|
|
756
|
+
const mime = dataURI.split(",")[0]?.split(":")[1]?.split(";")[0];
|
|
759
757
|
const max = bytes.length;
|
|
760
758
|
const ia = new Uint8Array(max);
|
|
761
759
|
for (let i = 0; i < max; i++) {
|
|
@@ -1088,7 +1086,7 @@ const titleCase = (s) => {
|
|
|
1088
1086
|
* @example removeLeftPadding("00000000-0000-0000-0000-000000000001") // 1
|
|
1089
1087
|
* @example removeLeftPadding("00000000-0000-0000-0000-000000000010") // 10
|
|
1090
1088
|
*/
|
|
1091
|
-
const removeLeftPadding = (id) => Number(id
|
|
1089
|
+
const removeLeftPadding = (id) => Number(id?.replace(/-/g, "").replace(/^0+/, ""));
|
|
1092
1090
|
/**
|
|
1093
1091
|
* Removes hidden characters from a string.
|
|
1094
1092
|
*
|
|
@@ -1107,9 +1105,11 @@ const stripHiddenCharacters = (input) => {
|
|
|
1107
1105
|
* If the value is not rgb, returns back the same value
|
|
1108
1106
|
*/
|
|
1109
1107
|
const rgb2hex = (rgb) => {
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1108
|
+
const hexColor = rgb
|
|
1109
|
+
.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/)
|
|
1110
|
+
?.slice(1)
|
|
1111
|
+
.map(n => parseInt(n, 10).toString(16).padStart(2, "0"))
|
|
1112
|
+
.join("");
|
|
1113
1113
|
return hexColor ? `#${hexColor}` : rgb;
|
|
1114
1114
|
};
|
|
1115
1115
|
/**
|
|
@@ -1177,9 +1177,8 @@ const getAllColors = (base64EncodedSVG) => {
|
|
|
1177
1177
|
}
|
|
1178
1178
|
});
|
|
1179
1179
|
svgImage.querySelectorAll("style").forEach(element => {
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
if (cssRules === null || cssRules === void 0 ? void 0 : cssRules.length) {
|
|
1180
|
+
const cssRules = element.sheet?.cssRules;
|
|
1181
|
+
if (cssRules?.length) {
|
|
1183
1182
|
for (let i = 0; i < cssRules.length; i++) {
|
|
1184
1183
|
// Checking if the style attribute exists
|
|
1185
1184
|
const cssRule = cssRules[i]; // Replace type casting with type assertion
|
|
@@ -1193,24 +1192,21 @@ const getAllColors = (base64EncodedSVG) => {
|
|
|
1193
1192
|
});
|
|
1194
1193
|
// Get fills
|
|
1195
1194
|
svgImage.querySelectorAll("[fill]").forEach(element => {
|
|
1196
|
-
|
|
1197
|
-
const color = (_a = element.attributes.getNamedItem("fill")) === null || _a === void 0 ? void 0 : _a.value;
|
|
1195
|
+
const color = element.attributes.getNamedItem("fill")?.value;
|
|
1198
1196
|
if (color) {
|
|
1199
1197
|
colors.push(color);
|
|
1200
1198
|
}
|
|
1201
1199
|
});
|
|
1202
1200
|
// Get stokes
|
|
1203
1201
|
svgImage.querySelectorAll("[stroke]").forEach(element => {
|
|
1204
|
-
|
|
1205
|
-
const color = (_a = element.attributes.getNamedItem("stroke")) === null || _a === void 0 ? void 0 : _a.value;
|
|
1202
|
+
const color = element.attributes.getNamedItem("stroke")?.value;
|
|
1206
1203
|
if (color) {
|
|
1207
1204
|
colors.push(color);
|
|
1208
1205
|
}
|
|
1209
1206
|
});
|
|
1210
1207
|
// Get stop-color (for gradients)
|
|
1211
1208
|
svgImage.querySelectorAll("[stop-color]").forEach(element => {
|
|
1212
|
-
|
|
1213
|
-
const color = (_a = element.attributes.getNamedItem("stop-color")) === null || _a === void 0 ? void 0 : _a.value;
|
|
1209
|
+
const color = element.attributes.getNamedItem("stop-color")?.value;
|
|
1214
1210
|
if (color) {
|
|
1215
1211
|
colors.push(color);
|
|
1216
1212
|
}
|
|
@@ -1255,31 +1251,6 @@ const isSorted = (sortInput) => {
|
|
|
1255
1251
|
return originalKeys.every((key, index) => key === sortedKeys[index]);
|
|
1256
1252
|
};
|
|
1257
1253
|
|
|
1258
|
-
/**
|
|
1259
|
-
* Converts meters to yards
|
|
1260
|
-
*
|
|
1261
|
-
* @param value The value to convert
|
|
1262
|
-
* @returns {number | undefined} The converted value
|
|
1263
|
-
*/
|
|
1264
|
-
const convertMetersToYards = (value) => {
|
|
1265
|
-
if (isNaN(value)) {
|
|
1266
|
-
return;
|
|
1267
|
-
}
|
|
1268
|
-
return Math.round(value * 1.09361);
|
|
1269
|
-
};
|
|
1270
|
-
/**
|
|
1271
|
-
* Converts yards to meters
|
|
1272
|
-
*
|
|
1273
|
-
* @param value The value to convert
|
|
1274
|
-
* @returns {number | undefined} The converted value
|
|
1275
|
-
*/
|
|
1276
|
-
const convertYardsToMeters = (value) => {
|
|
1277
|
-
if (isNaN(value)) {
|
|
1278
|
-
return;
|
|
1279
|
-
}
|
|
1280
|
-
return Math.round(value * 0.9144);
|
|
1281
|
-
};
|
|
1282
|
-
|
|
1283
1254
|
/**
|
|
1284
1255
|
* Generates a version 3 UUID (namespace with MD5).
|
|
1285
1256
|
*
|
|
@@ -1310,6 +1281,31 @@ const uuidv4 = () => {
|
|
|
1310
1281
|
*/
|
|
1311
1282
|
const uuidv5 = (name, namespace) => uuid.v5(name, namespace);
|
|
1312
1283
|
|
|
1284
|
+
/**
|
|
1285
|
+
* Converts meters to yards
|
|
1286
|
+
*
|
|
1287
|
+
* @param value The value to convert
|
|
1288
|
+
* @returns {number | undefined} The converted value
|
|
1289
|
+
*/
|
|
1290
|
+
const convertMetersToYards = (value) => {
|
|
1291
|
+
if (isNaN(value)) {
|
|
1292
|
+
return;
|
|
1293
|
+
}
|
|
1294
|
+
return Math.round(value * 1.09361);
|
|
1295
|
+
};
|
|
1296
|
+
/**
|
|
1297
|
+
* Converts yards to meters
|
|
1298
|
+
*
|
|
1299
|
+
* @param value The value to convert
|
|
1300
|
+
* @returns {number | undefined} The converted value
|
|
1301
|
+
*/
|
|
1302
|
+
const convertYardsToMeters = (value) => {
|
|
1303
|
+
if (isNaN(value)) {
|
|
1304
|
+
return;
|
|
1305
|
+
}
|
|
1306
|
+
return Math.round(value * 0.9144);
|
|
1307
|
+
};
|
|
1308
|
+
|
|
1313
1309
|
exports.DateTimeFormat = DateTimeFormat;
|
|
1314
1310
|
exports.align = align;
|
|
1315
1311
|
exports.alphabeticallySort = alphabeticallySort;
|
package/index.esm.js
CHANGED
|
@@ -112,14 +112,14 @@ Object.values(object);
|
|
|
112
112
|
* @returns A new array with the items from the previous array and the new array, but only if the item's key is unique.
|
|
113
113
|
*/
|
|
114
114
|
const unionArraysByKey = (key, previous, newArray) => {
|
|
115
|
-
const previousIds =
|
|
116
|
-
const newIds =
|
|
115
|
+
const previousIds = previous?.map(edge => edge[key]) || [];
|
|
116
|
+
const newIds = newArray?.map(edge => edge[key]) || [];
|
|
117
117
|
const mergedIds = [...previousIds, ...newIds];
|
|
118
118
|
const uniqueIds = [...new Set(mergedIds)];
|
|
119
119
|
return uniqueIds
|
|
120
120
|
.map(id => {
|
|
121
|
-
const previousEdge = previous
|
|
122
|
-
const newEdge = newArray
|
|
121
|
+
const previousEdge = previous?.find(edge => edge[key] === id);
|
|
122
|
+
const newEdge = newArray?.find(edge => edge[key] === id);
|
|
123
123
|
return newEdge || previousEdge;
|
|
124
124
|
})
|
|
125
125
|
.filter(truthy);
|
|
@@ -527,7 +527,7 @@ const toID = (uuid) => Number(uuid.replace(/-/g, "").replace(/^0+/, ""));
|
|
|
527
527
|
*
|
|
528
528
|
* @param uuid The list of UUIDs that should be converted to a list of valid machine IDs
|
|
529
529
|
*/
|
|
530
|
-
const toIDs = (ids) =>
|
|
530
|
+
const toIDs = (ids) => ids?.filter(id => !isNaN(toID(id))).map(id => toID(id)) ?? [];
|
|
531
531
|
|
|
532
532
|
/**
|
|
533
533
|
* Converts an HTMLImageElement to a PNG image.
|
|
@@ -542,9 +542,8 @@ const toIDs = (ids) => { var _a; return (_a = ids === null || ids === void 0 ? v
|
|
|
542
542
|
* @throws Error if unable to get the canvas context.
|
|
543
543
|
*/
|
|
544
544
|
const toPNG = ({ image, dimensions, maxHeight, maxWidth, idealArea, }) => {
|
|
545
|
-
|
|
546
|
-
let
|
|
547
|
-
let height = (_b = dimensions === null || dimensions === void 0 ? void 0 : dimensions.height) !== null && _b !== void 0 ? _b : image.height;
|
|
545
|
+
let width = dimensions?.width ?? image.width;
|
|
546
|
+
let height = dimensions?.height ?? image.height;
|
|
548
547
|
const scale = calculateImageScaleRatio(width, height, maxWidth, maxHeight, idealArea);
|
|
549
548
|
width = Math.round(width * scale);
|
|
550
549
|
height = Math.round(height * scale);
|
|
@@ -749,11 +748,10 @@ const trimIds = (str) => {
|
|
|
749
748
|
};
|
|
750
749
|
|
|
751
750
|
const dataURItoBlob = (dataURI) => {
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
const mime = (_e = (_d = dataURI.split(",")[0]) === null || _d === void 0 ? void 0 : _d.split(":")[1]) === null || _e === void 0 ? void 0 : _e.split(";")[0];
|
|
751
|
+
const bytes = (dataURI.split(",")[0] ?? "").indexOf("base64") >= 0
|
|
752
|
+
? atob(dataURI.split(",")[1] ?? "")
|
|
753
|
+
: unescape(dataURI.split(",")[1] ?? "");
|
|
754
|
+
const mime = dataURI.split(",")[0]?.split(":")[1]?.split(";")[0];
|
|
757
755
|
const max = bytes.length;
|
|
758
756
|
const ia = new Uint8Array(max);
|
|
759
757
|
for (let i = 0; i < max; i++) {
|
|
@@ -1086,7 +1084,7 @@ const titleCase = (s) => {
|
|
|
1086
1084
|
* @example removeLeftPadding("00000000-0000-0000-0000-000000000001") // 1
|
|
1087
1085
|
* @example removeLeftPadding("00000000-0000-0000-0000-000000000010") // 10
|
|
1088
1086
|
*/
|
|
1089
|
-
const removeLeftPadding = (id) => Number(id
|
|
1087
|
+
const removeLeftPadding = (id) => Number(id?.replace(/-/g, "").replace(/^0+/, ""));
|
|
1090
1088
|
/**
|
|
1091
1089
|
* Removes hidden characters from a string.
|
|
1092
1090
|
*
|
|
@@ -1105,9 +1103,11 @@ const stripHiddenCharacters = (input) => {
|
|
|
1105
1103
|
* If the value is not rgb, returns back the same value
|
|
1106
1104
|
*/
|
|
1107
1105
|
const rgb2hex = (rgb) => {
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1106
|
+
const hexColor = rgb
|
|
1107
|
+
.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/)
|
|
1108
|
+
?.slice(1)
|
|
1109
|
+
.map(n => parseInt(n, 10).toString(16).padStart(2, "0"))
|
|
1110
|
+
.join("");
|
|
1111
1111
|
return hexColor ? `#${hexColor}` : rgb;
|
|
1112
1112
|
};
|
|
1113
1113
|
/**
|
|
@@ -1175,9 +1175,8 @@ const getAllColors = (base64EncodedSVG) => {
|
|
|
1175
1175
|
}
|
|
1176
1176
|
});
|
|
1177
1177
|
svgImage.querySelectorAll("style").forEach(element => {
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
if (cssRules === null || cssRules === void 0 ? void 0 : cssRules.length) {
|
|
1178
|
+
const cssRules = element.sheet?.cssRules;
|
|
1179
|
+
if (cssRules?.length) {
|
|
1181
1180
|
for (let i = 0; i < cssRules.length; i++) {
|
|
1182
1181
|
// Checking if the style attribute exists
|
|
1183
1182
|
const cssRule = cssRules[i]; // Replace type casting with type assertion
|
|
@@ -1191,24 +1190,21 @@ const getAllColors = (base64EncodedSVG) => {
|
|
|
1191
1190
|
});
|
|
1192
1191
|
// Get fills
|
|
1193
1192
|
svgImage.querySelectorAll("[fill]").forEach(element => {
|
|
1194
|
-
|
|
1195
|
-
const color = (_a = element.attributes.getNamedItem("fill")) === null || _a === void 0 ? void 0 : _a.value;
|
|
1193
|
+
const color = element.attributes.getNamedItem("fill")?.value;
|
|
1196
1194
|
if (color) {
|
|
1197
1195
|
colors.push(color);
|
|
1198
1196
|
}
|
|
1199
1197
|
});
|
|
1200
1198
|
// Get stokes
|
|
1201
1199
|
svgImage.querySelectorAll("[stroke]").forEach(element => {
|
|
1202
|
-
|
|
1203
|
-
const color = (_a = element.attributes.getNamedItem("stroke")) === null || _a === void 0 ? void 0 : _a.value;
|
|
1200
|
+
const color = element.attributes.getNamedItem("stroke")?.value;
|
|
1204
1201
|
if (color) {
|
|
1205
1202
|
colors.push(color);
|
|
1206
1203
|
}
|
|
1207
1204
|
});
|
|
1208
1205
|
// Get stop-color (for gradients)
|
|
1209
1206
|
svgImage.querySelectorAll("[stop-color]").forEach(element => {
|
|
1210
|
-
|
|
1211
|
-
const color = (_a = element.attributes.getNamedItem("stop-color")) === null || _a === void 0 ? void 0 : _a.value;
|
|
1207
|
+
const color = element.attributes.getNamedItem("stop-color")?.value;
|
|
1212
1208
|
if (color) {
|
|
1213
1209
|
colors.push(color);
|
|
1214
1210
|
}
|
|
@@ -1253,31 +1249,6 @@ const isSorted = (sortInput) => {
|
|
|
1253
1249
|
return originalKeys.every((key, index) => key === sortedKeys[index]);
|
|
1254
1250
|
};
|
|
1255
1251
|
|
|
1256
|
-
/**
|
|
1257
|
-
* Converts meters to yards
|
|
1258
|
-
*
|
|
1259
|
-
* @param value The value to convert
|
|
1260
|
-
* @returns {number | undefined} The converted value
|
|
1261
|
-
*/
|
|
1262
|
-
const convertMetersToYards = (value) => {
|
|
1263
|
-
if (isNaN(value)) {
|
|
1264
|
-
return;
|
|
1265
|
-
}
|
|
1266
|
-
return Math.round(value * 1.09361);
|
|
1267
|
-
};
|
|
1268
|
-
/**
|
|
1269
|
-
* Converts yards to meters
|
|
1270
|
-
*
|
|
1271
|
-
* @param value The value to convert
|
|
1272
|
-
* @returns {number | undefined} The converted value
|
|
1273
|
-
*/
|
|
1274
|
-
const convertYardsToMeters = (value) => {
|
|
1275
|
-
if (isNaN(value)) {
|
|
1276
|
-
return;
|
|
1277
|
-
}
|
|
1278
|
-
return Math.round(value * 0.9144);
|
|
1279
|
-
};
|
|
1280
|
-
|
|
1281
1252
|
/**
|
|
1282
1253
|
* Generates a version 3 UUID (namespace with MD5).
|
|
1283
1254
|
*
|
|
@@ -1308,4 +1279,29 @@ const uuidv4 = () => {
|
|
|
1308
1279
|
*/
|
|
1309
1280
|
const uuidv5 = (name, namespace) => v5(name, namespace);
|
|
1310
1281
|
|
|
1282
|
+
/**
|
|
1283
|
+
* Converts meters to yards
|
|
1284
|
+
*
|
|
1285
|
+
* @param value The value to convert
|
|
1286
|
+
* @returns {number | undefined} The converted value
|
|
1287
|
+
*/
|
|
1288
|
+
const convertMetersToYards = (value) => {
|
|
1289
|
+
if (isNaN(value)) {
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
return Math.round(value * 1.09361);
|
|
1293
|
+
};
|
|
1294
|
+
/**
|
|
1295
|
+
* Converts yards to meters
|
|
1296
|
+
*
|
|
1297
|
+
* @param value The value to convert
|
|
1298
|
+
* @returns {number | undefined} The converted value
|
|
1299
|
+
*/
|
|
1300
|
+
const convertYardsToMeters = (value) => {
|
|
1301
|
+
if (isNaN(value)) {
|
|
1302
|
+
return;
|
|
1303
|
+
}
|
|
1304
|
+
return Math.round(value * 0.9144);
|
|
1305
|
+
};
|
|
1306
|
+
|
|
1311
1307
|
export { DateTimeFormat, HoursAndMinutesFormat, align, alphabeticallySort, arrayLengthCompare, arrayNotEmpty, booleanCompare, calculateImageScaleRatio, capitalize, colorsFromStyleDeclaration, convertBlobToBase64, convertMetersToYards, convertYardsToMeters, dateCompare, deleteUndefinedKeys, difference, doNothing, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, fetchImageAsBase64, filterByMultiple, formatAddress, formatCoordinates, fuzzySearch, getAllColors, getDifferenceBetweenDates, getEndOfDay, getFirstLevelObjectPropertyDifferences, getISOStringFromDate, getMimeTypeFromDataURL, getResizedDimensions, getStartOfDay, groupBy, groupTinyDataToOthers, hourIntervals, intersection, isArrayEqual, isSorted, isUUID, isValidImage, loadSVGDimensions, nonNullable, numberCompare, numberCompareUnknownAfterHighest, objNotEmpty, objectEntries, objectFromEntries, objectKeys, objectValues, pick, preload, removeLeftPadding, resizeBlob, resizeImage, rgb2hex, size, stringCompare, stringCompareFromKey, stringNaturalCompare, stripHiddenCharacters, svgToPNG, titleCase, toID, toIDs, toPNG, toUUID, trimIds, trimPath, truthy, unionArraysByKey, uuidv3, uuidv4, uuidv5 };
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED