@tspro/ts-utils-lib 2.3.0 → 3.0.0

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/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * TsUtilsLib v2.3.0 (esm)
2
+ * TsUtilsLib v3.0.0 (esm)
3
3
  * (c) 2023-2025 PahkaSoft
4
4
  * Licensed under the MIT License
5
5
  */
@@ -1221,26 +1221,38 @@ function eraseAll() {
1221
1221
  var device_exports = {};
1222
1222
  __export(device_exports, {
1223
1223
  DPI: () => DPI,
1224
+ DevicePixelRatio: () => DevicePixelRatio,
1224
1225
  FontSize: () => FontSize,
1225
1226
  HostAddress: () => HostAddress,
1226
1227
  IsMobileDevice: () => IsMobileDevice,
1227
1228
  IsTouchDevice: () => IsTouchDevice,
1228
- PxPerMm: () => PxPerMm,
1229
1229
  ScrollbarWidth: () => ScrollbarWidth,
1230
- mmToPx: () => mmToPx,
1231
- pxToMm: () => pxToMm,
1232
- toPx: () => toPx
1230
+ pxToUnit: () => pxToUnit,
1231
+ pxToUnitStr: () => pxToUnitStr,
1232
+ unitToPx: () => unitToPx
1233
1233
  });
1234
- function getDPI() {
1234
+ var CSS_UNIT_RE = /^\s*([+-]?\d*\.?\d+)\s*(px|cm|mm|in|pt|pc|em|rem|vw|vh|vmin|vmax|%)?\s*$/;
1235
+ function parseCssUnit(input) {
1236
+ const m = CSS_UNIT_RE.exec(input);
1237
+ if (!m) return void 0;
1238
+ return {
1239
+ value: Number(m[1]),
1240
+ unit: m[2] ?? void 0
1241
+ };
1242
+ }
1243
+ function getDevicePixelRatio() {
1244
+ return typeof window === "undefined" ? 1 : window.devicePixelRatio;
1245
+ }
1246
+ function getPxPerUnit(unit, _default) {
1235
1247
  try {
1236
1248
  let el = document.createElement("div");
1237
- el.style.width = "1in";
1249
+ el.style.width = "1" + unit;
1238
1250
  document.body.appendChild(el);
1239
1251
  let dpi = el.offsetWidth;
1240
1252
  el.remove();
1241
- return dpi || 96;
1253
+ return dpi || _default;
1242
1254
  } catch (e) {
1243
- return 96;
1255
+ return _default;
1244
1256
  }
1245
1257
  }
1246
1258
  function getScrollBarWidth() {
@@ -1298,55 +1310,60 @@ function getHostAddress() {
1298
1310
  }
1299
1311
  return `${location.protocol}//${location.host}`;
1300
1312
  }
1301
- var UnitRegExp = /^(mm|cm|in|inch|px|em)$/;
1302
- var ValueUnitRegExp = /^([0-9\\.]+)(.*)$/;
1303
- var DPI = getDPI();
1304
- var PxPerMm = DPI / 25.4;
1305
1313
  var ScrollbarWidth = getScrollBarWidth();
1306
1314
  var FontSize = getSystemFontSize();
1307
1315
  var IsTouchDevice = getIsTouchDevice();
1308
1316
  var IsMobileDevice = getIsMobileDevice();
1309
1317
  var HostAddress = getHostAddress();
1310
- function pxToMm(px) {
1311
- return px / PxPerMm;
1312
- }
1313
- function mmToPx(mm) {
1314
- return mm * PxPerMm;
1315
- }
1316
- function toPx(input) {
1317
- if (typeof input === "number") {
1318
- return input;
1319
- }
1320
- let value = NaN;
1321
- let unit = void 0;
1322
- let match = input.toString().match(ValueUnitRegExp);
1323
- if (match && match[1]) {
1324
- value = parseFloat(match[1]);
1325
- let unitStr = match[2] ? match[2].toLowerCase() : "undefined";
1326
- let unitStrOk = UnitRegExp.test(unitStr);
1327
- unit = unitStrOk ? unitStr : void 0;
1328
- if (!unit) {
1329
- console.log("Unknown unit '" + unitStr + "' => using 'px'.");
1330
- }
1331
- } else {
1332
- value = parseFloat(input);
1333
- }
1334
- assert_exports.isFinite(value, "value in function toPx");
1318
+ var DevicePixelRatio = getDevicePixelRatio();
1319
+ var DPI = getPxPerUnit("in", 96);
1320
+ var PxPerIn = getPxPerUnit("in", 96);
1321
+ var PxPerMm = getPxPerUnit("mm", 96 / 25.4);
1322
+ var PxPerCm = getPxPerUnit("cm", 96 / 25.4 * 10);
1323
+ var PxPerEm = getPxPerUnit("em", FontSize);
1324
+ function unitToPx(valueUnit) {
1325
+ if (typeof valueUnit === "number")
1326
+ return valueUnit;
1327
+ const p = parseCssUnit(valueUnit);
1328
+ const value = p?.value;
1329
+ const unit = p?.unit;
1330
+ if (!guard_exports.isFinite(value))
1331
+ assert_exports.fail(`Invalid value: ${value}`);
1335
1332
  switch (unit) {
1336
1333
  case "mm":
1337
- return mmToPx(value);
1334
+ return value * PxPerMm;
1338
1335
  case "cm":
1339
- return mmToPx(value) * 10;
1336
+ return value * PxPerCm;
1340
1337
  case "in":
1341
- case "inch":
1342
- return mmToPx(value) * 25.4;
1338
+ return value * PxPerIn;
1343
1339
  case "em":
1344
- return FontSize * value;
1345
- default:
1340
+ return value * PxPerEm;
1346
1341
  case "px":
1342
+ case void 0:
1347
1343
  return value;
1344
+ default:
1345
+ assert_exports.fail(`Unsupported CssUnit: ${unit}`);
1348
1346
  }
1349
1347
  }
1348
+ function pxToUnit(px, unit) {
1349
+ switch (unit) {
1350
+ case "mm":
1351
+ return px / PxPerMm;
1352
+ case "cm":
1353
+ return px / PxPerCm;
1354
+ case "em":
1355
+ return px / PxPerEm;
1356
+ case "in":
1357
+ return px / PxPerIn;
1358
+ case "px":
1359
+ return px;
1360
+ default:
1361
+ assert_exports.fail(`Unsupported CssUnit: ${unit}`);
1362
+ }
1363
+ }
1364
+ function pxToUnitStr(px, unit) {
1365
+ return `${pxToUnit(px, unit)}${unit}`;
1366
+ }
1350
1367
 
1351
1368
  // src/utils/index.ts
1352
1369
  var utils_exports = {};
@@ -1460,8 +1477,8 @@ __export(dom_exports, {
1460
1477
  function _getElemById(id) {
1461
1478
  return typeof document === "undefined" ? void 0 : document.getElementById(id) ?? void 0;
1462
1479
  }
1463
- function toPx2(value) {
1464
- return value === void 0 ? void 0 : device_exports.toPx(value);
1480
+ function toPx(value) {
1481
+ return value === void 0 ? void 0 : device_exports.unitToPx(value);
1465
1482
  }
1466
1483
  function hasClass(el, className) {
1467
1484
  if (className.length === 0) {
@@ -1557,38 +1574,38 @@ function getPadding(style) {
1557
1574
  if (!style) {
1558
1575
  return { top: 0, right: 0, bottom: 0, left: 0 };
1559
1576
  }
1560
- let top = toPx2(style.paddingTop);
1561
- let right = toPx2(style.paddingRight);
1562
- let bottom = toPx2(style.paddingBottom);
1563
- let left = toPx2(style.paddingLeft);
1577
+ let top = toPx(style.paddingTop);
1578
+ let right = toPx(style.paddingRight);
1579
+ let bottom = toPx(style.paddingBottom);
1580
+ let left = toPx(style.paddingLeft);
1564
1581
  let padding = (style.padding ?? "").toString().split(" ").filter((s) => s.length > 0);
1565
1582
  switch (padding.length) {
1566
1583
  case 0:
1567
1584
  break;
1568
1585
  case 1:
1569
- top ?? (top = toPx2(padding[0]));
1570
- right ?? (right = toPx2(padding[0]));
1571
- bottom ?? (bottom = toPx2(padding[0]));
1572
- left ?? (left = toPx2(padding[0]));
1586
+ top ?? (top = toPx(padding[0]));
1587
+ right ?? (right = toPx(padding[0]));
1588
+ bottom ?? (bottom = toPx(padding[0]));
1589
+ left ?? (left = toPx(padding[0]));
1573
1590
  break;
1574
1591
  case 2:
1575
- top ?? (top = toPx2(padding[0]));
1576
- right ?? (right = toPx2(padding[1]));
1577
- bottom ?? (bottom = toPx2(padding[0]));
1578
- left ?? (left = toPx2(padding[1]));
1592
+ top ?? (top = toPx(padding[0]));
1593
+ right ?? (right = toPx(padding[1]));
1594
+ bottom ?? (bottom = toPx(padding[0]));
1595
+ left ?? (left = toPx(padding[1]));
1579
1596
  break;
1580
1597
  case 3:
1581
- top ?? (top = toPx2(padding[0]));
1582
- right ?? (right = toPx2(padding[1]));
1583
- bottom ?? (bottom = toPx2(padding[2]));
1584
- left ?? (left = toPx2(padding[1]));
1598
+ top ?? (top = toPx(padding[0]));
1599
+ right ?? (right = toPx(padding[1]));
1600
+ bottom ?? (bottom = toPx(padding[2]));
1601
+ left ?? (left = toPx(padding[1]));
1585
1602
  break;
1586
1603
  case 4:
1587
1604
  default:
1588
- top ?? (top = toPx2(padding[0]));
1589
- right ?? (right = toPx2(padding[1]));
1590
- bottom ?? (bottom = toPx2(padding[2]));
1591
- left ?? (left = toPx2(padding[3]));
1605
+ top ?? (top = toPx(padding[0]));
1606
+ right ?? (right = toPx(padding[1]));
1607
+ bottom ?? (bottom = toPx(padding[2]));
1608
+ left ?? (left = toPx(padding[3]));
1592
1609
  break;
1593
1610
  }
1594
1611
  top ?? (top = 0);
@@ -1598,12 +1615,12 @@ function getPadding(style) {
1598
1615
  return { top, right, bottom, left };
1599
1616
  }
1600
1617
  function getDimension(style) {
1601
- let left = toPx2(style?.left);
1602
- let right = toPx2(style?.right);
1603
- let top = toPx2(style?.top);
1604
- let bottom = toPx2(style?.bottom);
1605
- let width = toPx2(style?.width);
1606
- let height = toPx2(style?.height);
1618
+ let left = toPx(style?.left);
1619
+ let right = toPx(style?.right);
1620
+ let top = toPx(style?.top);
1621
+ let bottom = toPx(style?.bottom);
1622
+ let width = toPx(style?.width);
1623
+ let height = toPx(style?.height);
1607
1624
  if (width === void 0 && left !== void 0 && right !== void 0) {
1608
1625
  width = right - left;
1609
1626
  }
@@ -4377,7 +4394,7 @@ var LinkedList = class _LinkedList extends BaseContainer {
4377
4394
 
4378
4395
  // src/index.ts
4379
4396
  function getLibInfo() {
4380
- return "TsUtilsLib v2.3.0 (esm)";
4397
+ return "TsUtilsLib v3.0.0 (esm)";
4381
4398
  }
4382
4399
 
4383
4400
  export { AnchoredRect, assert_exports as Assert, BaseContainer, BiMap, cookies_exports as Cookies, DefaultArray, DefaultEqualityFn, device_exports as Device, guard_exports as Guard, IndexArray, LRUCache, LinkedList, MultiContainer, Rect, SignedIndexArray, Stack, TriMap, UniMap, utils_exports as Utils, ValueSet, Vec, asMulti, getLibInfo };