@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.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * TsUtilsLib v2.2.1 (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}`);
1346
+ }
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}`);
1348
1362
  }
1349
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 = {};
@@ -1447,6 +1464,7 @@ __export(dom_exports, {
1447
1464
  getPadding: () => getPadding,
1448
1465
  getWidth: () => getWidth,
1449
1466
  hasClass: () => hasClass,
1467
+ injectCss: () => injectCss,
1450
1468
  removeClass: () => removeClass,
1451
1469
  removeFromParent: () => removeFromParent,
1452
1470
  setHeight: () => setHeight,
@@ -1459,8 +1477,8 @@ __export(dom_exports, {
1459
1477
  function _getElemById(id) {
1460
1478
  return typeof document === "undefined" ? void 0 : document.getElementById(id) ?? void 0;
1461
1479
  }
1462
- function toPx2(value) {
1463
- 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);
1464
1482
  }
1465
1483
  function hasClass(el, className) {
1466
1484
  if (className.length === 0) {
@@ -1556,38 +1574,38 @@ function getPadding(style) {
1556
1574
  if (!style) {
1557
1575
  return { top: 0, right: 0, bottom: 0, left: 0 };
1558
1576
  }
1559
- let top = toPx2(style.paddingTop);
1560
- let right = toPx2(style.paddingRight);
1561
- let bottom = toPx2(style.paddingBottom);
1562
- 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);
1563
1581
  let padding = (style.padding ?? "").toString().split(" ").filter((s) => s.length > 0);
1564
1582
  switch (padding.length) {
1565
1583
  case 0:
1566
1584
  break;
1567
1585
  case 1:
1568
- top ?? (top = toPx2(padding[0]));
1569
- right ?? (right = toPx2(padding[0]));
1570
- bottom ?? (bottom = toPx2(padding[0]));
1571
- 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]));
1572
1590
  break;
1573
1591
  case 2:
1574
- top ?? (top = toPx2(padding[0]));
1575
- right ?? (right = toPx2(padding[1]));
1576
- bottom ?? (bottom = toPx2(padding[0]));
1577
- 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]));
1578
1596
  break;
1579
1597
  case 3:
1580
- top ?? (top = toPx2(padding[0]));
1581
- right ?? (right = toPx2(padding[1]));
1582
- bottom ?? (bottom = toPx2(padding[2]));
1583
- 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]));
1584
1602
  break;
1585
1603
  case 4:
1586
1604
  default:
1587
- top ?? (top = toPx2(padding[0]));
1588
- right ?? (right = toPx2(padding[1]));
1589
- bottom ?? (bottom = toPx2(padding[2]));
1590
- 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]));
1591
1609
  break;
1592
1610
  }
1593
1611
  top ?? (top = 0);
@@ -1597,12 +1615,12 @@ function getPadding(style) {
1597
1615
  return { top, right, bottom, left };
1598
1616
  }
1599
1617
  function getDimension(style) {
1600
- let left = toPx2(style?.left);
1601
- let right = toPx2(style?.right);
1602
- let top = toPx2(style?.top);
1603
- let bottom = toPx2(style?.bottom);
1604
- let width = toPx2(style?.width);
1605
- 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);
1606
1624
  if (width === void 0 && left !== void 0 && right !== void 0) {
1607
1625
  width = right - left;
1608
1626
  }
@@ -1630,6 +1648,15 @@ function getCanvasTextWidth(text, font) {
1630
1648
  ctx.font = font;
1631
1649
  return ctx.measureText(text).width;
1632
1650
  }
1651
+ function injectCss(styleId, styleCss) {
1652
+ if (styleId === "" || styleCss === "") return;
1653
+ if (typeof document === "undefined") return;
1654
+ if (document.getElementById(styleId)) return;
1655
+ const style = document.createElement("style");
1656
+ style.id = styleId;
1657
+ style.textContent = styleCss;
1658
+ document.head.appendChild(style);
1659
+ }
1633
1660
 
1634
1661
  // src/utils/math/index.ts
1635
1662
  var math_exports = {};
@@ -4367,7 +4394,7 @@ var LinkedList = class _LinkedList extends BaseContainer {
4367
4394
 
4368
4395
  // src/index.ts
4369
4396
  function getLibInfo() {
4370
- return "TsUtilsLib v2.2.1 (esm)";
4397
+ return "TsUtilsLib v3.0.0 (esm)";
4371
4398
  }
4372
4399
 
4373
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 };