@tspro/ts-utils-lib 2.2.1 → 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.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  /*!
4
- * TsUtilsLib v2.2.1 (cjs)
4
+ * TsUtilsLib v3.0.0 (cjs)
5
5
  * (c) 2023-2025 PahkaSoft
6
6
  * Licensed under the MIT License
7
7
  */
@@ -1223,26 +1223,38 @@ function eraseAll() {
1223
1223
  var device_exports = {};
1224
1224
  __export(device_exports, {
1225
1225
  DPI: () => DPI,
1226
+ DevicePixelRatio: () => DevicePixelRatio,
1226
1227
  FontSize: () => FontSize,
1227
1228
  HostAddress: () => HostAddress,
1228
1229
  IsMobileDevice: () => IsMobileDevice,
1229
1230
  IsTouchDevice: () => IsTouchDevice,
1230
- PxPerMm: () => PxPerMm,
1231
1231
  ScrollbarWidth: () => ScrollbarWidth,
1232
- mmToPx: () => mmToPx,
1233
- pxToMm: () => pxToMm,
1234
- toPx: () => toPx
1232
+ pxToUnit: () => pxToUnit,
1233
+ pxToUnitStr: () => pxToUnitStr,
1234
+ unitToPx: () => unitToPx
1235
1235
  });
1236
- function getDPI() {
1236
+ var CSS_UNIT_RE = /^\s*([+-]?\d*\.?\d+)\s*(px|cm|mm|in|pt|pc|em|rem|vw|vh|vmin|vmax|%)?\s*$/;
1237
+ function parseCssUnit(input) {
1238
+ const m = CSS_UNIT_RE.exec(input);
1239
+ if (!m) return void 0;
1240
+ return {
1241
+ value: Number(m[1]),
1242
+ unit: m[2] ?? void 0
1243
+ };
1244
+ }
1245
+ function getDevicePixelRatio() {
1246
+ return typeof window === "undefined" ? 1 : window.devicePixelRatio;
1247
+ }
1248
+ function getPxPerUnit(unit, _default) {
1237
1249
  try {
1238
1250
  let el = document.createElement("div");
1239
- el.style.width = "1in";
1251
+ el.style.width = "1" + unit;
1240
1252
  document.body.appendChild(el);
1241
1253
  let dpi = el.offsetWidth;
1242
1254
  el.remove();
1243
- return dpi || 96;
1255
+ return dpi || _default;
1244
1256
  } catch (e) {
1245
- return 96;
1257
+ return _default;
1246
1258
  }
1247
1259
  }
1248
1260
  function getScrollBarWidth() {
@@ -1300,55 +1312,60 @@ function getHostAddress() {
1300
1312
  }
1301
1313
  return `${location.protocol}//${location.host}`;
1302
1314
  }
1303
- var UnitRegExp = /^(mm|cm|in|inch|px|em)$/;
1304
- var ValueUnitRegExp = /^([0-9\\.]+)(.*)$/;
1305
- var DPI = getDPI();
1306
- var PxPerMm = DPI / 25.4;
1307
1315
  var ScrollbarWidth = getScrollBarWidth();
1308
1316
  var FontSize = getSystemFontSize();
1309
1317
  var IsTouchDevice = getIsTouchDevice();
1310
1318
  var IsMobileDevice = getIsMobileDevice();
1311
1319
  var HostAddress = getHostAddress();
1312
- function pxToMm(px) {
1313
- return px / PxPerMm;
1314
- }
1315
- function mmToPx(mm) {
1316
- return mm * PxPerMm;
1317
- }
1318
- function toPx(input) {
1319
- if (typeof input === "number") {
1320
- return input;
1321
- }
1322
- let value = NaN;
1323
- let unit = void 0;
1324
- let match = input.toString().match(ValueUnitRegExp);
1325
- if (match && match[1]) {
1326
- value = parseFloat(match[1]);
1327
- let unitStr = match[2] ? match[2].toLowerCase() : "undefined";
1328
- let unitStrOk = UnitRegExp.test(unitStr);
1329
- unit = unitStrOk ? unitStr : void 0;
1330
- if (!unit) {
1331
- console.log("Unknown unit '" + unitStr + "' => using 'px'.");
1332
- }
1333
- } else {
1334
- value = parseFloat(input);
1335
- }
1336
- assert_exports.isFinite(value, "value in function toPx");
1320
+ var DevicePixelRatio = getDevicePixelRatio();
1321
+ var DPI = getPxPerUnit("in", 96);
1322
+ var PxPerIn = getPxPerUnit("in", 96);
1323
+ var PxPerMm = getPxPerUnit("mm", 96 / 25.4);
1324
+ var PxPerCm = getPxPerUnit("cm", 96 / 25.4 * 10);
1325
+ var PxPerEm = getPxPerUnit("em", FontSize);
1326
+ function unitToPx(valueUnit) {
1327
+ if (typeof valueUnit === "number")
1328
+ return valueUnit;
1329
+ const p = parseCssUnit(valueUnit);
1330
+ const value = p?.value;
1331
+ const unit = p?.unit;
1332
+ if (!guard_exports.isFinite(value))
1333
+ assert_exports.fail(`Invalid value: ${value}`);
1337
1334
  switch (unit) {
1338
1335
  case "mm":
1339
- return mmToPx(value);
1336
+ return value * PxPerMm;
1340
1337
  case "cm":
1341
- return mmToPx(value) * 10;
1338
+ return value * PxPerCm;
1342
1339
  case "in":
1343
- case "inch":
1344
- return mmToPx(value) * 25.4;
1340
+ return value * PxPerIn;
1345
1341
  case "em":
1346
- return FontSize * value;
1347
- default:
1342
+ return value * PxPerEm;
1348
1343
  case "px":
1344
+ case void 0:
1349
1345
  return value;
1346
+ default:
1347
+ assert_exports.fail(`Unsupported CssUnit: ${unit}`);
1348
+ }
1349
+ }
1350
+ function pxToUnit(px, unit) {
1351
+ switch (unit) {
1352
+ case "mm":
1353
+ return px / PxPerMm;
1354
+ case "cm":
1355
+ return px / PxPerCm;
1356
+ case "em":
1357
+ return px / PxPerEm;
1358
+ case "in":
1359
+ return px / PxPerIn;
1360
+ case "px":
1361
+ return px;
1362
+ default:
1363
+ assert_exports.fail(`Unsupported CssUnit: ${unit}`);
1350
1364
  }
1351
1365
  }
1366
+ function pxToUnitStr(px, unit) {
1367
+ return `${pxToUnit(px, unit)}${unit}`;
1368
+ }
1352
1369
 
1353
1370
  // src/utils/index.ts
1354
1371
  var utils_exports = {};
@@ -1449,6 +1466,7 @@ __export(dom_exports, {
1449
1466
  getPadding: () => getPadding,
1450
1467
  getWidth: () => getWidth,
1451
1468
  hasClass: () => hasClass,
1469
+ injectCss: () => injectCss,
1452
1470
  removeClass: () => removeClass,
1453
1471
  removeFromParent: () => removeFromParent,
1454
1472
  setHeight: () => setHeight,
@@ -1461,8 +1479,8 @@ __export(dom_exports, {
1461
1479
  function _getElemById(id) {
1462
1480
  return typeof document === "undefined" ? void 0 : document.getElementById(id) ?? void 0;
1463
1481
  }
1464
- function toPx2(value) {
1465
- return value === void 0 ? void 0 : device_exports.toPx(value);
1482
+ function toPx(value) {
1483
+ return value === void 0 ? void 0 : device_exports.unitToPx(value);
1466
1484
  }
1467
1485
  function hasClass(el, className) {
1468
1486
  if (className.length === 0) {
@@ -1558,38 +1576,38 @@ function getPadding(style) {
1558
1576
  if (!style) {
1559
1577
  return { top: 0, right: 0, bottom: 0, left: 0 };
1560
1578
  }
1561
- let top = toPx2(style.paddingTop);
1562
- let right = toPx2(style.paddingRight);
1563
- let bottom = toPx2(style.paddingBottom);
1564
- let left = toPx2(style.paddingLeft);
1579
+ let top = toPx(style.paddingTop);
1580
+ let right = toPx(style.paddingRight);
1581
+ let bottom = toPx(style.paddingBottom);
1582
+ let left = toPx(style.paddingLeft);
1565
1583
  let padding = (style.padding ?? "").toString().split(" ").filter((s) => s.length > 0);
1566
1584
  switch (padding.length) {
1567
1585
  case 0:
1568
1586
  break;
1569
1587
  case 1:
1570
- top ?? (top = toPx2(padding[0]));
1571
- right ?? (right = toPx2(padding[0]));
1572
- bottom ?? (bottom = toPx2(padding[0]));
1573
- left ?? (left = toPx2(padding[0]));
1588
+ top ?? (top = toPx(padding[0]));
1589
+ right ?? (right = toPx(padding[0]));
1590
+ bottom ?? (bottom = toPx(padding[0]));
1591
+ left ?? (left = toPx(padding[0]));
1574
1592
  break;
1575
1593
  case 2:
1576
- top ?? (top = toPx2(padding[0]));
1577
- right ?? (right = toPx2(padding[1]));
1578
- bottom ?? (bottom = toPx2(padding[0]));
1579
- left ?? (left = toPx2(padding[1]));
1594
+ top ?? (top = toPx(padding[0]));
1595
+ right ?? (right = toPx(padding[1]));
1596
+ bottom ?? (bottom = toPx(padding[0]));
1597
+ left ?? (left = toPx(padding[1]));
1580
1598
  break;
1581
1599
  case 3:
1582
- top ?? (top = toPx2(padding[0]));
1583
- right ?? (right = toPx2(padding[1]));
1584
- bottom ?? (bottom = toPx2(padding[2]));
1585
- left ?? (left = toPx2(padding[1]));
1600
+ top ?? (top = toPx(padding[0]));
1601
+ right ?? (right = toPx(padding[1]));
1602
+ bottom ?? (bottom = toPx(padding[2]));
1603
+ left ?? (left = toPx(padding[1]));
1586
1604
  break;
1587
1605
  case 4:
1588
1606
  default:
1589
- top ?? (top = toPx2(padding[0]));
1590
- right ?? (right = toPx2(padding[1]));
1591
- bottom ?? (bottom = toPx2(padding[2]));
1592
- left ?? (left = toPx2(padding[3]));
1607
+ top ?? (top = toPx(padding[0]));
1608
+ right ?? (right = toPx(padding[1]));
1609
+ bottom ?? (bottom = toPx(padding[2]));
1610
+ left ?? (left = toPx(padding[3]));
1593
1611
  break;
1594
1612
  }
1595
1613
  top ?? (top = 0);
@@ -1599,12 +1617,12 @@ function getPadding(style) {
1599
1617
  return { top, right, bottom, left };
1600
1618
  }
1601
1619
  function getDimension(style) {
1602
- let left = toPx2(style?.left);
1603
- let right = toPx2(style?.right);
1604
- let top = toPx2(style?.top);
1605
- let bottom = toPx2(style?.bottom);
1606
- let width = toPx2(style?.width);
1607
- let height = toPx2(style?.height);
1620
+ let left = toPx(style?.left);
1621
+ let right = toPx(style?.right);
1622
+ let top = toPx(style?.top);
1623
+ let bottom = toPx(style?.bottom);
1624
+ let width = toPx(style?.width);
1625
+ let height = toPx(style?.height);
1608
1626
  if (width === void 0 && left !== void 0 && right !== void 0) {
1609
1627
  width = right - left;
1610
1628
  }
@@ -1632,6 +1650,15 @@ function getCanvasTextWidth(text, font) {
1632
1650
  ctx.font = font;
1633
1651
  return ctx.measureText(text).width;
1634
1652
  }
1653
+ function injectCss(styleId, styleCss) {
1654
+ if (styleId === "" || styleCss === "") return;
1655
+ if (typeof document === "undefined") return;
1656
+ if (document.getElementById(styleId)) return;
1657
+ const style = document.createElement("style");
1658
+ style.id = styleId;
1659
+ style.textContent = styleCss;
1660
+ document.head.appendChild(style);
1661
+ }
1635
1662
 
1636
1663
  // src/utils/math/index.ts
1637
1664
  var math_exports = {};
@@ -4369,7 +4396,7 @@ var LinkedList = class _LinkedList extends BaseContainer {
4369
4396
 
4370
4397
  // src/index.ts
4371
4398
  function getLibInfo() {
4372
- return "TsUtilsLib v2.2.1 (cjs)";
4399
+ return "TsUtilsLib v3.0.0 (cjs)";
4373
4400
  }
4374
4401
 
4375
4402
  exports.AnchoredRect = AnchoredRect;