@putkoff/abstract-utilities 0.1.240 → 0.1.242

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/esm/index.js CHANGED
@@ -1,5 +1,8 @@
1
1
  export { useCallback, useEffect, useRef, useState } from 'react';
2
- import path from 'path';
2
+ import path$1 from 'path';
3
+ import * as fs from 'node:fs';
4
+ import * as fsp from 'node:fs/promises';
5
+ import * as path from 'node:path';
3
6
  import { jsx, jsxs } from 'react/jsx-runtime';
4
7
 
5
8
  function getSafeDocument() {
@@ -514,7 +517,6 @@ function fetchIt(endpoint_1) {
514
517
  ? JSON.stringify(body)
515
518
  : undefined,
516
519
  };
517
- console.debug("➡️ secureFetchIt →", url, opts);
518
520
  let res = yield fetch(url, opts);
519
521
  if (!res.ok) {
520
522
  const err = yield res.text();
@@ -601,7 +603,7 @@ function get_full_path(partial_path, parent_dir = null) {
601
603
  if (typeof partial_path !== 'string') {
602
604
  throw new Error('partial_path must be a string');
603
605
  }
604
- if (path.isAbsolute(partial_path)) {
606
+ if (path$1.isAbsolute(partial_path)) {
605
607
  return partial_path;
606
608
  }
607
609
  return urlJoin(parent_dir, partial_path);
@@ -616,7 +618,7 @@ function path_to_url(filePath, all_paths) {
616
618
  }
617
619
  for (const key in all_paths) {
618
620
  const mapping = all_paths[key];
619
- const normalizedBase = path.normalize(mapping.path);
621
+ const normalizedBase = path$1.normalize(mapping.path);
620
622
  if (filePath.startsWith(normalizedBase)) {
621
623
  const relativePath = filePath.substring(normalizedBase.length);
622
624
  return urlJoin(mapping.url, relativePath.replace(/\\/g, '/'));
@@ -1277,6 +1279,313 @@ function getAlphas() {
1277
1279
  return 'abcdefghijklmnopqrstuvwxyz';
1278
1280
  }
1279
1281
 
1282
+ /** ---- Data: large but explicit, mirrors your Python mapping ---- */
1283
+ const MIME_TYPES = {
1284
+ image: {
1285
+ ".jpg": "image/jpeg",
1286
+ ".jpeg": "image/jpeg",
1287
+ ".png": "image/png",
1288
+ ".gif": "image/gif",
1289
+ ".bmp": "image/bmp",
1290
+ ".tiff": "image/tiff",
1291
+ ".webp": "image/webp",
1292
+ ".svg": "image/svg+xml",
1293
+ ".ico": "image/vnd.microsoft.icon",
1294
+ ".heic": "image/heic",
1295
+ ".psd": "image/vnd.adobe.photoshop",
1296
+ ".raw": "image/x-raw",
1297
+ },
1298
+ video: {
1299
+ ".mp4": "video/mp4",
1300
+ ".webm": "video/webm",
1301
+ ".ogg": "video/ogg",
1302
+ ".mov": "video/quicktime",
1303
+ ".avi": "video/x-msvideo",
1304
+ ".mkv": "video/x-matroska",
1305
+ ".flv": "video/x-flv",
1306
+ ".wmv": "video/x-ms-wmv",
1307
+ ".3gp": "video/3gpp",
1308
+ ".ts": "video/mp2t",
1309
+ ".mpeg": "video/mpeg",
1310
+ ".mpg": "video/mpg",
1311
+ },
1312
+ audio: {
1313
+ ".mp3": "audio/mpeg",
1314
+ ".wav": "audio/wav",
1315
+ ".flac": "audio/flac",
1316
+ ".aac": "audio/aac",
1317
+ ".ogg": "audio/ogg",
1318
+ ".m4a": "audio/mp4",
1319
+ ".opus": "audio/opus",
1320
+ },
1321
+ document: {
1322
+ ".pdf": "application/pdf",
1323
+ ".doc": "application/msword",
1324
+ ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
1325
+ ".odt": "application/vnd.oasis.opendocument.text",
1326
+ ".txt": "text/plain",
1327
+ ".rtf": "application/rtf",
1328
+ ".md": "text/markdown",
1329
+ ".markdown": "text/markdown",
1330
+ ".tex": "application/x-tex",
1331
+ ".log": "text/plain",
1332
+ ".json": "application/json",
1333
+ ".xml": "application/xml",
1334
+ ".yaml": "application/x-yaml",
1335
+ ".yml": "application/x-yaml",
1336
+ ".ini": "text/plain",
1337
+ ".cfg": "text/plain",
1338
+ ".toml": "application/toml",
1339
+ ".csv": "text/csv",
1340
+ ".tsv": "text/tab-separated-values",
1341
+ },
1342
+ presentation: {
1343
+ ".ppt": "application/vnd.ms-powerpoint",
1344
+ ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
1345
+ ".odp": "application/vnd.oasis.opendocument.presentation",
1346
+ },
1347
+ spreadsheet: {
1348
+ ".xls": "application/vnd.ms-excel",
1349
+ ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
1350
+ ".ods": "application/vnd.oasis.opendocument.spreadsheet",
1351
+ ".csv": "text/csv",
1352
+ ".tsv": "text/tab-separated-values",
1353
+ },
1354
+ code: {
1355
+ ".py": "text/x-python",
1356
+ ".java": "text/x-java-source",
1357
+ ".c": "text/x-c",
1358
+ ".cpp": "text/x-c++",
1359
+ ".h": "text/x-c",
1360
+ ".hpp": "text/x-c++",
1361
+ ".js": "application/javascript",
1362
+ ".cjs": "application/javascript",
1363
+ ".mjs": "application/javascript",
1364
+ ".jsx": "application/javascript",
1365
+ ".ts": "application/typescript",
1366
+ ".tsx": "application/typescript",
1367
+ ".rb": "text/x-ruby",
1368
+ ".php": "application/x-php",
1369
+ ".go": "text/x-go",
1370
+ ".rs": "text/rust",
1371
+ ".swift": "text/x-swift",
1372
+ ".kt": "text/x-kotlin",
1373
+ ".sh": "application/x-shellscript",
1374
+ ".bash": "application/x-shellscript",
1375
+ ".ps1": "application/x-powershell",
1376
+ ".sql": "application/sql",
1377
+ ".yml": "application/x-yaml",
1378
+ ".coffee": "text/coffeescript",
1379
+ ".lua": "text/x-lua",
1380
+ },
1381
+ archive: {
1382
+ ".zip": "application/zip",
1383
+ ".tar": "application/x-tar",
1384
+ ".gz": "application/gzip",
1385
+ ".tgz": "application/gzip",
1386
+ ".bz2": "application/x-bzip2",
1387
+ ".xz": "application/x-xz",
1388
+ ".rar": "application/vnd.rar",
1389
+ ".7z": "application/x-7z-compressed",
1390
+ ".iso": "application/x-iso9660-image",
1391
+ ".dmg": "application/x-apple-diskimage",
1392
+ ".jar": "application/java-archive",
1393
+ ".war": "application/java-archive",
1394
+ ".whl": "application/python-wheel",
1395
+ ".egg": "application/python-egg",
1396
+ },
1397
+ font: {
1398
+ ".ttf": "font/ttf",
1399
+ ".otf": "font/otf",
1400
+ ".woff": "font/woff",
1401
+ ".woff2": "font/woff2",
1402
+ ".eot": "application/vnd.ms-fontobject",
1403
+ },
1404
+ executable: {
1405
+ ".exe": "application/vnd.microsoft.portable-executable",
1406
+ ".dll": "application/vnd.microsoft.portable-executable",
1407
+ ".bin": "application/octet-stream",
1408
+ ".deb": "application/vnd.debian.binary-package",
1409
+ ".rpm": "application/x-rpm",
1410
+ },
1411
+ };
1412
+ /** Mirror of MEDIA_TYPES in Python: category -> Set of extensions */
1413
+ const MEDIA_TYPES = Object.fromEntries(Object.entries(MIME_TYPES).map(([cat, mapping]) => [
1414
+ cat,
1415
+ new Set(Object.keys(mapping)),
1416
+ ]));
1417
+ /** ---- Helpers ---- */
1418
+ function toCategorySet(categories) {
1419
+ const allCats = new Set(Object.keys(MIME_TYPES));
1420
+ if (!categories) {
1421
+ // all categories
1422
+ return new Set(allCats);
1423
+ }
1424
+ const out = new Set();
1425
+ for (const c of categories) {
1426
+ const key = String(c);
1427
+ if (allCats.has(key))
1428
+ out.add(key);
1429
+ }
1430
+ return out.size ? out : new Set(allCats);
1431
+ }
1432
+ function normalizeCategories(categories, opts) {
1433
+ var _a;
1434
+ const selected = (_a = categories !== null && categories !== void 0 ? categories : opts === null || opts === void 0 ? void 0 : opts.media_types) !== null && _a !== void 0 ? _a : null;
1435
+ return toCategorySet(selected);
1436
+ }
1437
+ function extOf(input) {
1438
+ // Behaves like pathlib.Path(...).suffix.lower(): last extension only; lowercased.
1439
+ let ext = path.extname(input || "");
1440
+ if (!ext && input && input.startsWith(".")) {
1441
+ // user passed ".jpg" directly
1442
+ ext = input;
1443
+ }
1444
+ return (ext || "").toLowerCase();
1445
+ }
1446
+ function unionExts(categories) {
1447
+ const out = new Set();
1448
+ for (const c of categories) {
1449
+ const set = MEDIA_TYPES[c];
1450
+ for (const e of set)
1451
+ out.add(e);
1452
+ }
1453
+ return out;
1454
+ }
1455
+ /** ---- API (Python parity) ---- */
1456
+ /**
1457
+ * Return a sub-map of MEDIA_TYPES for the given categories.
1458
+ * If categories is falsy, returns all categories.
1459
+ */
1460
+ function getMediaMap(categories, opts) {
1461
+ const cats = normalizeCategories(categories, opts);
1462
+ const result = {};
1463
+ for (const c of cats)
1464
+ result[c] = new Set(MEDIA_TYPES[c]);
1465
+ return result;
1466
+ }
1467
+ /**
1468
+ * Return a flat, sorted list of all extensions for the given categories.
1469
+ */
1470
+ function getMediaExts(categories, opts) {
1471
+ const cats = normalizeCategories(categories, opts);
1472
+ return Array.from(unionExts(cats)).sort();
1473
+ }
1474
+ /**
1475
+ * Given a file path or extension, return its media category (e.g. "image") or null.
1476
+ * Mirrors Python's confirm_type.
1477
+ */
1478
+ function confirmType(pathOrExt, categories, opts) {
1479
+ const cats = normalizeCategories(categories, opts);
1480
+ const ext = extOf(pathOrExt);
1481
+ // Preserve object insertion order like Python dict iteration
1482
+ for (const [category, exts] of Object.entries(MEDIA_TYPES)) {
1483
+ if (!cats.has(category))
1484
+ continue;
1485
+ if (ext && exts.has(ext))
1486
+ return category;
1487
+ }
1488
+ return null;
1489
+ }
1490
+ /**
1491
+ * True if the given file path or extension belongs to one of the categories.
1492
+ */
1493
+ function isMediaType(pathOrExt, categories, opts) {
1494
+ return confirmType(pathOrExt, categories, opts) !== null;
1495
+ }
1496
+ /**
1497
+ * Look up the MIME type by extension; fall back to 'application/octet-stream'.
1498
+ */
1499
+ function getMimeType(pathOrExt) {
1500
+ const ext = extOf(pathOrExt);
1501
+ for (const mapping of Object.values(MIME_TYPES)) {
1502
+ if (ext && mapping[ext]) {
1503
+ return mapping[ext];
1504
+ }
1505
+ }
1506
+ return "application/octet-stream";
1507
+ }
1508
+ /**
1509
+ * Recursively collect files under `directory` whose extension is in `categories`.
1510
+ * Synchronous version.
1511
+ */
1512
+ function getAllFileTypesSync(directory, categories, opts) {
1513
+ const base = directory;
1514
+ let stat;
1515
+ try {
1516
+ stat = fs.statSync(base);
1517
+ }
1518
+ catch (_a) {
1519
+ return [];
1520
+ }
1521
+ if (!stat.isDirectory())
1522
+ return [];
1523
+ const cats = normalizeCategories(categories, opts);
1524
+ const wanted = unionExts(cats);
1525
+ const results = [];
1526
+ function walkSync(dir) {
1527
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
1528
+ for (const ent of entries) {
1529
+ const full = path.join(dir, ent.name);
1530
+ if (ent.isDirectory()) {
1531
+ walkSync(full);
1532
+ }
1533
+ else if (ent.isFile()) {
1534
+ const ext = path.extname(ent.name).toLowerCase();
1535
+ if (wanted.has(ext))
1536
+ results.push(full);
1537
+ }
1538
+ }
1539
+ }
1540
+ walkSync(base);
1541
+ return results;
1542
+ }
1543
+ /**
1544
+ * Recursively collect files under `directory` whose extension is in `categories`.
1545
+ * Async/Promise version.
1546
+ */
1547
+ function getAllFileTypes(directory, categories, opts) {
1548
+ return __awaiter(this, void 0, void 0, function* () {
1549
+ let stat;
1550
+ try {
1551
+ stat = yield fsp.stat(directory);
1552
+ }
1553
+ catch (_a) {
1554
+ return [];
1555
+ }
1556
+ if (!stat.isDirectory())
1557
+ return [];
1558
+ const cats = normalizeCategories(categories, opts);
1559
+ const wanted = unionExts(cats);
1560
+ const results = [];
1561
+ function walkAsync(dir) {
1562
+ return __awaiter(this, void 0, void 0, function* () {
1563
+ const entries = yield fsp.readdir(dir, { withFileTypes: true });
1564
+ for (const ent of entries) {
1565
+ const full = path.join(dir, ent.name);
1566
+ if (ent.isDirectory()) {
1567
+ yield walkAsync(full);
1568
+ }
1569
+ else if (ent.isFile()) {
1570
+ const ext = path.extname(ent.name).toLowerCase();
1571
+ if (wanted.has(ext))
1572
+ results.push(full);
1573
+ }
1574
+ }
1575
+ });
1576
+ }
1577
+ yield walkAsync(directory);
1578
+ return results;
1579
+ });
1580
+ }
1581
+ /** Optional convenience re-exports that mirror your Python names */
1582
+ const get_all_file_types = getAllFileTypes;
1583
+ const get_media_map = getMediaMap;
1584
+ const get_media_exts = getMediaExts;
1585
+ const confirm_type = confirmType;
1586
+ const is_media_type = isMediaType;
1587
+ const get_mime_type = getMimeType;
1588
+
1280
1589
  function getSubstring(obj, maxLength = null, minLength = null) {
1281
1590
  const objLength = obj.length;
1282
1591
  const effectiveMaxLength = maxLength !== null && maxLength !== void 0 ? maxLength : objLength; // Use nullish coalescing for clarity
@@ -1511,6 +1820,23 @@ function make_path(...paths) {
1511
1820
  }
1512
1821
  return real_path;
1513
1822
  }
1823
+ function makePath(...paths) {
1824
+ var _a;
1825
+ const pathArray = ensure_list(paths);
1826
+ let real_path = '';
1827
+ for (let i = 0; i < pathArray.length; i++) {
1828
+ let seg = String((_a = pathArray[i]) !== null && _a !== void 0 ? _a : '');
1829
+ if (i === 0) {
1830
+ real_path = seg;
1831
+ }
1832
+ else {
1833
+ seg = eatInner(seg, ['/']);
1834
+ real_path = eatOuter(real_path, ['/']);
1835
+ real_path = `${real_path}/${seg}`;
1836
+ }
1837
+ }
1838
+ return real_path || '';
1839
+ }
1514
1840
  function sanitizeFilename(filename) {
1515
1841
  return filename
1516
1842
  .toLowerCase()
@@ -1540,13 +1866,13 @@ function make_sanitized_path(...paths) {
1540
1866
  }
1541
1867
  return real_path || '';
1542
1868
  }
1869
+ /** FIXED: your regexes were strings. This correctly joins without duplicate slashes. */
1543
1870
  function normalizeUrl(base, p) {
1544
1871
  if (!p)
1545
- return base;
1546
- const cleanBase = base.replace(/\/+$/, ''); // regex literal
1547
- const cleanPath = p.replace(/^\/+/, '');
1548
- // collapse multiple “//” into one, but keep the “://” after protocol
1549
- return `${cleanBase}/${cleanPath}`.replace(/([^:])\/{2,}/g, '$1/');
1872
+ return base.replace(/\/+$/g, '');
1873
+ const cleanBase = base.replace(/\/+$/g, '');
1874
+ const cleanPath = p.replace(/^\/+/g, '');
1875
+ return `${cleanBase}/${cleanPath}`.replace(/([^:])\/{2,}/g, '$1/'); // keep protocol //
1550
1876
  }
1551
1877
 
1552
1878
  /**
@@ -1829,5 +2155,5 @@ const encodeTextForUrl = (text) => encode(text);
1829
2155
  /** Quick helper: decode long blobs coming from share-intents/UTMs */
1830
2156
  const decodeShareBlob = (blob) => decodeMaybeDouble(blob).replace(/\r\n/g, "\n");
1831
2157
 
1832
- export { API_PREFIX, BASE_URL, Button, Checkbox, DEV_PREFIX, DOMAIN_NAME, Input, PROD_PREFIX, PROTOCOL, SUB_DIR, Spinner, alertIt, alertit, assureArray, assureList, assureNumber, assureString, assure_array, assure_list, assure_number, assure_string, buildUrl, callStorage, callWindowMethod, capitalize, capitalize_str, checkResponse, cleanArray, cleanText, create_list_string, currentUsername, currentUsernames, decodeFormComponent, decodeJwt, decodeMaybeDouble, decodeSafe, decodeShareBlob, eatAll, eatEnd, eatInner, eatOuter, encode, encodeTextForUrl, ensureArray, ensureList, ensureNumber, ensureString, ensure_array, ensure_list, ensure_number, ensure_string, fetchIndexHtml, fetchIndexHtmlContainer, fetchIt, formatNumber, geAuthsUtilsDirectory, geBackupsUtilsDirectory, geConstantsUtilsDirectory, geEnvUtilsDirectory, geFetchUtilsDirectory, geFileUtilsDirectory, gePathUtilsDirectory, geStaticDirectory, geStringUtilsDirectory, geTypeUtilsDirectory, get, getAbsDir, getAbsPath, getAlphaNum, getAlphas, getAuthorizationHeader, getBaseDir, getBody, getChar, getCleanArray, getComponentsUtilsDirectory, getConfigContent, getConfigJson, getConfigVar, getDbConfigsPath, getDistDir, getDocumentProp, getEnvDir, getEnvPath, getFetchVars, getFunctionsDir, getFunctionsUtilsDirectory, getHeaders, getHooksUtilsDirectory, getHtmlDirectory, getLibUtilsDirectory, getMethod, getNums, getPublicDir, getResult, getSafeDocument, getSafeLocalStorage, getSafeWindow, getSchemasDirPath, getSchemasPath, getSrcDir, getSubstring, getToken, getWindowHost, getWindowProp, get_basename, get_dirname, get_extname, get_filename, get_full_path, get_full_url, get_key_value, get_keyword_string, get_relative_path, get_result, get_safe_path, get_splitext, get_window, get_window_location, get_window_parts, get_window_pathname, isLoggedIn, isNum, isStrInString, isTokenExpired, isType, make_path, make_sanitized_path, normalizeUrl, parseQuery, parseResult, path_to_url, processKeywords, readJsonFile, removeToken, requireToken, roundPercentage, safeDivide, safeGlobalProp, safeMultiply, safeNums, safeStorage, sanitizeFilename, stripPrefixes, truncateString, tryParse, urlJoin, url_to_path };
2158
+ export { API_PREFIX, BASE_URL, Button, Checkbox, DEV_PREFIX, DOMAIN_NAME, Input, MEDIA_TYPES, MIME_TYPES, PROD_PREFIX, PROTOCOL, SUB_DIR, Spinner, alertIt, alertit, assureArray, assureList, assureNumber, assureString, assure_array, assure_list, assure_number, assure_string, buildUrl, callStorage, callWindowMethod, capitalize, capitalize_str, checkResponse, cleanArray, cleanText, confirmType, confirm_type, create_list_string, currentUsername, currentUsernames, decodeFormComponent, decodeJwt, decodeMaybeDouble, decodeSafe, decodeShareBlob, eatAll, eatEnd, eatInner, eatOuter, encode, encodeTextForUrl, ensureArray, ensureList, ensureNumber, ensureString, ensure_array, ensure_list, ensure_number, ensure_string, fetchIndexHtml, fetchIndexHtmlContainer, fetchIt, formatNumber, geAuthsUtilsDirectory, geBackupsUtilsDirectory, geConstantsUtilsDirectory, geEnvUtilsDirectory, geFetchUtilsDirectory, geFileUtilsDirectory, gePathUtilsDirectory, geStaticDirectory, geStringUtilsDirectory, geTypeUtilsDirectory, get, getAbsDir, getAbsPath, getAllFileTypes, getAllFileTypesSync, getAlphaNum, getAlphas, getAuthorizationHeader, getBaseDir, getBody, getChar, getCleanArray, getComponentsUtilsDirectory, getConfigContent, getConfigJson, getConfigVar, getDbConfigsPath, getDistDir, getDocumentProp, getEnvDir, getEnvPath, getFetchVars, getFunctionsDir, getFunctionsUtilsDirectory, getHeaders, getHooksUtilsDirectory, getHtmlDirectory, getLibUtilsDirectory, getMediaExts, getMediaMap, getMethod, getMimeType, getNums, getPublicDir, getResult, getSafeDocument, getSafeLocalStorage, getSafeWindow, getSchemasDirPath, getSchemasPath, getSrcDir, getSubstring, getToken, getWindowHost, getWindowProp, get_all_file_types, get_basename, get_dirname, get_extname, get_filename, get_full_path, get_full_url, get_key_value, get_keyword_string, get_media_exts, get_media_map, get_mime_type, get_relative_path, get_result, get_safe_path, get_splitext, get_window, get_window_location, get_window_parts, get_window_pathname, isLoggedIn, isMediaType, isNum, isStrInString, isTokenExpired, isType, is_media_type, makePath, make_path, make_sanitized_path, normalizeUrl, parseQuery, parseResult, path_to_url, processKeywords, readJsonFile, removeToken, requireToken, roundPercentage, safeDivide, safeGlobalProp, safeMultiply, safeNums, safeStorage, sanitizeFilename, stripPrefixes, truncateString, tryParse, urlJoin, url_to_path };
1833
2159
  //# sourceMappingURL=index.js.map