@putkoff/abstract-utilities 0.1.241 → 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/cjs/index.js CHANGED
@@ -2,9 +2,33 @@
2
2
 
3
3
  var react = require('react');
4
4
  var path = require('path');
5
+ var fs = require('node:fs');
6
+ var fsp = require('node:fs/promises');
7
+ var path$1 = require('node:path');
5
8
  var jsxRuntime = require('react/jsx-runtime');
6
9
 
7
10
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
11
+ function _interopNamespaceDefault(e) {
12
+ var n = Object.create(null);
13
+ if (e) {
14
+ Object.keys(e).forEach(function (k) {
15
+ if (k !== 'default') {
16
+ var d = Object.getOwnPropertyDescriptor(e, k);
17
+ Object.defineProperty(n, k, d.get ? d : {
18
+ enumerable: true,
19
+ get: function () { return e[k]; }
20
+ });
21
+ }
22
+ });
23
+ }
24
+ n.default = e;
25
+ return Object.freeze(n);
26
+ }
27
+
28
+ var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
29
+ var fsp__namespace = /*#__PURE__*/_interopNamespaceDefault(fsp);
30
+ var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path$1);
31
+
8
32
  function getSafeDocument() {
9
33
  return typeof document !== 'undefined' ? document : undefined;
10
34
  }
@@ -1279,6 +1303,313 @@ function getAlphas() {
1279
1303
  return 'abcdefghijklmnopqrstuvwxyz';
1280
1304
  }
1281
1305
 
