@tspro/ts-utils-lib 2.3.0 → 3.1.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.3.0 (cjs)
4
+ * TsUtilsLib v3.1.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}`);
1350
1348
  }
1351
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}`);
1364
+ }
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 = {};
@@ -1462,8 +1479,8 @@ __export(dom_exports, {
1462
1479
  function _getElemById(id) {
1463
1480
  return typeof document === "undefined" ? void 0 : document.getElementById(id) ?? void 0;
1464
1481
  }
1465
- function toPx2(value) {
1466
- 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);
1467
1484
  }
1468
1485
  function hasClass(el, className) {
1469
1486
  if (className.length === 0) {
@@ -1559,38 +1576,38 @@ function getPadding(style) {
1559
1576
  if (!style) {
1560
1577
  return { top: 0, right: 0, bottom: 0, left: 0 };
1561
1578
  }
1562
- let top = toPx2(style.paddingTop);
1563
- let right = toPx2(style.paddingRight);
1564
- let bottom = toPx2(style.paddingBottom);
1565
- 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);
1566
1583
  let padding = (style.padding ?? "").toString().split(" ").filter((s) => s.length > 0);
1567
1584
  switch (padding.length) {
1568
1585
  case 0:
1569
1586
  break;
1570
1587
  case 1:
1571
- top ?? (top = toPx2(padding[0]));
1572
- right ?? (right = toPx2(padding[0]));
1573
- bottom ?? (bottom = toPx2(padding[0]));
1574
- 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]));
1575
1592
  break;
1576
1593
  case 2:
1577
- top ?? (top = toPx2(padding[0]));
1578
- right ?? (right = toPx2(padding[1]));
1579
- bottom ?? (bottom = toPx2(padding[0]));
1580
- 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]));
1581
1598
  break;
1582
1599
  case 3:
1583
- top ?? (top = toPx2(padding[0]));
1584
- right ?? (right = toPx2(padding[1]));
1585
- bottom ?? (bottom = toPx2(padding[2]));
1586
- 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]));
1587
1604
  break;
1588
1605
  case 4:
1589
1606
  default:
1590
- top ?? (top = toPx2(padding[0]));
1591
- right ?? (right = toPx2(padding[1]));
1592
- bottom ?? (bottom = toPx2(padding[2]));
1593
- 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]));
1594
1611
  break;
1595
1612
  }
1596
1613
  top ?? (top = 0);
@@ -1600,12 +1617,12 @@ function getPadding(style) {
1600
1617
  return { top, right, bottom, left };
1601
1618
  }
1602
1619
  function getDimension(style) {
1603
- let left = toPx2(style?.left);
1604
- let right = toPx2(style?.right);
1605
- let top = toPx2(style?.top);
1606
- let bottom = toPx2(style?.bottom);
1607
- let width = toPx2(style?.width);
1608
- 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);
1609
1626
  if (width === void 0 && left !== void 0 && right !== void 0) {
1610
1627
  width = right - left;
1611
1628
  }
@@ -4377,15 +4394,40 @@ var LinkedList = class _LinkedList extends BaseContainer {
4377
4394
  }
4378
4395
  };
4379
4396
 
4397
+ // src/core/call-tracker.ts
4398
+ var _CallTracker = class _CallTracker {
4399
+ constructor() {
4400
+ __publicField(this, "counts", new UniMap((a, b) => a === b));
4401
+ }
4402
+ getCallCountFor(value) {
4403
+ let oldCount = this.counts.getOrCreate(value, 0);
4404
+ this.counts.set(value, oldCount + 1);
4405
+ return oldCount;
4406
+ }
4407
+ hasBeenCalledWith(value) {
4408
+ return this.getCallCountFor(value) > 0;
4409
+ }
4410
+ static getCallCountFor(value) {
4411
+ return this._default.getCallCountFor(value);
4412
+ }
4413
+ static hasBeenCalledWith(value) {
4414
+ return this._default.hasBeenCalledWith(value);
4415
+ }
4416
+ };
4417
+ // Static default interface
4418
+ __publicField(_CallTracker, "_default", new _CallTracker());
4419
+ var CallTracker = _CallTracker;
4420
+
4380
4421
  // src/index.ts
4381
4422
  function getLibInfo() {
4382
- return "TsUtilsLib v2.3.0 (cjs)";
4423
+ return "TsUtilsLib v3.1.0 (cjs)";
4383
4424
  }
4384
4425
 
4385
4426
  exports.AnchoredRect = AnchoredRect;
4386
4427
  exports.Assert = assert_exports;
4387
4428
  exports.BaseContainer = BaseContainer;
4388
4429
  exports.BiMap = BiMap;
4430
+ exports.CallTracker = CallTracker;
4389
4431
  exports.Cookies = cookies_exports;
4390
4432
  exports.DefaultArray = DefaultArray;
4391
4433
  exports.DefaultEqualityFn = DefaultEqualityFn;