@trackunit/shared-utils 1.0.2 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 = (previous === null || previous === void 0 ? void 0 : previous.map(edge => edge[key])) || [];
118
- const newIds = (newArray === null || newArray === void 0 ? void 0 : newArray.map(edge => edge[key])) || [];
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 === null || previous === void 0 ? void 0 : previous.find(edge => edge[key] === id);
124
- const newEdge = newArray === null || newArray === void 0 ? void 0 : newArray.find(edge => edge[key] === id);
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) => { var _a; return (_a = ids === null || ids === void 0 ? void 0 : ids.filter(id => !isNaN(toID(id))).map(id => toID(id))) !== null && _a !== void 0 ? _a : []; };
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
- var _a, _b;
548
- let width = (_a = dimensions === null || dimensions === void 0 ? void 0 : dimensions.width) !== null && _a !== void 0 ? _a : image.width;
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
- var _a, _b, _c, _d, _e;
755
- const bytes = ((_a = dataURI.split(",")[0]) !== null && _a !== void 0 ? _a : "").indexOf("base64") >= 0
756
- ? atob((_b = dataURI.split(",")[1]) !== null && _b !== void 0 ? _b : "")
757
- : unescape((_c = dataURI.split(",")[1]) !== null && _c !== void 0 ? _c : "");
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 === null || id === void 0 ? void 0 : id.replace(/-/g, "").replace(/^0+/, ""));
1089
+ const removeLeftPadding = (id) => Number(id?.replace(/-/g, "").replace(/^0+/, ""));
1092
1090
  /**
1093
1091
  * Removes hidden characters from a string.
1094
1092
  *
@@ -1097,9 +1095,7 @@ const removeLeftPadding = (id) => Number(id === null || id === void 0 ? void 0 :
1097
1095
  */
1098
1096
  const stripHiddenCharacters = (input) => {
1099
1097
  // Regular expression to match various hidden characters
1100
- const hiddenRegex = new RegExp(
1101
- // eslint-disable-next-line no-control-regex
1102
- /[\u200B-\u200F\u202A-\u202E\u2060-\u2064\u2066-\u206F\uFEFF\uFFF9-\uFFFB\u0000-\u001F\u007F-\u009F]/gu);
1098
+ const hiddenRegex = new RegExp(/[\u200B-\u200F\u202A-\u202E\u2060-\u2064\u2066-\u206F\uFEFF\uFFF9-\uFFFB\u0000-\u001F\u007F-\u009F]/gu);
1103
1099
  // Replace hidden characters with an empty string
1104
1100
  return input.replace(hiddenRegex, "");
1105
1101
  };
@@ -1109,9 +1105,11 @@ const stripHiddenCharacters = (input) => {
1109
1105
  * If the value is not rgb, returns back the same value
1110
1106
  */
1111
1107
  const rgb2hex = (rgb) => {
1112
- var _a;
1113
- const hexColor = (_a = rgb
1114
- .match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/)) === null || _a === void 0 ? void 0 : _a.slice(1).map(n => parseInt(n, 10).toString(16).padStart(2, "0")).join("");
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("");
1115
1113
  return hexColor ? `#${hexColor}` : rgb;
1116
1114
  };
1117
1115
  /**
@@ -1179,9 +1177,8 @@ const getAllColors = (base64EncodedSVG) => {
1179
1177
  }
1180
1178
  });
1181
1179
  svgImage.querySelectorAll("style").forEach(element => {
1182
- var _a;
1183
- const cssRules = (_a = element.sheet) === null || _a === void 0 ? void 0 : _a.cssRules;
1184
- if (cssRules === null || cssRules === void 0 ? void 0 : cssRules.length) {
1180
+ const cssRules = element.sheet?.cssRules;
1181
+ if (cssRules?.length) {
1185
1182
  for (let i = 0; i < cssRules.length; i++) {
1186
1183
  // Checking if the style attribute exists
1187
1184
  const cssRule = cssRules[i]; // Replace type casting with type assertion
@@ -1195,24 +1192,21 @@ const getAllColors = (base64EncodedSVG) => {
1195
1192
  });
1196
1193
  // Get fills
1197
1194
  svgImage.querySelectorAll("[fill]").forEach(element => {
1198
- var _a;
1199
- const color = (_a = element.attributes.getNamedItem("fill")) === null || _a === void 0 ? void 0 : _a.value;
1195
+ const color = element.attributes.getNamedItem("fill")?.value;
1200
1196
  if (color) {
1201
1197
  colors.push(color);
1202
1198
  }
1203
1199
  });
1204
1200
  // Get stokes
1205
1201
  svgImage.querySelectorAll("[stroke]").forEach(element => {
1206
- var _a;
1207
- const color = (_a = element.attributes.getNamedItem("stroke")) === null || _a === void 0 ? void 0 : _a.value;
1202
+ const color = element.attributes.getNamedItem("stroke")?.value;
1208
1203
  if (color) {
1209
1204
  colors.push(color);
1210
1205
  }
1211
1206
  });
1212
1207
  // Get stop-color (for gradients)
1213
1208
  svgImage.querySelectorAll("[stop-color]").forEach(element => {
1214
- var _a;
1215
- 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;
1216
1210
  if (color) {
1217
1211
  colors.push(color);
1218
1212
  }
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 = (previous === null || previous === void 0 ? void 0 : previous.map(edge => edge[key])) || [];
116
- const newIds = (newArray === null || newArray === void 0 ? void 0 : newArray.map(edge => edge[key])) || [];
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 === null || previous === void 0 ? void 0 : previous.find(edge => edge[key] === id);
122
- const newEdge = newArray === null || newArray === void 0 ? void 0 : newArray.find(edge => edge[key] === id);
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) => { var _a; return (_a = ids === null || ids === void 0 ? void 0 : ids.filter(id => !isNaN(toID(id))).map(id => toID(id))) !== null && _a !== void 0 ? _a : []; };
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
- var _a, _b;
546
- let width = (_a = dimensions === null || dimensions === void 0 ? void 0 : dimensions.width) !== null && _a !== void 0 ? _a : image.width;
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
- var _a, _b, _c, _d, _e;
753
- const bytes = ((_a = dataURI.split(",")[0]) !== null && _a !== void 0 ? _a : "").indexOf("base64") >= 0
754
- ? atob((_b = dataURI.split(",")[1]) !== null && _b !== void 0 ? _b : "")
755
- : unescape((_c = dataURI.split(",")[1]) !== null && _c !== void 0 ? _c : "");
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 === null || id === void 0 ? void 0 : id.replace(/-/g, "").replace(/^0+/, ""));
1087
+ const removeLeftPadding = (id) => Number(id?.replace(/-/g, "").replace(/^0+/, ""));
1090
1088
  /**
1091
1089
  * Removes hidden characters from a string.
1092
1090
  *
@@ -1095,9 +1093,7 @@ const removeLeftPadding = (id) => Number(id === null || id === void 0 ? void 0 :
1095
1093
  */
