@putkoff/abstract-utilities 0.1.235 → 0.1.238

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
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var react = require('react');
4
+ var path = require('path');
4
5
  var jsxRuntime = require('react/jsx-runtime');
5
6
 
6
7
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
@@ -561,6 +562,89 @@ function fetchIndexHtmlContainer(filename_1) {
561
562
  });
562
563
  }
563
564
 
565
+ function urlJoin(...parts) {
566
+ var _a;
567
+ const s = (parts.length === 1 && Array.isArray(parts[0]) ? parts[0] : parts);
568
+ let r = "";
569
+ for (let i = 0; i < s.length; i++) {
570
+ let d = ((_a = s[i]) !== null && _a !== void 0 ? _a : "").toString();
571
+ if (!d)
572
+ continue;
573
+ if (i === 0)
574
+ r = d;
575
+ else {
576
+ d = d.replace(/^\/+/, "");
577
+ r = r.replace(/\/+$/, "");
578
+ r = `${r}/${d}`;
579
+ }
580
+ }
581
+ return r;
582
+ }
583
+ /**
584
+ * Returns a full URL.
585
+ * If partial_url is already absolute (starts with http), it is returned as is.
586
+ * Otherwise, it is combined with the base URL.
587
+ */
588
+ function get_full_url(partial_url, domain = null) {
589
+ if (typeof partial_url !== 'string') {
590
+ throw new Error('partial_url must be a string');
591
+ }
592
+ // If it already starts with http, assume it is absolute.
593
+ if (partial_url.startsWith('http')) {
594
+ return partial_url;
595
+ }
596
+ return urlJoin(domain, partial_url);
597
+ }
598
+ /**
599
+ * Returns a full file system path.
600
+ * If partial_path is already absolute, it is returned as is.
601
+ * Otherwise, it is joined with the base directory.
602
+ */
603
+ function get_full_path(partial_path, parent_dir = null) {
604
+ if (typeof partial_path !== 'string') {
605
+ throw new Error('partial_path must be a string');
606
+ }
607
+ if (path.isAbsolute(partial_path)) {
608
+ return partial_path;
609
+ }
610
+ return urlJoin(parent_dir, partial_path);
611
+ }
612
+ /**
613
+ * Converts a local file system path into its corresponding URL.
614
+ * It checks against the known directories in all_paths and replaces the matching base.
615
+ */
616
+ function path_to_url(filePath, all_paths) {
617
+ if (typeof filePath !== 'string') {
618
+ throw new Error('filePath must be a string');
619
+ }
620
+ for (const key in all_paths) {
621
+ const mapping = all_paths[key];
622
+ const normalizedBase = path.normalize(mapping.path);
623
+ if (filePath.startsWith(normalizedBase)) {
624
+ const relativePath = filePath.substring(normalizedBase.length);
625
+ return urlJoin(mapping.url, relativePath.replace(/\\/g, '/'));
626
+ }
627
+ }
628
+ return null;
629
+ }
630
+ /**
631
+ * Converts a URL into its corresponding local file system path.
632
+ * It checks against the known URL prefixes in all_paths and replaces the matching base.
633
+ */
634
+ function url_to_path(urlStr, all_paths) {
635
+ if (typeof urlStr !== 'string') {
636
+ throw new Error('urlStr must be a string');
637
+ }
638
+ for (const key in all_paths) {
639
+ const mapping = all_paths[key];
640
+ if (urlStr.startsWith(mapping.url)) {
641
+ const relativeUrl = urlStr.substring(mapping.url.length);
642
+ return urlJoin(mapping.path, relativeUrl);
643
+ }
644
+ }
645
+ return null;
646
+ }
647
+
564
648
  function assertPath(path) {
565
649
  if (typeof path !== 'string') {
566
650
  throw new TypeError('Path must be a string. Received ' + JSON.stringify(path));
@@ -1678,6 +1762,76 @@ function roundPercentage(x) {
1678
1762
  return safeDivide(Math.round(pct), 100);
1679
1763
  }
1680
1764
 
1765
+ // urlTools.ts
1766
+ // Minimal, safe encoders/decoders + helpers to build/parse URLs
1767
+ /** Encode a single query value/key safely */
1768
+ const encode = (v) => encodeURIComponent(String(v !== null && v !== void 0 ? v : ""));
1769
+ /** Decode a single query value/key safely (never throws) */
1770
+ const decodeSafe = (v) => {
1771
+ if (v == null)
1772
+ return "";
1773
+ try {
1774
+ return decodeURIComponent(v);
1775
+ }
1776
+ catch (_a) {
1777
+ // handles bad % sequences or already-decoded strings
1778
+ return v;
1779
+ }
1780
+ };
1781
+ /** Decode strings that might be double-encoded (up to 2 passes) */
1782
+ const decodeMaybeDouble = (v) => {
1783
+ const once = decodeSafe(v);
1784
+ const twice = decodeSafe(once);
1785
+ return twice.length < once.length ? twice : once;
1786
+ };
1787
+ /** Convert + to spaces (legacy www-form behavior) then decode */
1788
+ const decodeFormComponent = (v) => decodeSafe(v.replace(/\+/g, " "));
1789
+ /** Build a URL with query params (skips null/undefined) */
1790
+ const buildUrl = (base, params) => {
1791
+ const u = new URL(base, typeof window !== "undefined" ? window.location.origin : "http://localhost");
1792
+ if (params) {
1793
+ for (const [k, val] of Object.entries(params)) {
1794
+ if (val === null || val === undefined)
1795
+ continue;
1796
+ // arrays -> multiple entries
1797
+ if (Array.isArray(val)) {
1798
+ val.forEach(v => u.searchParams.append(k, String(v)));
1799
+ }
1800
+ else {
1801
+ u.searchParams.set(k, String(val));
1802
+ }
1803
+ }
1804
+ }
1805
+ return u.toString();
1806
+ };
1807
+ /** Parse a query string into an object (first value wins; arrays if repeat=true) */
1808
+ const parseQuery = (qs, opts) => {
1809
+ var _a;
1810
+ const { repeat = false, form = false } = opts || {};
1811
+ const out = {};
1812
+ const s = qs.startsWith("?") ? qs.slice(1) : qs;
1813
+ if (!s)
1814
+ return out;
1815
+ for (const part of s.split("&")) {
1816
+ if (!part)
1817
+ continue;
1818
+ const [kRaw, vRaw = ""] = part.split("=");
1819
+ const K = form ? decodeFormComponent(kRaw) : decodeSafe(kRaw);
1820
+ const V = form ? decodeFormComponent(vRaw) : decodeSafe(vRaw);
1821
+ if (repeat) {
1822
+ ((_a = out[K]) !== null && _a !== void 0 ? _a : (out[K] = [])).push(V);
1823
+ }
1824
+ else {
1825
+ out[K] = V;
1826
+ }
1827
+ }
1828
+ return out;
1829
+ };
1830
+ /** Quick helper: percent-encode whole strings for placement in URLs */
1831
+ const encodeTextForUrl = (text) => encode(text);
1832
+ /** Quick helper: decode long blobs coming from share-intents/UTMs */
1833
+ const decodeShareBlob = (blob) => decodeMaybeDouble(blob).replace(/\r\n/g, "\n");
1834
+
1681
1835
  Object.defineProperty(exports, "useCallback", {
1682
1836
  enumerable: true,
1683
1837
  get: function () { return react.useCallback; }
@@ -1715,6 +1869,7 @@ exports.assure_array = assure_array;
1715
1869
  exports.assure_list = assure_list;
1716
1870
  exports.assure_number = assure_number;
1717
1871
  exports.assure_string = assure_string;
1872
+ exports.buildUrl = buildUrl;
1718
1873
  exports.callStorage = callStorage;
1719
1874
  exports.callWindowMethod = callWindowMethod;
1720
1875
  exports.capitalize = capitalize;
@@ -1725,11 +1880,17 @@ exports.cleanText = cleanText;
1725
1880
  exports.create_list_string = create_list_string;
1726
1881
  exports.currentUsername = currentUsername;
1727
1882
  exports.currentUsernames = currentUsernames;
1883
+ exports.decodeFormComponent = decodeFormComponent;
1728
1884
  exports.decodeJwt = decodeJwt;
1885
+ exports.decodeMaybeDouble = decodeMaybeDouble;
1886
+ exports.decodeSafe = decodeSafe;
1887
+ exports.decodeShareBlob = decodeShareBlob;
1729
1888
  exports.eatAll = eatAll;
1730
1889
  exports.eatEnd = eatEnd;
1731
1890
  exports.eatInner = eatInner;
1732
1891
  exports.eatOuter = eatOuter;
1892
+ exports.encode = encode;
1893
+ exports.encodeTextForUrl = encodeTextForUrl;
1733
1894
  exports.ensureArray = ensureArray;
1734
1895
  exports.ensureList = ensureList;
1735
1896
  exports.ensureNumber = ensureNumber;
@@ -1796,6 +1957,8 @@ exports.get_basename = get_basename;
1796
1957
  exports.get_dirname = get_dirname;
1797
1958
  exports.get_extname = get_extname;
1798
1959
  exports.get_filename = get_filename;
1960
+ exports.get_full_path = get_full_path;
1961
+ exports.get_full_url = get_full_url;
1799
1962
  exports.get_key_value = get_key_value;
1800
1963
  exports.get_keyword_string = get_keyword_string;
1801
1964
  exports.get_relative_path = get_relative_path;
@@ -1814,7 +1977,9 @@ exports.isType = isType;
1814
1977
  exports.make_path = make_path;
1815
1978
  exports.make_sanitized_path = make_sanitized_path;
1816
1979
  exports.normalizeUrl = normalizeUrl;
1980
+ exports.parseQuery = parseQuery;
1817
1981
  exports.parseResult = parseResult;
1982
+ exports.path_to_url = path_to_url;
1818
1983
  exports.processKeywords = processKeywords;
1819
1984
  exports.readJsonFile = readJsonFile;
1820
1985
  exports.removeToken = removeToken;
@@ -1829,4 +1994,6 @@ exports.sanitizeFilename = sanitizeFilename;
1829
1994
  exports.stripPrefixes = stripPrefixes;
1830
1995
  exports.truncateString = truncateString;
1831
1996
  exports.tryParse = tryParse;
1997
+ exports.urlJoin = urlJoin;
1998
+ exports.url_to_path = url_to_path;
1832
1999
  //# sourceMappingURL=index.js.map