@tspro/ts-utils-lib 2.2.0 → 2.3.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.0 (esm)
2
+ * TsUtilsLib v2.3.0 (esm)
3
3
  * (c) 2023-2025 PahkaSoft
4
4
  * Licensed under the MIT License
5
5
  */
@@ -1143,7 +1143,9 @@ var _consent;
1143
1143
  var _expires;
1144
1144
  var str = _read(ConsentCookieName);
1145
1145
  _consent = str === "accept" /* Accept */ || str === "decline" /* Decline */ ? str : void 0;
1146
- function _getList() {
1146
+ function _getCookieList() {
1147
+ if (typeof document === "undefined")
1148
+ return [];
1147
1149
  let s = document.cookie;
1148
1150
  return s.split(";").map((c) => c.trim());
1149
1151
  }
@@ -1152,15 +1154,17 @@ function _save(name, value) {
1152
1154
  if (_expires) {
1153
1155
  cookie += "expires=" + _expires.toUTCString() + ";";
1154
1156
  }
1155
- document.cookie = cookie;
1157
+ if (typeof document !== "undefined")
1158
+ document.cookie = cookie;
1156
1159
  return value;
1157
1160
  }
1158
1161
  function _read(name, defaultValue) {
1159
- let str2 = _getList().find((c) => c.startsWith(name + "="));
1162
+ let str2 = _getCookieList().find((c) => c.startsWith(name + "="));
1160
1163
  return str2 === void 0 ? defaultValue : str2.substring(name.length + 1);
1161
1164
  }
1162
1165
  function _erase(name) {
1163
- document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 UTC;";
1166
+ if (typeof document !== "undefined")
1167
+ document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 UTC;";
1164
1168
  }
1165
1169
  function setExpireDays(days) {
1166
1170
  _expires = /* @__PURE__ */ new Date();
@@ -1209,7 +1213,8 @@ function erase(name) {
1209
1213
  _erase(name);
1210
1214
  }
1211
1215
  function eraseAll() {
1212
- document.cookie.split(";").forEach((c) => erase(c.trim().split("=")[0]));
1216
+ if (typeof document !== "undefined")
1217
+ document.cookie.split(";").forEach((c) => erase(c.trim().split("=")[0]));
1213
1218
  }
1214
1219
 
1215
1220
  // src/web/device.ts
@@ -1227,12 +1232,16 @@ __export(device_exports, {
1227
1232
  toPx: () => toPx
1228
1233
  });
1229
1234
  function getDPI() {
1230
- let el = document.createElement("div");
1231
- el.style.width = "1in";
1232
- document.body.appendChild(el);
1233
- let dpi = el.offsetWidth;
1234
- el.remove();
1235
- return dpi || 96;
1235
+ try {
1236
+ let el = document.createElement("div");
1237
+ el.style.width = "1in";
1238
+ document.body.appendChild(el);
1239
+ let dpi = el.offsetWidth;
1240
+ el.remove();
1241
+ return dpi || 96;
1242
+ } catch (e) {
1243
+ return 96;
1244
+ }
1236
1245
  }
1237
1246
  function getScrollBarWidth() {
1238
1247
  try {
@@ -1255,18 +1264,23 @@ function getScrollBarWidth() {
1255
1264
  }
1256
1265
  }
1257
1266
  function getSystemFontSize() {
1258
- let tmpDiv = document.createElement("div");
1259
- tmpDiv.style.cssText = "display:inline-block; padding:0; line-height:1; position:absolute; visibility:hidden; font-size:1em";
1260
- tmpDiv.appendChild(document.createTextNode("M"));
1261
- document.body.appendChild(tmpDiv);
1262
- let fontsize = tmpDiv.offsetHeight;
1263
- document.body.removeChild(tmpDiv);
1264
- return fontsize;
1267
+ try {
1268
+ let tmpDiv = document.createElement("div");
1269
+ tmpDiv.style.cssText = "display:inline-block; padding:0; line-height:1; position:absolute; visibility:hidden; font-size:1em";
1270
+ tmpDiv.appendChild(document.createTextNode("M"));
1271
+ document.body.appendChild(tmpDiv);
1272
+ let fontsize = tmpDiv.offsetHeight;
1273
+ document.body.removeChild(tmpDiv);
1274
+ return fontsize;
1275
+ } catch (e) {
1276
+ return 16;
1277
+ }
1265
1278
  }
1266
1279
  function getIsTouchDevice() {
1267
- if ("ontouchstart" in window || "DocumentTouch" in window || "createTouch" in document && "createTouchList" in document) {
1280
+ if (typeof window === "undefined")
1281
+ return false;
1282
+ if ("ontouchstart" in window || "DocumentTouch" in window || "createTouch" in document && "createTouchList" in document)
1268
1283
  return true;
1269
- }
1270
1284
  var prefixes = " -webkit- -moz- -o- -ms- ".split(" ");
1271
1285
  var mq = function(query2) {
1272
1286
  return window.matchMedia(query2).matches;
@@ -1279,7 +1293,10 @@ function getIsMobileDevice() {
1279
1293
  return /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4));
1280
1294
  }
1281
1295
  function getHostAddress() {
1282
- return location.protocol + "//" + location.host;
1296
+ if (typeof location === "undefined" || !location.host) {
1297
+ return "localhost";
1298
+ }
1299
+ return `${location.protocol}//${location.host}`;
1283
1300
  }
1284
1301
  var UnitRegExp = /^(mm|cm|in|inch|px|em)$/;
1285
1302
  var ValueUnitRegExp = /^([0-9\\.]+)(.*)$/;
@@ -1430,6 +1447,7 @@ __export(dom_exports, {
1430
1447
  getPadding: () => getPadding,
1431
1448
  getWidth: () => getWidth,
1432
1449
  hasClass: () => hasClass,
1450
+ injectCss: () => injectCss,
1433
1451
  removeClass: () => removeClass,
1434
1452
  removeFromParent: () => removeFromParent,
1435
1453
  setHeight: () => setHeight,
@@ -1439,6 +1457,9 @@ __export(dom_exports, {
1439
1457
  setWidth: () => setWidth,
1440
1458
  styleLayoutChanged: () => styleLayoutChanged
1441
1459
  });
1460
+ function _getElemById(id) {
1461
+ return typeof document === "undefined" ? void 0 : document.getElementById(id) ?? void 0;
1462
+ }
1442
1463
  function toPx2(value) {
1443
1464
  return value === void 0 ? void 0 : device_exports.toPx(value);
1444
1465
  }
@@ -1475,12 +1496,17 @@ function setOffset(el, left, top, unit = "px") {
1475
1496
  el.style.top = top + unit;
1476
1497
  }
1477
1498
  function getOffset(el) {
1478
- let box = el.getBoundingClientRect();
1479
- let docElem = document.documentElement;
1480
- return {
1481
- top: box.top + window.pageYOffset - docElem.clientTop,
1482
- left: box.left + window.pageXOffset - docElem.clientLeft
1483
- };
1499
+ let { left, top } = el.getBoundingClientRect();
1500
+ if (typeof window !== "undefined") {
1501
+ left += window.pageXOffset;
1502
+ top += window.pageYOffset;
1503
+ }
1504
+ if (typeof document !== "undefined") {
1505
+ let de = document.documentElement;
1506
+ left -= de.clientLeft;
1507
+ top -= de.clientTop;
1508
+ }
1509
+ return { left, top };
1484
1510
  }
1485
1511
  function getWidth(el) {
1486
1512
  if (el instanceof Window) {
@@ -1520,11 +1546,11 @@ function setRect(el, left, top, width, height, unit = "px") {
1520
1546
  el.style.height = height + unit;
1521
1547
  }
1522
1548
  function getButton(btn) {
1523
- let el = typeof btn === "string" ? document.getElementById(btn) : btn;
1549
+ let el = typeof btn === "string" ? _getElemById(btn) : btn;
1524
1550
  return el instanceof HTMLButtonElement ? el : void 0;
1525
1551
  }
1526
1552
  function getCanvas(canvas2) {
1527
- let el = typeof canvas2 === "string" ? document.getElementById(canvas2) : canvas2;
1553
+ let el = typeof canvas2 === "string" ? _getElemById(canvas2) : canvas2;
1528
1554
  return el instanceof HTMLCanvasElement ? el : void 0;
1529
1555
  }
1530
1556
  function getPadding(style) {
@@ -1597,14 +1623,23 @@ function styleLayoutChanged(style1, style2) {
1597
1623
  }
1598
1624
  var canvas;
1599
1625
  function getCanvasTextWidth(text, font) {
1600
- canvas ?? (canvas = document.createElement("canvas"));
1601
- let ctx = canvas.getContext("2d");
1602
- if (!ctx) {
1626
+ if (!canvas && typeof document !== "undefined")
1627
+ canvas = document.createElement("canvas");
1628
+ let ctx = canvas?.getContext("2d");
1629
+ if (!ctx)
1603
1630
  return 0;
1604
- }
1605
1631
  ctx.font = font;
1606
1632
  return ctx.measureText(text).width;
1607
1633
  }
1634
+ function injectCss(styleId, styleCss) {
1635
+ if (styleId === "" || styleCss === "") return;
1636
+ if (typeof document === "undefined") return;
1637
+ if (document.getElementById(styleId)) return;
1638
+ const style = document.createElement("style");
1639
+ style.id = styleId;
1640
+ style.textContent = styleCss;
1641
+ document.head.appendChild(style);
1642
+ }
1608
1643
 
1609
1644
  // src/utils/math/index.ts
1610
1645
  var math_exports = {};
@@ -4342,7 +4377,7 @@ var LinkedList = class _LinkedList extends BaseContainer {
4342
4377
 
4343
4378
  // src/index.ts
4344
4379
  function getLibInfo() {
4345
- return "TsUtilsLib v2.2.0 (esm)";
4380
+ return "TsUtilsLib v2.3.0 (esm)";
4346
4381
  }
4347
4382
 
4348
4383
  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 };