1096
1094
  const stripHiddenCharacters = (input) => {
1097
1095
  // Regular expression to match various hidden characters
1098
- const hiddenRegex = new RegExp(
1099
- // eslint-disable-next-line no-control-regex
1100
- /[\u200B-\u200F\u202A-\u202E\u2060-\u2064\u2066-\u206F\uFEFF\uFFF9-\uFFFB\u0000-\u001F\u007F-\u009F]/gu);
1096
+ const hiddenRegex = new RegExp(/[\u200B-\u200F\u202A-\u202E\u2060-\u2064\u2066-\u206F\uFEFF\uFFF9-\uFFFB\u0000-\u001F\u007F-\u009F]/gu);
1101
1097
  // Replace hidden characters with an empty string
1102
1098
  return input.replace(hiddenRegex, "");
1103
1099
  };
@@ -1107,9 +1103,11 @@ const stripHiddenCharacters = (input) => {
1107
1103
  * If the value is not rgb, returns back the same value
1108
1104
  */
1109
1105
  const rgb2hex = (rgb) => {
1110
- var _a;
1111
- const hexColor = (_a = rgb
1112
- .match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/)) === null || _a === void 0 ? void 0 : _a.slice(1).map(n => parseInt(n, 10).toString(16).padStart(2, "0")).join("");
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("");
1113
1111
  return hexColor ? `#${hexColor}` : rgb;
1114
1112
  };
1115
1113
  /**
@@ -1177,9 +1175,8 @@ const getAllColors = (base64EncodedSVG) => {
1177
1175
  }
1178
1176
  });
1179
1177
  svgImage.querySelectorAll("style").forEach(element => {
1180
- var _a;
1181
- const cssRules = (_a = element.sheet) === null || _a === void 0 ? void 0 : _a.cssRules;
1182
- if (cssRules === null || cssRules === void 0 ? void 0 : cssRules.length) {
1178
+ const cssRules = element.sheet?.cssRules;
1179
+ if (cssRules?.length) {
1183
1180
  for (let i = 0; i < cssRules.length; i++) {
1184
1181
  // Checking if the style attribute exists
1185
1182
  const cssRule = cssRules[i]; // Replace type casting with type assertion
@@ -1193,24 +1190,21 @@ const getAllColors = (base64EncodedSVG) => {
1193
1190
  });
1194
1191
  // Get fills
1195
1192
  svgImage.querySelectorAll("[fill]").forEach(element => {
1196
- var _a;
1197
- const color = (_a = element.attributes.getNamedItem("fill")) === null || _a === void 0 ? void 0 : _a.value;
1193
+ const color = element.attributes.getNamedItem("fill")?.value;
1198
1194
  if (color) {
1199
1195
  colors.push(color);
1200
1196
  }
1201
1197
  });
1202
1198
  // Get stokes
1203
1199
  svgImage.querySelectorAll("[stroke]").forEach(element => {
1204
- var _a;
1205
- const color = (_a = element.attributes.getNamedItem("stroke")) === null || _a === void 0 ? void 0 : _a.value;
1200
+ const color = element.attributes.getNamedItem("stroke")?.value;
1206
1201
  if (color) {
1207
1202
  colors.push(color);
1208
1203
  }
1209
1204
  });
1210
1205
  // Get stop-color (for gradients)
1211
1206
  svgImage.querySelectorAll("[stop-color]").forEach(element => {
1212
- var _a;
1213
- 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;
1214
1208
  if (color) {
1215
1209
  colors.push(color);
1216
1210
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/shared-utils",
3
- "version": "1.0.2",
3
+ "version": "1.1.1",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "engines": {
6
6
  "node": ">=20.x"
@@ -37,4 +37,5 @@ export declare const removeLeftPadding: (id: string | null | undefined) => numbe
37
37
  * @returns {string} The cleaned string with hidden characters removed.
38
38
  */
39
39
  export declare const stripHiddenCharacters: (input: string) => string;
40
- export type StringWithAutocompleteOptions<TOptions extends string> = TOptions | string;
40
+ export type StringWithAutocompleteOptions<TOptions extends string> = TOptions | ({} & string);
41
+ export type NonEmptyString<TString extends string> = "" extends TString ? never : TString;