@trackunit/shared-utils 1.10.0 → 1.10.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
@@ -1357,33 +1357,131 @@ const uuidv4 = () => {
1357
1357
  */
1358
1358
  const uuidv5 = (name, namespace) => uuid.v5(name, namespace);
1359
1359
 
1360
+ const METERS_PER_YARD = 0.9144; // 1 yard = 0.9144 meters
1361
+ const METERS_PER_KILOMETER = 1000; // 1 kilometer = 1000 meters
1362
+ const YARDS_PER_METER = 1.09361; // 1 meter = 1.09361 yards
1363
+ const YARDS_PER_MILE = 1760; // 1 mile = 1760 yards
1364
+ const UnitsOfMeasurementSI = {
1365
+ METERS: "METERS",
1366
+ KILOMETERS: "KILOMETERS",
1367
+ };
1368
+ const UnitsOfMeasurementUSCustomary = {
1369
+ YARDS: "YARDS",
1370
+ MILES: "MILES",
1371
+ };
1360
1372
  /**
1361
1373
  * Converts meters to yards
1362
1374
  *
1363
1375
  * @param value The value to convert
1376
+ * @param shouldRound Whether to round the result to the nearest integer (default: true)
1377
+ * @returns {number | undefined} The converted value
1378
+ */
1379
+ const convertMetersToYards = (value, shouldRound = true) => {
1380
+ if (isNaN(value)) {
1381
+ return;
1382
+ }
1383
+ return shouldRound ? Math.round(value * YARDS_PER_METER) : value * YARDS_PER_METER;
1384
+ };
1385
+ /**
1386
+ * Converts meters to kilometers
1387
+ *
1388
+ * @param value The value to convert
1389
+ * @param shouldRound Whether to round the result to the nearest integer (default: true)
1364
1390
  * @returns {number | undefined} The converted value
1365
1391
  */
1366
- const convertMetersToYards = (value) => {
1392
+ const convertMetersToKilometers = (value, shouldRound = true) => {
1367
1393
  if (isNaN(value)) {
1368
1394
  return;
1369
1395
  }
1370
- return Math.round(value * 1.09361);
1396
+ return shouldRound ? Math.round(value / METERS_PER_KILOMETER) : value / METERS_PER_KILOMETER;
1371
1397
  };
1372
1398
  /**
1373
1399
  * Converts yards to meters
1374
1400
  *
1375
1401
  * @param value The value to convert
1402
+ * @param shouldRound Whether to round the result to the nearest integer (default: true)
1403
+ * @returns {number | undefined} The converted value
1404
+ */
1405
+ const convertYardsToMeters = (value, shouldRound = true) => {
1406
+ if (isNaN(value)) {
1407
+ return;
1408
+ }
1409
+ return shouldRound ? Math.round(value * METERS_PER_YARD) : value * METERS_PER_YARD;
1410
+ };
1411
+ /**
1412
+ * Converts yards to miles
1413
+ *
1414
+ * @param value The value to convert
1415
+ * @param shouldRound Whether to round the result to the nearest integer (default: true)
1376
1416
  * @returns {number | undefined} The converted value
1377
1417
  */
1378
- const convertYardsToMeters = (value) => {
1418
+ const convertYardsToMiles = (value, shouldRound = true) => {
1419
+ if (isNaN(value)) {
1420
+ return;
1421
+ }
1422
+ return shouldRound ? Math.round(value / YARDS_PER_MILE) : value / YARDS_PER_MILE;
1423
+ };
1424
+ /**
1425
+ * Returns a human readable distance in either meters or kilometers
1426
+ *
1427
+ * - less than 1km -> meters rounded to the nearest integer
1428
+ * - less than 100km -> kilometers rounded to 1 decimal
1429
+ * - greater than 100km -> kilometers rounded to the nearest integer
1430
+ *
1431
+ * @param value The distance in meters
1432
+ * @returns {{distance: number; unit: UnitsOfMeasurementSIType} | undefined} The formatted distance
1433
+ */
1434
+ const formatSiDistance = (value) => {
1435
+ if (isNaN(value)) {
1436
+ return;
1437
+ }
1438
+ if (value < METERS_PER_KILOMETER) {
1439
+ return { distance: Math.round(value), unit: UnitsOfMeasurementSI.METERS };
1440
+ }
1441
+ else {
1442
+ const kilometers = convertMetersToKilometers(value, false);
1443
+ if (kilometers === undefined) {
1444
+ return;
1445
+ }
1446
+ return {
1447
+ distance: kilometers < 100 ? Number(kilometers.toFixed(1)) : Math.round(kilometers),
1448
+ unit: UnitsOfMeasurementSI.KILOMETERS,
1449
+ };
1450
+ }
1451
+ };
1452
+ /**
1453
+ * Returns a human readable distance in either yards or miles
1454
+ *
1455
+ * - less than 1 mile -> yards rounded to the nearest integer
1456
+ * - less than 100 miles -> miles rounded to 1 decimal
1457
+ * - greater than 100 miles -> miles rounded to the nearest integer
1458
+ *
1459
+ * @param value The distance in yards
1460
+ * @returns {{distance: number; unit: UnitsOfMeasurementUSCustomaryType} | undefined} The formatted distance
1461
+ */
1462
+ const formatUsCustomaryDistance = (value) => {
1379
1463
  if (isNaN(value)) {
1380
1464
  return;
1381
1465
  }
1382
- return Math.round(value * 0.9144);
1466
+ if (value < YARDS_PER_MILE) {
1467
+ return { distance: Math.round(value), unit: UnitsOfMeasurementUSCustomary.YARDS };
1468
+ }
1469
+ else {
1470
+ const miles = convertYardsToMiles(value, false);
1471
+ if (miles === undefined) {
1472
+ return;
1473
+ }
1474
+ return {
1475
+ distance: miles < 100 ? Number(miles.toFixed(1)) : Math.round(miles),
1476
+ unit: UnitsOfMeasurementUSCustomary.MILES,
1477
+ };
1478
+ }
1383
1479
  };
1384
1480
 
1385
1481
  exports.DateTimeFormat = DateTimeFormat;
1386
1482
  exports.HoursAndMinutesFormat = HoursAndMinutesFormat;
1483
+ exports.UnitsOfMeasurementSI = UnitsOfMeasurementSI;
1484
+ exports.UnitsOfMeasurementUSCustomary = UnitsOfMeasurementUSCustomary;
1387
1485
  exports.align = align;
1388
1486
  exports.alphabeticallySort = alphabeticallySort;
1389
1487
  exports.arrayLengthCompare = arrayLengthCompare;
@@ -1393,8 +1491,10 @@ exports.calculateImageScaleRatio = calculateImageScaleRatio;
1393
1491
  exports.capitalize = capitalize;
1394
1492
  exports.colorsFromStyleDeclaration = colorsFromStyleDeclaration;
1395
1493
  exports.convertBlobToBase64 = convertBlobToBase64;
1494
+ exports.convertMetersToKilometers = convertMetersToKilometers;
1396
1495
  exports.convertMetersToYards = convertMetersToYards;
1397
1496
  exports.convertYardsToMeters = convertYardsToMeters;
1497
+ exports.convertYardsToMiles = convertYardsToMiles;
1398
1498
  exports.dateCompare = dateCompare;
1399
1499
  exports.deleteUndefinedKeys = deleteUndefinedKeys;
1400
1500
  exports.difference = difference;
@@ -1407,6 +1507,8 @@ exports.fetchImageAsBase64 = fetchImageAsBase64;
1407
1507
  exports.filterByMultiple = filterByMultiple;
1408
1508
  exports.formatAddress = formatAddress;
1409
1509
  exports.formatCoordinates = formatCoordinates;
1510
+ exports.formatSiDistance = formatSiDistance;
1511
+ exports.formatUsCustomaryDistance = formatUsCustomaryDistance;
1410
1512
  exports.fuzzySearch = fuzzySearch;
1411
1513
  exports.getAllColors = getAllColors;
1412
1514
  exports.getDifferenceBetweenDates = getDifferenceBetweenDates;
package/index.esm.js CHANGED
@@ -1355,29 +1355,125 @@ const uuidv4 = () => {
1355
1355
  */
1356
1356
  const uuidv5 = (name, namespace) => v5(name, namespace);
1357
1357
 
1358
+ const METERS_PER_YARD = 0.9144; // 1 yard = 0.9144 meters
1359
+ const METERS_PER_KILOMETER = 1000; // 1 kilometer = 1000 meters
1360
+ const YARDS_PER_METER = 1.09361; // 1 meter = 1.09361 yards
1361
+ const YARDS_PER_MILE = 1760; // 1 mile = 1760 yards
1362
+ const UnitsOfMeasurementSI = {
1363
+ METERS: "METERS",
1364
+ KILOMETERS: "KILOMETERS",
1365
+ };
1366
+ const UnitsOfMeasurementUSCustomary = {
1367
+ YARDS: "YARDS",
1368
+ MILES: "MILES",
1369
+ };
1358
1370
  /**
1359
1371
  * Converts meters to yards
1360
1372
  *
1361
1373
  * @param value The value to convert
1374
+ * @param shouldRound Whether to round the result to the nearest integer (default: true)
1375
+ * @returns {number | undefined} The converted value
1376
+ */
1377
+ const convertMetersToYards = (value, shouldRound = true) => {
1378
+ if (isNaN(value)) {
1379
+ return;
1380
+ }
1381
+ return shouldRound ? Math.round(value * YARDS_PER_METER) : value * YARDS_PER_METER;
1382
+ };
1383
+ /**
1384
+ * Converts meters to kilometers
1385
+ *
1386
+ * @param value The value to convert
1387
+ * @param shouldRound Whether to round the result to the nearest integer (default: true)
1362
1388
  * @returns {number | undefined} The converted value
1363
1389
  */
1364
- const convertMetersToYards = (value) => {
1390
+ const convertMetersToKilometers = (value, shouldRound = true) => {
1365
1391
  if (isNaN(value)) {
1366
1392
  return;
1367
1393
  }
1368
- return Math.round(value * 1.09361);
1394
+ return shouldRound ? Math.round(value / METERS_PER_KILOMETER) : value / METERS_PER_KILOMETER;
1369
1395
  };
1370
1396
  /**
1371
1397
  * Converts yards to meters
1372
1398
  *
1373
1399
  * @param value The value to convert
1400
+ * @param shouldRound Whether to round the result to the nearest integer (default: true)
1401
+ * @returns {number | undefined} The converted value
1402
+ */
1403
+ const convertYardsToMeters = (value, shouldRound = true) => {
1404
+ if (isNaN(value)) {
1405
+ return;
1406
+ }
1407
+ return shouldRound ? Math.round(value * METERS_PER_YARD) : value * METERS_PER_YARD;
1408
+ };
1409
+ /**
1410
+ * Converts yards to miles
1411
+ *
1412
+ * @param value The value to convert
1413
+ * @param shouldRound Whether to round the result to the nearest integer (default: true)
1374
1414
  * @returns {number | undefined} The converted value
1375
1415
  */
1376
- const convertYardsToMeters = (value) => {
1416
+ const convertYardsToMiles = (value, shouldRound = true) => {
1417
+ if (isNaN(value)) {
1418
+ return;
1419
+ }
1420
+ return shouldRound ? Math.round(value / YARDS_PER_MILE) : value / YARDS_PER_MILE;
1421
+ };
1422
+ /**
1423
+ * Returns a human readable distance in either meters or kilometers
1424
+ *
1425
+ * - less than 1km -> meters rounded to the nearest integer
1426
+ * - less than 100km -> kilometers rounded to 1 decimal
1427
+ * - greater than 100km -> kilometers rounded to the nearest integer
1428
+ *
1429
+ * @param value The distance in meters
1430
+ * @returns {{distance: number; unit: UnitsOfMeasurementSIType} | undefined} The formatted distance
1431
+ */
1432
+ const formatSiDistance = (value) => {
1433
+ if (isNaN(value)) {
1434
+ return;
1435
+ }
1436
+ if (value < METERS_PER_KILOMETER) {
1437
+ return { distance: Math.round(value), unit: UnitsOfMeasurementSI.METERS };
1438
+ }
1439
+ else {
1440
+ const kilometers = convertMetersToKilometers(value, false);
1441
+ if (kilometers === undefined) {
1442
+ return;
1443
+ }
1444
+ return {
1445
+ distance: kilometers < 100 ? Number(kilometers.toFixed(1)) : Math.round(kilometers),
1446
+ unit: UnitsOfMeasurementSI.KILOMETERS,
1447
+ };
1448
+ }
1449
+ };
1450
+ /**
1451
+ * Returns a human readable distance in either yards or miles
1452
+ *
1453
+ * - less than 1 mile -> yards rounded to the nearest integer
1454
+ * - less than 100 miles -> miles rounded to 1 decimal
1455
+ * - greater than 100 miles -> miles rounded to the nearest integer
1456
+ *
1457
+ * @param value The distance in yards
1458
+ * @returns {{distance: number; unit: UnitsOfMeasurementUSCustomaryType} | undefined} The formatted distance
1459
+ */
1460
+ const formatUsCustomaryDistance = (value) => {
1377
1461
  if (isNaN(value)) {
1378
1462
  return;
1379
1463
  }
1380
- return Math.round(value * 0.9144);
1464
+ if (value < YARDS_PER_MILE) {
1465
+ return { distance: Math.round(value), unit: UnitsOfMeasurementUSCustomary.YARDS };
1466
+ }
1467
+ else {
1468
+ const miles = convertYardsToMiles(value, false);
1469
+ if (miles === undefined) {
1470
+ return;
1471
+ }
1472
+ return {
1473
+ distance: miles < 100 ? Number(miles.toFixed(1)) : Math.round(miles),
1474
+ unit: UnitsOfMeasurementUSCustomary.MILES,
1475
+ };
1476
+ }
1381
1477
  };
1382
1478
 
1383
- 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, removeProperties, removeProperty, replaceNullableNumbersWithZero, resizeBlob, resizeImage, rgb2hex, size, stringCompare, stringCompareFromKey, stringNaturalCompare, stripHiddenCharacters, svgToPNG, titleCase, toID, toIDs, toPNG, toUUID, trimIds, trimPath, truthy, unionArraysByKey, uuidv3, uuidv4, uuidv5 };
1479
+ export { DateTimeFormat, HoursAndMinutesFormat, UnitsOfMeasurementSI, UnitsOfMeasurementUSCustomary, align, alphabeticallySort, arrayLengthCompare, arrayNotEmpty, booleanCompare, calculateImageScaleRatio, capitalize, colorsFromStyleDeclaration, convertBlobToBase64, convertMetersToKilometers, convertMetersToYards, convertYardsToMeters, convertYardsToMiles, dateCompare, deleteUndefinedKeys, difference, doNothing, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, fetchImageAsBase64, filterByMultiple, formatAddress, formatCoordinates, formatSiDistance, formatUsCustomaryDistance, 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, removeProperties, removeProperty, replaceNullableNumbersWithZero, 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/shared-utils",
3
- "version": "1.10.0",
3
+ "version": "1.10.1",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "engines": {
6
6
  "node": ">=24.x"
@@ -1,14 +1,70 @@
1
+ export declare const UnitsOfMeasurementSI: {
2
+ readonly METERS: "METERS";
3
+ readonly KILOMETERS: "KILOMETERS";
4
+ };
5
+ export declare const UnitsOfMeasurementUSCustomary: {
6
+ readonly YARDS: "YARDS";
7
+ readonly MILES: "MILES";
8
+ };
9
+ export type UnitsOfMeasurementSIType = (typeof UnitsOfMeasurementSI)[keyof typeof UnitsOfMeasurementSI];
10
+ export type UnitsOfMeasurementUSCustomaryType = (typeof UnitsOfMeasurementUSCustomary)[keyof typeof UnitsOfMeasurementUSCustomary];
1
11
  /**
2
12
  * Converts meters to yards
3
13
  *
4
14
  * @param value The value to convert
15
+ * @param shouldRound Whether to round the result to the nearest integer (default: true)
5
16
  * @returns {number | undefined} The converted value
6
17
  */
7
- export declare const convertMetersToYards: (value: number) => number | undefined;
18
+ export declare const convertMetersToYards: (value: number, shouldRound?: boolean) => number | undefined;
19
+ /**
20
+ * Converts meters to kilometers
21
+ *
22
+ * @param value The value to convert
23
+ * @param shouldRound Whether to round the result to the nearest integer (default: true)
24
+ * @returns {number | undefined} The converted value
25
+ */
26
+ export declare const convertMetersToKilometers: (value: number, shouldRound?: boolean) => number | undefined;
8
27
  /**
9
28
  * Converts yards to meters
10
29
  *
11
30
  * @param value The value to convert
31
+ * @param shouldRound Whether to round the result to the nearest integer (default: true)
32
+ * @returns {number | undefined} The converted value
33
+ */
34
+ export declare const convertYardsToMeters: (value: number, shouldRound?: boolean) => number | undefined;
35
+ /**
36
+ * Converts yards to miles
37
+ *
38
+ * @param value The value to convert
39
+ * @param shouldRound Whether to round the result to the nearest integer (default: true)
12
40
  * @returns {number | undefined} The converted value
13
41
  */
14
- export declare const convertYardsToMeters: (value: number) => number | undefined;
42
+ export declare const convertYardsToMiles: (value: number, shouldRound?: boolean) => number | undefined;
43
+ /**
44
+ * Returns a human readable distance in either meters or kilometers
45
+ *
46
+ * - less than 1km -> meters rounded to the nearest integer
47
+ * - less than 100km -> kilometers rounded to 1 decimal
48
+ * - greater than 100km -> kilometers rounded to the nearest integer
49
+ *
50
+ * @param value The distance in meters
51
+ * @returns {{distance: number; unit: UnitsOfMeasurementSIType} | undefined} The formatted distance
52
+ */
53
+ export declare const formatSiDistance: (value: number) => {
54
+ distance: number;
55
+ unit: UnitsOfMeasurementSIType;
56
+ } | undefined;
57
+ /**
58
+ * Returns a human readable distance in either yards or miles
59
+ *
60
+ * - less than 1 mile -> yards rounded to the nearest integer
61
+ * - less than 100 miles -> miles rounded to 1 decimal
62
+ * - greater than 100 miles -> miles rounded to the nearest integer
63
+ *
64
+ * @param value The distance in yards
65
+ * @returns {{distance: number; unit: UnitsOfMeasurementUSCustomaryType} | undefined} The formatted distance
66
+ */
67
+ export declare const formatUsCustomaryDistance: (value: number) => {
68
+ distance: number;
69
+ unit: UnitsOfMeasurementUSCustomaryType;
70
+ } | undefined;