1306
+ /** ---- Data: large but explicit, mirrors your Python mapping ---- */
1307
+ const MIME_TYPES = {
1308
+ image: {
1309
+ ".jpg": "image/jpeg",
1310
+ ".jpeg": "image/jpeg",
1311
+ ".png": "image/png",
1312
+ ".gif": "image/gif",
1313
+ ".bmp": "image/bmp",
1314
+ ".tiff": "image/tiff",
1315
+ ".webp": "image/webp",
1316
+ ".svg": "image/svg+xml",
1317
+ ".ico": "image/vnd.microsoft.icon",
1318
+ ".heic": "image/heic",
1319
+ ".psd": "image/vnd.adobe.photoshop",
1320
+ ".raw": "image/x-raw",
1321
+ },
1322
+ video: {
1323
+ ".mp4": "video/mp4",
1324
+ ".webm": "video/webm",
1325
+ ".ogg": "video/ogg",
1326
+ ".mov": "video/quicktime",
1327
+ ".avi": "video/x-msvideo",
1328
+ ".mkv": "video/x-matroska",
1329
+ ".flv": "video/x-flv",
1330
+ ".wmv": "video/x-ms-wmv",
1331
+ ".3gp": "video/3gpp",
1332
+ ".ts": "video/mp2t",
1333
+ ".mpeg": "video/mpeg",
1334
+ ".mpg": "video/mpg",
1335
+ },
1336
+ audio: {
1337
+ ".mp3": "audio/mpeg",
1338
+ ".wav": "audio/wav",
1339
+ ".flac": "audio/flac",
1340
+ ".aac": "audio/aac",
1341
+ ".ogg": "audio/ogg",
1342
+ ".m4a": "audio/mp4",
1343
+ ".opus": "audio/opus",
1344
+ },
1345
+ document: {
1346
+ ".pdf": "application/pdf",
1347
+ ".doc": "application/msword",
1348
+ ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
1349
+ ".odt": "application/vnd.oasis.opendocument.text",
1350
+ ".txt": "text/plain",
1351
+ ".rtf": "application/rtf",
1352
+ ".md": "text/markdown",
1353
+ ".markdown": "text/markdown",
1354
+ ".tex": "application/x-tex",
1355
+ ".log": "text/plain",
1356
+ ".json": "application/json",
1357
+ ".xml": "application/xml",
1358
+ ".yaml": "application/x-yaml",
1359
+ ".yml": "application/x-yaml",
1360
+ ".ini": "text/plain",
1361
+ ".cfg": "text/plain",
1362
+ ".toml": "application/toml",
1363
+ ".csv": "text/csv",
1364
+ ".tsv": "text/tab-separated-values",
1365
+ },
1366
+ presentation: {
1367
+ ".ppt": "application/vnd.ms-powerpoint",
1368
+ ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
1369
+ ".odp": "application/vnd.oasis.opendocument.presentation",
1370
+ },
1371
+ spreadsheet: {
1372
+ ".xls": "application/vnd.ms-excel",
1373
+ ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
1374
+ ".ods": "application/vnd.oasis.opendocument.spreadsheet",
1375
+ ".csv": "text/csv",
1376
+ ".tsv": "text/tab-separated-values",
1377
+ },
1378
+ code: {
1379
+ ".py": "text/x-python",
1380
+ ".java": "text/x-java-source",
1381
+ ".c": "text/x-c",
1382
+ ".cpp": "text/x-c++",
1383
+ ".h": "text/x-c",
1384
+ ".hpp": "text/x-c++",
1385
+ ".js": "application/javascript",
1386
+ ".cjs": "application/javascript",
1387
+ ".mjs": "application/javascript",
1388
+ ".jsx": "application/javascript",
1389
+ ".ts": "application/typescript",
1390
+ ".tsx": "application/typescript",
1391
+ ".rb": "text/x-ruby",
1392
+ ".php": "application/x-php",
1393
+ ".go": "text/x-go",
1394
+ ".rs": "text/rust",
1395
+ ".swift": "text/x-swift",
1396
+ ".kt": "text/x-kotlin",
1397
+ ".sh": "application/x-shellscript",
1398
+ ".bash": "application/x-shellscript",
1399
+ ".ps1": "application/x-powershell",
1400
+ ".sql": "application/sql",
1401
+ ".yml": "application/x-yaml",
1402
+ ".coffee": "text/coffeescript",
1403
+ ".lua": "text/x-lua",
1404
+ },
1405
+ archive: {
1406
+ ".zip": "application/zip",
1407
+ ".tar": "application/x-tar",
1408
+ ".gz": "application/gzip",
1409
+ ".tgz": "application/gzip",
1410
+ ".bz2": "application/x-bzip2",
1411
+ ".xz": "application/x-xz",
1412
+ ".rar": "application/vnd.rar",
1413
+ ".7z": "application/x-7z-compressed",
1414
+ ".iso": "application/x-iso9660-image",
1415
+ ".dmg": "application/x-apple-diskimage",
1416
+ ".jar": "application/java-archive",
1417
+ ".war": "application/java-archive",
1418
+ ".whl": "application/python-wheel",
1419
+ ".egg": "application/python-egg",
1420
+ },
1421
+ font: {
1422
+ ".ttf": "font/ttf",
1423
+ ".otf": "font/otf",
1424
+ ".woff": "font/woff",
1425
+ ".woff2": "font/woff2",
1426
+ ".eot": "application/vnd.ms-fontobject",
1427
+ },
1428
+ executable: {
1429
+ ".exe": "application/vnd.microsoft.portable-executable",
1430
+ ".dll": "application/vnd.microsoft.portable-executable",
1431
+ ".bin": "application/octet-stream",
1432
+ ".deb": "application/vnd.debian.binary-package",
1433
+ ".rpm": "application/x-rpm",
1434
+ },
1435
+ };
1436
+ /** Mirror of MEDIA_TYPES in Python: category -> Set of extensions */
1437
+ const MEDIA_TYPES = Object.fromEntries(Object.entries(MIME_TYPES).map(([cat, mapping]) => [
1438
+ cat,
1439
+ new Set(Object.keys(mapping)),
1440
+ ]));
1441
+ /** ---- Helpers ---- */
1442
+ function toCategorySet(categories) {
1443
+ const allCats = new Set(Object.keys(MIME_TYPES));
1444
+ if (!categories) {
1445
+ // all categories
1446
+ return new Set(allCats);
1447
+ }
1448
+ const out = new Set();
1449
+ for (const c of categories) {
1450
+ const key = String(c);
1451
+ if (allCats.has(key))
1452
+ out.add(key);
1453
+ }
1454
+ return out.size ? out : new Set(allCats);
1455
+ }
1456
+ function normalizeCategories(categories, opts) {
1457
+ var _a;
1458
+ 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;
1459
+ return toCategorySet(selected);
1460
+ }
1461
+ function extOf(input) {
1462
+ // Behaves like pathlib.Path(...).suffix.lower(): last extension only; lowercased.
1463
+ let ext = path__namespace.extname(input || "");
1464
+ if (!ext && input && input.startsWith(".")) {
1465
+ // user passed ".jpg" directly
1466
+ ext = input;
1467
+ }
1468
+ return (ext || "").toLowerCase();
1469
+ }
1470
+ function unionExts(categories) {
1471
+ const out = new Set();
1472
+ for (const c of categories) {
1473
+ const set = MEDIA_TYPES[c];
1474
+ for (const e of set)
1475
+ out.add(e);
1476
+ }
1477
+ return out;
1478
+ }
1479
+ /** ---- API (Python parity) ---- */
1480
+ /**
1481
+ * Return a sub-map of MEDIA_TYPES for the given categories.
1482
+ * If categories is falsy, returns all categories.
1483
+ */
1484
+ function getMediaMap(categories, opts) {
1485
+ const cats = normalizeCategories(categories, opts);
1486
+ const result = {};
1487
+ for (const c of cats)
1488
+ result[c] = new Set(MEDIA_TYPES[c]);
1489
+ return result;
1490
+ }
1491
+ /**
1492
+ * Return a flat, sorted list of all extensions for the given categories.
1493
+ */
1494
+ function getMediaExts(categories, opts) {
1495
+ const cats = normalizeCategories(categories, opts);
1496
+ return Array.from(unionExts(cats)).sort();
1497
+ }
1498
+ /**
1499
+ * Given a file path or extension, return its media category (e.g. "image") or null.
1500
+ * Mirrors Python's confirm_type.
1501
+ */
1502
+ function confirmType(pathOrExt, categories, opts) {
1503
+ const cats = normalizeCategories(categories, opts);
1504
+ const ext = extOf(pathOrExt);
1505
+ // Preserve object insertion order like Python dict iteration
1506
+ for (const [category, exts] of Object.entries(MEDIA_TYPES)) {
1507
+ if (!cats.has(category))
1508
+ continue;
1509
+ if (ext && exts.has(ext))
1510
+ return category;
1511
+ }
1512
+ return null;
1513
+ }
1514
+ /**
1515
+ * True if the given file path or extension belongs to one of the categories.
1516
+ */
1517
+ function isMediaType(pathOrExt, categories, opts) {
1518
+ return confirmType(pathOrExt, categories, opts) !== null;
1519
+ }
1520
+ /**
1521
+ * Look up the MIME type by extension; fall back to 'application/octet-stream'.
1522
+ */
1523
+ function getMimeType(pathOrExt) {
1524
+ const ext = extOf(pathOrExt);
1525
+ for (const mapping of Object.values(MIME_TYPES)) {
1526
+ if (ext && mapping[ext]) {
1527
+ return mapping[ext];
1528
+ }
1529
+ }
1530
+ return "application/octet-stream";
1531
+ }
1532
+ /**
1533
+ * Recursively collect files under `directory` whose extension is in `categories`.
1534
+ * Synchronous version.
1535
+ */
1536
+ function getAllFileTypesSync(directory, categories, opts) {
1537
+ const base = directory;
1538
+ let stat;
1539
+ try {
1540
+ stat = fs__namespace.statSync(base);
1541
+ }
1542
+ catch (_a) {
1543
+ return [];
1544
+ }
1545
+ if (!stat.isDirectory())
1546
+ return [];
1547
+ const cats = normalizeCategories(categories, opts);
1548
+ const wanted = unionExts(cats);
1549
+ const results = [];
1550
+ function walkSync(dir) {
1551
+ const entries = fs__namespace.readdirSync(dir, { withFileTypes: true });
1552
+ for (const ent of entries) {
1553
+ const full = path__namespace.join(dir, ent.name);
1554
+ if (ent.isDirectory()) {
1555
+ walkSync(full);
1556
+ }
1557
+ else if (ent.isFile()) {
1558
+ const ext = path__namespace.extname(ent.name).toLowerCase();
1559
+ if (wanted.has(ext))
1560
+ results.push(full);
1561
+ }
1562
+ }
1563
+ }
1564
+ walkSync(base);
1565
+ return results;
1566
+ }
1567
+ /**
1568
+ * Recursively collect files under `directory` whose extension is in `categories`.
1569
+ * Async/Promise version.
1570
+ */
1571
+ function getAllFileTypes(directory, categories, opts) {
1572
+ return __awaiter(this, void 0, void 0, function* () {
1573
+ let stat;
1574
+ try {
1575
+ stat = yield fsp__namespace.stat(directory);
1576
+ }
1577
+ catch (_a) {
1578
+ return [];
1579
+ }
1580
+ if (!stat.isDirectory())
1581
+ return [];
1582
+ const cats = normalizeCategories(categories, opts);
1583
+ const wanted = unionExts(cats);
1584
+ const results = [];
1585
+ function walkAsync(dir) {
1586
+ return __awaiter(this, void 0, void 0, function* () {
1587
+ const entries = yield fsp__namespace.readdir(dir, { withFileTypes: true });
1588
+ for (const ent of entries) {
1589
+ const full = path__namespace.join(dir, ent.name);
1590
+ if (ent.isDirectory()) {
1591
+ yield walkAsync(full);
1592
+ }
1593
+ else if (ent.isFile()) {
1594
+ const ext = path__namespace.extname(ent.name).toLowerCase();
1595
+ if (wanted.has(ext))
1596
+ results.push(full);
1597
+ }
1598
+ }
1599
+ });
1600
+ }
1601
+ yield walkAsync(directory);
1602
+ return results;
1603
+ });
1604
+ }
1605
+ /** Optional convenience re-exports that mirror your Python names */
1606
+ const get_all_file_types = getAllFileTypes;
1607
+ const get_media_map = getMediaMap;
1608
+ const get_media_exts = getMediaExts;
1609
+ const confirm_type = confirmType;
1610
+ const is_media_type = isMediaType;
1611
+ const get_mime_type = getMimeType;
1612
+
1282
1613
  function getSubstring(obj, maxLength = null, minLength = null) {
1283
1614
  const objLength = obj.length;
1284
1615
  const effectiveMaxLength = maxLength !== null && maxLength !== void 0 ? maxLength : objLength; // Use nullish coalescing for clarity
@@ -1871,6 +2202,8 @@ exports.Checkbox = Checkbox;
1871
2202
  exports.DEV_PREFIX = DEV_PREFIX;
1872
2203
  exports.DOMAIN_NAME = DOMAIN_NAME;
1873
2204
  exports.Input = Input;
2205
+ exports.MEDIA_TYPES = MEDIA_TYPES;
2206
+ exports.MIME_TYPES = MIME_TYPES;
1874
2207
  exports.PROD_PREFIX = PROD_PREFIX;
1875
2208
  exports.PROTOCOL = PROTOCOL;
1876
2209
  exports.SUB_DIR = SUB_DIR;
@@ -1893,6 +2226,8 @@ exports.capitalize_str = capitalize_str;
1893
2226
  exports.checkResponse = checkResponse;
1894
2227
  exports.cleanArray = cleanArray;
1895
2228
  exports.cleanText = cleanText;
2229
+ exports.confirmType = confirmType;
2230
+ exports.confirm_type = confirm_type;
1896
2231
  exports.create_list_string = create_list_string;
1897
2232
  exports.currentUsername = currentUsername;
1898
2233
  exports.currentUsernames = currentUsernames;
@@ -1932,6 +2267,8 @@ exports.geTypeUtilsDirectory = geTypeUtilsDirectory;
1932
2267
  exports.get = get;
1933
2268
  exports.getAbsDir = getAbsDir;
1934
2269
  exports.getAbsPath = getAbsPath;
2270
+ exports.getAllFileTypes = getAllFileTypes;
2271
+ exports.getAllFileTypesSync = getAllFileTypesSync;
1935
2272
  exports.getAlphaNum = getAlphaNum;
1936
2273
  exports.getAlphas = getAlphas;
1937
2274
  exports.getAuthorizationHeader = getAuthorizationHeader;
@@ -1955,7 +2292,10 @@ exports.getHeaders = getHeaders;
1955
2292
  exports.getHooksUtilsDirectory = getHooksUtilsDirectory;
1956
2293
  exports.getHtmlDirectory = getHtmlDirectory;
1957
2294
  exports.getLibUtilsDirectory = getLibUtilsDirectory;
2295
+ exports.getMediaExts = getMediaExts;
2296
+ exports.getMediaMap = getMediaMap;
1958
2297
  exports.getMethod = getMethod;
2298
+ exports.getMimeType = getMimeType;
1959
2299
  exports.getNums = getNums;
1960
2300
  exports.getPublicDir = getPublicDir;
1961
2301
  exports.getResult = getResult;
@@ -1969,6 +2309,7 @@ exports.getSubstring = getSubstring;
1969
2309
  exports.getToken = getToken;
1970
2310
  exports.getWindowHost = getWindowHost;
1971
2311
  exports.getWindowProp = getWindowProp;
2312
+ exports.get_all_file_types = get_all_file_types;
1972
2313
  exports.get_basename = get_basename;
1973
2314
  exports.get_dirname = get_dirname;
1974
2315
  exports.get_extname = get_extname;
@@ -1977,6 +2318,9 @@ exports.get_full_path = get_full_path;
1977
2318
  exports.get_full_url = get_full_url;
1978
2319
  exports.get_key_value = get_key_value;
1979
2320
  exports.get_keyword_string = get_keyword_string;
2321
+ exports.get_media_exts = get_media_exts;
2322
+ exports.get_media_map = get_media_map;
2323
+ exports.get_mime_type = get_mime_type;
1980
2324
  exports.get_relative_path = get_relative_path;
1981
2325
  exports.get_result = get_result;
1982
2326
  exports.get_safe_path = get_safe_path;
@@ -1986,10 +2330,12 @@ exports.get_window_location = get_window_location;
1986
2330
  exports.get_window_parts = get_window_parts;
1987
2331
  exports.get_window_pathname = get_window_pathname;
1988
2332
  exports.isLoggedIn = isLoggedIn;
2333
+ exports.isMediaType = isMediaType;
1989
2334
  exports.isNum = isNum;
1990
2335
  exports.isStrInString = isStrInString;
1991
2336
  exports.isTokenExpired = isTokenExpired;
1992
2337
  exports.isType = isType;
2338
+ exports.is_media_type = is_media_type;
1993
2339
  exports.makePath = makePath;
1994
2340
  exports.make_path = make_path;
1995
2341
  exports.make_sanitized_path = make_sanitized_path;