@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/esm/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { useCallback, useEffect, useRef, useState } from 'react';
2
+ import path from 'path';
2
3
  import { jsx, jsxs } from 'react/jsx-runtime';
3
4
 
4
5
  function getSafeDocument() {
@@ -558,6 +559,89 @@ function fetchIndexHtmlContainer(filename_1) {
558
559
  });
559
560
  }
560
561
 
562
+ function urlJoin(...parts) {
563
+ var _a;
564
+ const s = (parts.length === 1 && Array.isArray(parts[0]) ? parts[0] : parts);
565
+ let r = "";
566
+ for (let i = 0; i < s.length; i++) {
567
+ let d = ((_a = s[i]) !== null && _a !== void 0 ? _a : "").toString();
568
+ if (!d)
569
+ continue;
570
+ if (i === 0)
571
+ r = d;
572
+ else {
573
+ d = d.replace(/^\/+/, "");
574
+ r = r.replace(/\/+$/, "");
575
+ r = `${r}/${d}`;
576
+ }
577
+ }
578
+ return r;
579
+ }
580
+ /**
581
+ * Returns a full URL.
582
+ * If partial_url is already absolute (starts with http), it is returned as is.
583
+ * Otherwise, it is combined with the base URL.
584
+ */
585
+ function get_full_url(partial_url, domain = null) {
586
+ if (typeof partial_url !== 'string') {
587
+ throw new Error('partial_url must be a string');
588
+ }
589
+ // If it already starts with http, assume it is absolute.
590
+ if (partial_url.startsWith('http')) {
591
+ return partial_url;
592
+ }
593
+ return urlJoin(domain, partial_url);
594
+ }
595
+ /**
596
+ * Returns a full file system path.
597
+ * If partial_path is already absolute, it is returned as is.
598
+ * Otherwise, it is joined with the base directory.
599
+ */
600
+ function get_full_path(partial_path, parent_dir = null) {
601
+ if (typeof partial_path !== 'string') {
602
+ throw new Error('partial_path must be a string');
603
+ }
604
+ if (path.isAbsolute(partial_path)) {
605
+ return partial_path;
606
+ }
607
+ return urlJoin(parent_dir, partial_path);
608
+ }
609
+ /**
610
+ * Converts a local file system path into its corresponding URL.
611
+ * It checks against the known directories in all_paths and replaces the matching base.
612
+ */
613
+ function path_to_url(filePath, all_paths) {
614
+ if (typeof filePath !== 'string') {
615
+ throw new Error('filePath must be a string');
616
+ }
617
+ for (const key in all_paths) {
618
+ const mapping = all_paths[key];
619
+ const normalizedBase = path.normalize(mapping.path);
620
+ if (filePath.startsWith(normalizedBase)) {
621
+ const relativePath = filePath.substring(normalizedBase.length);
622
+ return urlJoin(mapping.url, relativePath.replace(/\\/g, '/'));
623
+ }
624
+ }
625
+ return null;
626
+ }
627
+ /**
628
+ * Converts a URL into its corresponding local file system path.
629
+ * It checks against the known URL prefixes in all_paths and replaces the matching base.
630
+ */
631
+ function url_to_path(urlStr, all_paths) {
632
+ if (typeof urlStr !== 'string') {
633
+ throw new Error('urlStr must be a string');
634
+ }
635
+ for (const key in all_paths) {
636
+ const mapping = all_paths[key];
637
+ if (urlStr.startsWith(mapping.url)) {
638
+ const relativeUrl = urlStr.substring(mapping.url.length);
639
+ return urlJoin(mapping.path, relativeUrl);
640
+ }
641
+ }
642
+ return null;
643
+ }
644
+
561
645
  function assertPath(path) {
562
646
  if (typeof path !== 'string') {
563
647
  throw new TypeError('Path must be a string. Received ' + JSON.stringify(path));
@@ -1675,5 +1759,75 @@ function roundPercentage(x) {
1675
1759
  return safeDivide(Math.round(pct), 100);
1676
1760
  }
1677
1761
 
1678
- 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, callStorage, callWindowMethod, capitalize, capitalize_str, checkResponse, cleanArray, cleanText, create_list_string, currentUsername, currentUsernames, decodeJwt, eatAll, eatEnd, eatInner, eatOuter, 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_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, parseResult, processKeywords, readJsonFile, removeToken, requireToken, roundPercentage, safeDivide, safeGlobalProp, safeMultiply, safeNums, safeStorage, sanitizeFilename, stripPrefixes, truncateString, tryParse };
1762
+ // urlTools.ts
1763
+ // Minimal, safe encoders/decoders + helpers to build/parse URLs
1764
+ /** Encode a single query value/key safely */
1765
+ const encode = (v) => encodeURIComponent(String(v !== null && v !== void 0 ? v : ""));
1766
+ /** Decode a single query value/key safely (never throws) */
1767
+ const decodeSafe = (v) => {
1768
+ if (v == null)
1769
+ return "";
1770
+ try {
1771
+ return decodeURIComponent(v);
1772
+ }
1773
+ catch (_a) {
1774
+ // handles bad % sequences or already-decoded strings
1775
+ return v;
1776
+ }
1777
+ };
1778
+ /** Decode strings that might be double-encoded (up to 2 passes) */
1779
+ const decodeMaybeDouble = (v) => {
1780
+ const once = decodeSafe(v);
1781
+ const twice = decodeSafe(once);
1782
+ return twice.length < once.length ? twice : once;
1783
+ };
1784
+ /** Convert + to spaces (legacy www-form behavior) then decode */
1785
+ const decodeFormComponent = (v) => decodeSafe(v.replace(/\+/g, " "));
1786
+ /** Build a URL with query params (skips null/undefined) */
1787
+ const buildUrl = (base, params) => {
1788
+ const u = new URL(base, typeof window !== "undefined" ? window.location.origin : "http://localhost");
1789
+ if (params) {
1790
+ for (const [k, val] of Object.entries(params)) {
1791
+ if (val === null || val === undefined)
1792
+ continue;
1793
+ // arrays -> multiple entries
1794
+ if (Array.isArray(val)) {
1795
+ val.forEach(v => u.searchParams.append(k, String(v)));
1796
+ }
1797
+ else {
1798
+ u.searchParams.set(k, String(val));
1799
+ }
1800
+ }
1801
+ }
1802
+ return u.toString();
1803
+ };
1804
+ /** Parse a query string into an object (first value wins; arrays if repeat=true) */
1805
+ const parseQuery = (qs, opts) => {
1806
+ var _a;
1807
+ const { repeat = false, form = false } = opts || {};
1808
+ const out = {};
1809
+ const s = qs.startsWith("?") ? qs.slice(1) : qs;
1810
+ if (!s)
1811
+ return out;
1812
+ for (const part of s.split("&")) {
1813
+ if (!part)
1814
+ continue;
1815
+ const [kRaw, vRaw = ""] = part.split("=");
1816
+ const K = form ? decodeFormComponent(kRaw) : decodeSafe(kRaw);
1817
+ const V = form ? decodeFormComponent(vRaw) : decodeSafe(vRaw);
1818
+ if (repeat) {
1819
+ ((_a = out[K]) !== null && _a !== void 0 ? _a : (out[K] = [])).push(V);
1820
+ }
1821
+ else {
1822
+ out[K] = V;
1823
+ }
1824
+ }
1825
+ return out;
1826
+ };
1827
+ /** Quick helper: percent-encode whole strings for placement in URLs */
1828
+ const encodeTextForUrl = (text) => encode(text);
1829
+ /** Quick helper: decode long blobs coming from share-intents/UTMs */
1830
+ const decodeShareBlob = (blob) => decodeMaybeDouble(blob).replace(/\r\n/g, "\n");
1831
+
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 };
1679
1833
  //# sourceMappingURL=index.js.map