@putkoff/abstract-utilities 0.1.243 → 0.1.244

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.
Files changed (95) hide show
  1. package/dist/cjs/index.js +511 -11
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/esm/index.js +514 -13
  4. package/dist/esm/index.js.map +1 -1
  5. package/dist/functions/auth_utils/imports.js +1 -0
  6. package/dist/functions/auth_utils/index.js +1 -0
  7. package/dist/functions/auth_utils/src/index.js +1 -0
  8. package/dist/functions/auth_utils/src/token_utils.js +104 -0
  9. package/dist/functions/config_utils/imports.js +2 -0
  10. package/dist/functions/config_utils/index.js +1 -0
  11. package/dist/functions/config_utils/src/config_utils.js +16 -0
  12. package/dist/functions/config_utils/src/index.js +1 -0
  13. package/dist/functions/constants_utils/index.js +1 -0
  14. package/dist/functions/constants_utils/src/constants.js +7 -0
  15. package/dist/functions/constants_utils/src/index.js +1 -0
  16. package/dist/functions/env_utils/imports.js +1 -0
  17. package/dist/functions/env_utils/index.js +1 -0
  18. package/dist/functions/env_utils/src/index.js +1 -0
  19. package/dist/functions/env_utils/src/window_utils.js +47 -0
  20. package/dist/functions/fetch_utils/imports.js +2 -0
  21. package/dist/functions/fetch_utils/index.js +1 -0
  22. package/dist/functions/fetch_utils/src/fetch_utils.js +65 -0
  23. package/dist/functions/fetch_utils/src/index.js +3 -0
  24. package/dist/functions/fetch_utils/src/url_utils.js +83 -0
  25. package/dist/functions/fetch_utils/src/utils.js +101 -0
  26. package/dist/functions/index.js +17 -0
  27. package/dist/functions/math_utils/index.js +1 -0
  28. package/dist/functions/math_utils/safe_math.js +26 -0
  29. package/dist/functions/path_utils/imports.js +28 -0
  30. package/dist/functions/path_utils/index.js +1 -0
  31. package/dist/functions/path_utils/src/base_dirs.js +17 -0
  32. package/dist/functions/path_utils/src/function_dirs.js +42 -0
  33. package/dist/functions/path_utils/src/index.js +6 -0
  34. package/dist/functions/path_utils/src/misc_dirs.js +15 -0
  35. package/dist/functions/path_utils/src/path_utils.browser.js +3 -0
  36. package/dist/functions/path_utils/src/path_utils.js +112 -0
  37. package/dist/functions/path_utils/src/path_utils.node.js +3 -0
  38. package/dist/functions/path_utils/src/paths.js +12 -0
  39. package/dist/functions/path_utils/src/src_dirs.js +25 -0
  40. package/dist/functions/read_utils/imports.js +1 -0
  41. package/dist/functions/read_utils/index.js +1 -0
  42. package/dist/functions/read_utils/src/index.js +1 -0
  43. package/dist/functions/read_utils/src/read_utils.browser.js +17 -0
  44. package/dist/functions/read_utils/src/utils.browser.js +32 -0
  45. package/dist/functions/read_utils/src/utils.js +70 -0
  46. package/dist/functions/rndm_utils/imports.js +1 -0
  47. package/dist/functions/rndm_utils/index.js +1 -0
  48. package/dist/functions/rndm_utils/src/index.js +1 -0
  49. package/dist/functions/rndm_utils/src/utils.js +13 -0
  50. package/dist/functions/safe_utils/imports.js +1 -0
  51. package/dist/functions/safe_utils/index.js +1 -0
  52. package/dist/functions/safe_utils/src/index.js +4 -0
  53. package/dist/functions/safe_utils/src/safe_document.js +12 -0
  54. package/dist/functions/safe_utils/src/safe_globals.js +14 -0
  55. package/dist/functions/safe_utils/src/safe_storage.js +52 -0
  56. package/dist/functions/safe_utils/src/safe_window.js +42 -0
  57. package/dist/functions/string_utils/index.js +1 -0
  58. package/dist/functions/string_utils/src/index.js +1 -0
  59. package/dist/functions/string_utils/src/string_utils.js +153 -0
  60. package/dist/functions/type_utils/imports.js +1 -0
  61. package/dist/functions/type_utils/index.js +1 -0
  62. package/dist/functions/type_utils/src/clean_utils.js +38 -0
  63. package/dist/functions/type_utils/src/ensure_utils.js +33 -0
  64. package/dist/functions/type_utils/src/index.js +5 -0
  65. package/dist/functions/type_utils/src/json_utils.js +22 -0
  66. package/dist/functions/type_utils/src/mime_utils.js +311 -0
  67. package/dist/functions/type_utils/src/type_utils.js +39 -0
  68. package/dist/functions/ui_utils/imports.js +5 -0
  69. package/dist/functions/ui_utils/index.js +1 -0
  70. package/dist/functions/ui_utils/src/button.js +22 -0
  71. package/dist/functions/ui_utils/src/checkbox.js +6 -0
  72. package/dist/functions/ui_utils/src/index.js +4 -0
  73. package/dist/functions/ui_utils/src/input.js +6 -0
  74. package/dist/functions/ui_utils/src/spinner.js +4 -0
  75. package/dist/functions/url_utils/index.js +1 -0
  76. package/dist/functions/url_utils/uri_utils.js +69 -0
  77. package/dist/functions/variable_utils/imports.js +1 -0
  78. package/dist/functions/variable_utils/index.js +1 -0
  79. package/dist/functions/variable_utils/src/index.js +1 -0
  80. package/dist/functions/variable_utils/src/variable_utils.js +34 -0
  81. package/dist/functions.json +1727 -0
  82. package/dist/index.js +2 -0
  83. package/dist/types/index.js +1 -0
  84. package/dist/types/src/ChangePassword.js +1 -0
  85. package/dist/types/src/Files.js +1 -0
  86. package/dist/types/src/index.js +5 -0
  87. package/dist/types/src/login.js +1 -0
  88. package/dist/types/src/logout.js +1 -0
  89. package/dist/types/src/utils.js +1 -0
  90. package/dist/utils/imports.js +1 -0
  91. package/dist/utils/index.js +1 -0
  92. package/dist/utils/src/Input.js +2 -0
  93. package/dist/utils/src/config.js +12 -0
  94. package/dist/utils/src/index.js +3 -0
  95. package/package.json +4 -1
@@ -0,0 +1 @@
1
+ export { callStorage } from './../safe_utils';
@@ -0,0 +1 @@
1
+ export * from './src';
@@ -0,0 +1 @@
1
+ export * from "./token_utils";
@@ -0,0 +1,104 @@
1
+ import { callStorage } from './../imports.js';
2
+ /**
3
+ ***Changes**:
4
+ *- Updated import path for `InputProps` to `../../types/interfaces`.
5
+ *
6
+ *3. **Token Utilities** (`src/functions/auth/token_utils.ts`):
7
+ * Copy from `/var/www/abstractendeavors/my-login-app/src/functions/auth_utils/token_utils.ts`.
8
+ *
9
+ */
10
+ /** Read raw JWT from LocalStorage (or null if absent) */
11
+ export function getToken() {
12
+ return callStorage('getItem', 'token');
13
+ }
14
+ export function isLoggedIn() {
15
+ const tok = getToken();
16
+ return !!tok && !isTokenExpired(tok !== null && tok !== void 0 ? tok : "");
17
+ }
18
+ export function removeToken() {
19
+ let is_logged = isLoggedIn();
20
+ if (is_logged) {
21
+ callStorage('removeItem', "token");
22
+ }
23
+ is_logged = isLoggedIn();
24
+ if (is_logged) {
25
+ return false;
26
+ }
27
+ return true;
28
+ }
29
+ /**
30
+ * Add a Bearer Authorization header.
31
+ * A shallow copy of headers is returned so callers can keep chaining.
32
+ */
33
+ export function getAuthorizationHeader(headers = {}, token = null) {
34
+ token = token !== null && token !== void 0 ? token : getToken();
35
+ headers = headers || {};
36
+ if (token)
37
+ headers["Authorization"] = `Bearer ${token}`;
38
+ return Object.assign({}, headers);
39
+ }
40
+ /** Throw + redirect if there’s no valid token; otherwise return it. */
41
+ export function requireToken() {
42
+ const tok = getToken();
43
+ if (!tok || isTokenExpired(tok)) {
44
+ console.warn("→ No token or expired token, redirecting to login…");
45
+ removeToken();
46
+ window.location.href = '/';
47
+ throw new Error("Redirecting to login…");
48
+ }
49
+ return tok;
50
+ }
51
+ /** True if token is structurally bad or its exp ≤ now. */
52
+ export function isTokenExpired(token) {
53
+ try {
54
+ const payload = decodeJwt(token);
55
+ return Date.now() / 1000 >= payload.exp;
56
+ }
57
+ catch (_a) {
58
+ return true; // treat malformed token as expired
59
+ }
60
+ }
61
+ /** Convenience wrapper: return username from the JWT (or null). */
62
+ export function currentUsername() {
63
+ const token = getToken();
64
+ let uName = null;
65
+ if (token) {
66
+ const decodedJwt = decodeJwt(token) || {};
67
+ uName = (decodedJwt === null || decodedJwt === void 0 ? void 0 : decodedJwt.username) || null;
68
+ }
69
+ return uName;
70
+ }
71
+ export function currentUsernames() {
72
+ var _a;
73
+ const tok = getToken();
74
+ if (!tok)
75
+ return null;
76
+ try {
77
+ const parts = tok.split(".");
78
+ if (parts.length !== 3)
79
+ return null;
80
+ let b64 = parts[1].replace(/-/g, "+").replace(/_/g, "/");
81
+ b64 = b64.padEnd(Math.ceil(b64.length / 4) * 4, "=");
82
+ const jsonText = atob(b64);
83
+ const payload = JSON.parse(jsonText);
84
+ return (_a = payload.username) !== null && _a !== void 0 ? _a : null;
85
+ }
86
+ catch (_b) {
87
+ return null;
88
+ }
89
+ }
90
+ /* ------------------------------------------------------------------ */
91
+ /* internals */
92
+ /* ------------------------------------------------------------------ */
93
+ export function decodeJwt(token) {
94
+ const [header, payload, signature] = token.split(".");
95
+ if (!header || !payload || !signature) {
96
+ throw new Error("Malformed JWT");
97
+ }
98
+ // Handle URL-safe Base64
99
+ let b64 = payload.replace(/-/g, "+").replace(/_/g, "/");
100
+ // Add padding if necessary
101
+ b64 = b64.padEnd(Math.ceil(b64.length / 4) * 4, "=");
102
+ const jsonText = atob(b64);
103
+ return JSON.parse(jsonText);
104
+ }
@@ -0,0 +1,2 @@
1
+ export { getDocumentProp, safeGlobalProp } from './../safe_utils';
2
+ export { readJsonFile } from './../read_utils';
@@ -0,0 +1 @@
1
+ export * from './src';
@@ -0,0 +1,16 @@
1
+ // src/functions/config_utils/src/config_utils.ts
2
+ import { __awaiter } from "tslib";
3
+ import { readJsonFile } from "../imports.js";
4
+ /**
5
+ * Attempt to load config.json if present, otherwise return empty object.
6
+ */
7
+ export function getConfigJson() {
8
+ return __awaiter(this, void 0, void 0, function* () {
9
+ try {
10
+ return (yield readJsonFile('config.json')) || {};
11
+ }
12
+ catch (_a) {
13
+ return {};
14
+ }
15
+ });
16
+ }
@@ -0,0 +1 @@
1
+ export * from './config_utils';
@@ -0,0 +1 @@
1
+ export * from './src';
@@ -0,0 +1,7 @@
1
+ export const PROTOCOL = 'https://';
2
+ export const DOMAIN_NAME = 'abstractendeavors';
3
+ export const BASE_URL = `${PROTOCOL}${DOMAIN_NAME}.com`;
4
+ export const SUB_DIR = 'secure-files';
5
+ export const PROD_PREFIX = `/${SUB_DIR}/`;
6
+ export const API_PREFIX = `/${SUB_DIR}/api`;
7
+ export const DEV_PREFIX = `/${SUB_DIR}-dev/`;
@@ -0,0 +1 @@
1
+ export * from './constants';
@@ -0,0 +1 @@
1
+ export { BASE_URL, DEV_PREFIX } from './../constants_utils/index';
@@ -0,0 +1 @@
1
+ export * from './src';
@@ -0,0 +1 @@
1
+ export * from './window_utils';
@@ -0,0 +1,47 @@
1
+ import { BASE_URL, DEV_PREFIX } from './../imports.js';
2
+ export function get_window() {
3
+ try {
4
+ if (typeof window !== 'undefined') {
5
+ return window;
6
+ }
7
+ }
8
+ catch (err) {
9
+ alert(err);
10
+ }
11
+ return null;
12
+ }
13
+ export function get_window_location() {
14
+ try {
15
+ const Window = get_window();
16
+ if (!Window) {
17
+ return BASE_URL;
18
+ }
19
+ return Window.location;
20
+ }
21
+ catch (err) {
22
+ alert(err);
23
+ }
24
+ return null;
25
+ }
26
+ export function get_window_pathname() {
27
+ try {
28
+ const Window = get_window();
29
+ if (!Window) {
30
+ return DEV_PREFIX;
31
+ }
32
+ return Window.location.pathname;
33
+ }
34
+ catch (err) {
35
+ alert(err);
36
+ }
37
+ return null;
38
+ }
39
+ export function get_window_parts() {
40
+ try {
41
+ return get_window_location();
42
+ }
43
+ catch (err) {
44
+ alert(err);
45
+ }
46
+ return null;
47
+ }
@@ -0,0 +1,2 @@
1
+ export { getAuthorizationHeader, getToken } from "./../auth_utils";
2
+ export { readJsonFile, getConfigContent } from './../read_utils';
@@ -0,0 +1 @@
1
+ export * from './src';
@@ -0,0 +1,65 @@
1
+ import { __awaiter } from "tslib";
2
+ import { getAuthorizationHeader } from './../imports.js';
3
+ import { getResult, parseResult } from './utils.js';
4
+ export function fetchIt(endpoint_1) {
5
+ return __awaiter(this, arguments, void 0, function* (endpoint, body = null, method = null, headers = null, blob = false, configUrl = false, withCredentials = true, returnJson = true, returnResult = true) {
6
+ method = (method || "GET").toUpperCase();
7
+ // 1) auto-detect absolute URLs
8
+ const isAbsolute = typeof endpoint === "string" && /^https?:\/\//i.test(endpoint);
9
+ // 2) choose the URL
10
+ let url = endpoint;
11
+ // 3) prepare headers & body
12
+ headers = Object.assign(Object.assign(Object.assign({}, (body instanceof FormData ? {} : { "Content-Type": "application/json" })), getAuthorizationHeader()), headers);
13
+ const opts = {
14
+ method,
15
+ credentials: withCredentials ? "include" : "same-origin",
16
+ headers,
17
+ body: body instanceof FormData
18
+ ? body
19
+ : body != null && method !== "GET"
20
+ ? JSON.stringify(body)
21
+ : undefined,
22
+ };
23
+ let res = yield fetch(url, opts);
24
+ if (!res.ok) {
25
+ const err = yield res.text();
26
+ throw new Error(`HTTP ${res.status}: ${err}`);
27
+ }
28
+ if (blob)
29
+ return res.blob();
30
+ if (returnResult || returnJson) {
31
+ let JsonRes = parseResult(res);
32
+ if (returnResult) {
33
+ JsonRes = getResult(JsonRes);
34
+ }
35
+ return JsonRes;
36
+ }
37
+ return res;
38
+ });
39
+ }
40
+ // Constructs HTML directory path
41
+ export function getHtmlDirectory(directory, filename) {
42
+ return `${directory}/${filename}.html`;
43
+ }
44
+ // Fetches HTML content
45
+ export function fetchIndexHtml(filename_1) {
46
+ return __awaiter(this, arguments, void 0, function* (filename, directory = 'sf_index', base = 'html') {
47
+ const url = `/${base}/${directory}/${filename}.html`;
48
+ const response = yield fetch(url);
49
+ return yield response.text();
50
+ });
51
+ }
52
+ // Fetches and injects HTML content into container
53
+ export function fetchIndexHtmlContainer(filename_1) {
54
+ return __awaiter(this, arguments, void 0, function* (filename, doc = document, directory = 'html') {
55
+ const container = `${filename}-container`;
56
+ const html = yield fetchIndexHtml(filename, directory);
57
+ const el = doc.getElementById(container);
58
+ if (el) {
59
+ el.innerHTML = html;
60
+ }
61
+ else {
62
+ console.warn(`⚠️ No container found for: #${container}`);
63
+ }
64
+ });
65
+ }
@@ -0,0 +1,3 @@
1
+ export * from './fetch_utils';
2
+ export * from './utils';
3
+ export * from './url_utils';
@@ -0,0 +1,83 @@
1
+ import path from 'path';
2
+ export function urlJoin(...parts) {
3
+ var _a;
4
+ const s = (parts.length === 1 && Array.isArray(parts[0]) ? parts[0] : parts);
5
+ let r = "";
6
+ for (let i = 0; i < s.length; i++) {
7
+ let d = ((_a = s[i]) !== null && _a !== void 0 ? _a : "").toString();
8
+ if (!d)
9
+ continue;
10
+ if (i === 0)
11
+ r = d;
12
+ else {
13
+ d = d.replace(/^\/+/, "");
14
+ r = r.replace(/\/+$/, "");
15
+ r = `${r}/${d}`;
16
+ }
17
+ }
18
+ return r;
19
+ }
20
+ /**
21
+ * Returns a full URL.
22
+ * If partial_url is already absolute (starts with http), it is returned as is.
23
+ * Otherwise, it is combined with the base URL.
24
+ */
25
+ export function get_full_url(partial_url, domain = null) {
26
+ if (typeof partial_url !== 'string') {
27
+ throw new Error('partial_url must be a string');
28
+ }
29
+ // If it already starts with http, assume it is absolute.
30
+ if (partial_url.startsWith('http')) {
31
+ return partial_url;
32
+ }
33
+ return urlJoin(domain, partial_url);
34
+ }
35
+ /**
36
+ * Returns a full file system path.
37
+ * If partial_path is already absolute, it is returned as is.
38
+ * Otherwise, it is joined with the base directory.
39
+ */
40
+ export function get_full_path(partial_path, parent_dir = null) {
41
+ if (typeof partial_path !== 'string') {
42
+ throw new Error('partial_path must be a string');
43
+ }
44
+ if (path.isAbsolute(partial_path)) {
45
+ return partial_path;
46
+ }
47
+ return urlJoin(parent_dir, partial_path);
48
+ }
49
+ /**
50
+ * Converts a local file system path into its corresponding URL.
51
+ * It checks against the known directories in all_paths and replaces the matching base.
52
+ */
53
+ export function path_to_url(filePath, all_paths) {
54
+ if (typeof filePath !== 'string') {
55
+ throw new Error('filePath must be a string');
56
+ }
57
+ for (const key in all_paths) {
58
+ const mapping = all_paths[key];
59
+ const normalizedBase = path.normalize(mapping.path);
60
+ if (filePath.startsWith(normalizedBase)) {
61
+ const relativePath = filePath.substring(normalizedBase.length);
62
+ return urlJoin(mapping.url, relativePath.replace(/\\/g, '/'));
63
+ }
64
+ }
65
+ return null;
66
+ }
67
+ /**
68
+ * Converts a URL into its corresponding local file system path.
69
+ * It checks against the known URL prefixes in all_paths and replaces the matching base.
70
+ */
71
+ export function url_to_path(urlStr, all_paths) {
72
+ if (typeof urlStr !== 'string') {
73
+ throw new Error('urlStr must be a string');
74
+ }
75
+ for (const key in all_paths) {
76
+ const mapping = all_paths[key];
77
+ if (urlStr.startsWith(mapping.url)) {
78
+ const relativeUrl = urlStr.substring(mapping.url.length);
79
+ return urlJoin(mapping.path, relativeUrl);
80
+ }
81
+ }
82
+ return null;
83
+ }
@@ -0,0 +1,101 @@
1
+ import { __awaiter } from "tslib";
2
+ import { getAuthorizationHeader, getToken } from './../imports.js';
3
+ /**
4
+ * Unwraps nested { result } fields until you hit a non-object or no more "result" keys.
5
+ */
6
+ export function getResult(obj) {
7
+ let current = obj;
8
+ while (current &&
9
+ typeof current === "object" &&
10
+ Object.prototype.hasOwnProperty.call(current, "result")) {
11
+ current = current.result;
12
+ }
13
+ return current;
14
+ }
15
+ export const get_result = getResult;
16
+ // Determines HTTP method, defaults to GET or POST based on body
17
+ export function getMethod(method = null, body = null) {
18
+ const validMethods = ['GET', 'POST', 'PUT', 'PATCH', 'PULL'];
19
+ method = (method || '').toUpperCase();
20
+ if (!validMethods.includes(method)) {
21
+ method = body ? 'POST' : 'GET';
22
+ }
23
+ return method;
24
+ }
25
+ // Gets headers, skips JSON headers when body is FormData
26
+ export function getHeaders(headers = {}, method = null, body = null) {
27
+ const result = Object.assign({}, headers);
28
+ // inject auth if missing
29
+ if (!result.Authorization) {
30
+ const token = getToken();
31
+ Object.assign(result, getAuthorizationHeader(result, token));
32
+ }
33
+ method = getMethod(method, body);
34
+ // if it’s a multipart FormData, let the browser set the boundary header
35
+ if (body instanceof FormData) {
36
+ return result;
37
+ }
38
+ // otherwise for POST/PUT/PATCH default to JSON
39
+ if (['POST', 'PUT', 'PATCH'].includes(method) && !result['Content-Type']) {
40
+ result['Content-Type'] = 'application/json';
41
+ }
42
+ return result;
43
+ }
44
+ // Prepares request body, serializes to JSON for non-GET requests
45
+ export function getBody(body = null, method = null) {
46
+ method = getMethod(method, body);
47
+ if (method === 'GET') {
48
+ return undefined;
49
+ }
50
+ if (body) {
51
+ try {
52
+ return JSON.stringify(body);
53
+ }
54
+ catch (err) {
55
+ return body;
56
+ }
57
+ }
58
+ return undefined;
59
+ }
60
+ // Prepares fetch variables, passes FormData intact
61
+ export function getFetchVars(headers = null, method = null, body = null) {
62
+ method = getMethod(method, body);
63
+ headers = getHeaders(headers || {}, method, body);
64
+ // only JSON-stringify non-FormData bodies
65
+ if (!(body instanceof FormData)) {
66
+ body = getBody(body, method);
67
+ }
68
+ return { method, headers, body };
69
+ }
70
+ /*
71
+ * parseResult no longer needs to worry about JSON vs HTML redirect errors;
72
+ * all 401/403 have already been handled above.
73
+ */
74
+ export function parseResult(res) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ // runs checkResponse first, will throw if session is expired
77
+ res = checkResponse(res);
78
+ if (!res.ok) {
79
+ // for any other non-401 errors, you can still surface them
80
+ const errorText = yield res.text();
81
+ throw new Error(errorText || res.statusText);
82
+ }
83
+ // now safely parse JSON
84
+ return res.json();
85
+ });
86
+ }
87
+ /**
88
+ * Intercept 401/403 and force a clean redirect to login
89
+ * without ever showing an alert.
90
+ */
91
+ export function checkResponse(res) {
92
+ if (res.status === 401 || res.status === 403) {
93
+ // 1) clear out the stale token
94
+ localStorage.removeItem("token");
95
+ // 2) replace history so "back" doesn’t re-trigger the protected route
96
+ window.history.replaceState({}, "", "/secure-files");
97
+ // 3) short-circuit all further fetch logic
98
+ throw new Error("SessionExpired");
99
+ }
100
+ return res;
101
+ }
@@ -0,0 +1,17 @@
1
+ export { useCallback, useRef, useState, useEffect } from "react";
2
+ export * from './auth_utils';
3
+ export * from './constants_utils';
4
+ export * from './env_utils';
5
+ export * from './fetch_utils';
6
+ export * from './path_utils';
7
+ export * from './rndm_utils';
8
+ export * from './string_utils';
9
+ export * from './type_utils';
10
+ export * from './ui_utils';
11
+ export * from './config_utils';
12
+ export * from './read_utils';
13
+ export * from './safe_utils';
14
+ export * from './read_utils';
15
+ export * from './variable_utils';
16
+ export * from './math_utils';
17
+ export * from './url_utils';
@@ -0,0 +1 @@
1
+ export * from './safe_math';
@@ -0,0 +1,26 @@
1
+ import { ensure_number } from './../type_utils/index.js';
2
+ // take N args and coerce them all to numbers
3
+ export function safeNums(...args) {
4
+ return args.map(ensure_number);
5
+ }
6
+ // divide the first value by each of the following
7
+ export function safeDivide(...args) {
8
+ const [head, ...rest] = safeNums(...args);
9
+ // if we don’t have a head or any divisor is zero, bail
10
+ if (head === 0 || rest.some((d) => d === 0))
11
+ return 0;
12
+ return rest.reduce((acc, d) => acc / d, head);
13
+ }
14
+ // multiply all the values together
15
+ export function safeMultiply(...args) {
16
+ const nums = safeNums(...args);
17
+ // if any number is zero, result is zero
18
+ if (nums.includes(0))
19
+ return 0;
20
+ return nums.reduce((acc, n) => acc * n, 1);
21
+ }
22
+ // round a value to two decimals by percent
23
+ export function roundPercentage(x) {
24
+ const pct = safeMultiply(ensure_number(x), 100);
25
+ return safeDivide(Math.round(pct), 100);
26
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * In the browser we already have a WHATWG URL constructor on window.
3
+ * Here we re-export it as “url” so other modules can import it.
4
+ */
5
+ const _url = typeof window !== 'undefined' ? window.URL : undefined;
6
+ export { _url as url };
7
+ /**
8
+ * Minimal fileURLToPath implementation for browser-side code.
9
+ * If you only ever need to strip off “file://” in development, this is enough.
10
+ */
11
+ export function fileURLToPath(fileUrl) {
12
+ // e.g. fileUrl = "file:///Users/foo/bar.txt"
13
+ try {
14
+ const u = new URL(fileUrl);
15
+ return u.pathname;
16
+ }
17
+ catch (_a) {
18
+ // fallback: just strip file://
19
+ return fileUrl.replace(/^file:\/\//, '');
20
+ }
21
+ }
22
+ export { eatOuter, eatInner } from './../string_utils';
23
+ export { ensure_list } from './../type_utils';
24
+ export function getAbsolutePath() {
25
+ if (typeof window !== 'undefined')
26
+ return '';
27
+ return fileURLToPath(import.meta.url);
28
+ }
@@ -0,0 +1 @@
1
+ export * from './src';
@@ -0,0 +1,17 @@
1
+ import { make_path, get_dirname } from './path_utils.js';
2
+ import { getSrcDir } from './src_dirs.js';
3
+ export function getBaseDir() {
4
+ return get_dirname(getSrcDir());
5
+ }
6
+ export function getPublicDir() {
7
+ return make_path(getBaseDir(), 'public');
8
+ }
9
+ ;
10
+ export function getDistDir() {
11
+ return make_path(getBaseDir(), 'dist');
12
+ }
13
+ ;
14
+ export function getEnvDir() {
15
+ return make_path(getBaseDir(), '.env');
16
+ }
17
+ ;
@@ -0,0 +1,42 @@
1
+ import { make_path, get_dirname } from './path_utils.js';
2
+ import { getAbsDir } from './paths.js';
3
+ export function getFunctionsDir() {
4
+ return get_dirname(getAbsDir());
5
+ }
6
+ ;
7
+ export function geAuthsUtilsDirectory() {
8
+ return make_path(getFunctionsDir(), 'auths');
9
+ }
10
+ ;
11
+ export function geBackupsUtilsDirectory() {
12
+ return make_path(getFunctionsDir(), 'backups');
13
+ }
14
+ ;
15
+ export function geConstantsUtilsDirectory() {
16
+ return make_path(getFunctionsDir(), 'constants');
17
+ }
18
+ ;
19
+ export function geEnvUtilsDirectory() {
20
+ return make_path(getFunctionsDir(), 'env_utils');
21
+ }
22
+ ;
23
+ export function geFetchUtilsDirectory() {
24
+ return make_path(getFunctionsDir(), 'fetch_utils');
25
+ }
26
+ ;
27
+ export function geFileUtilsDirectory() {
28
+ return make_path(getFunctionsDir(), 'file_utils');
29
+ }
30
+ ;
31
+ export function gePathUtilsDirectory() {
32
+ return make_path(getFunctionsDir(), 'path_utils');
33
+ }
34
+ ;
35
+ export function geStringUtilsDirectory() {
36
+ return make_path(getFunctionsDir(), 'string_utils');
37
+ }
38
+ ;
39
+ export function geTypeUtilsDirectory() {
40
+ return make_path(getFunctionsDir(), 'type_utils');
41
+ }
42
+ ;
@@ -0,0 +1,6 @@
1
+ export * from './path_utils';
2
+ export * from './paths';
3
+ export * from './function_dirs';
4
+ export * from './src_dirs';
5
+ export * from './base_dirs';
6
+ export * from './misc_dirs';
@@ -0,0 +1,15 @@
1
+ import { getBaseDir, getEnvDir } from './base_dirs.js';
2
+ import { make_path } from './path_utils.js';
3
+ export function getEnvPath(string = '.env') {
4
+ return make_path(getEnvDir(), string);
5
+ }
6
+ ;
7
+ export function getDbConfigsPath() {
8
+ return make_path(getBaseDir(), 'dbConfigs');
9
+ }
10
+ export function getSchemasPath() {
11
+ return make_path(getDbConfigsPath(), 'schemas');
12
+ }
13
+ export function getSchemasDirPath(subPath) {
14
+ return make_path(getSchemasPath(), subPath);
15
+ }
@@ -0,0 +1,3 @@
1
+ // path_utils.browser.ts
2
+ import * as path from 'path-browserify';
3
+ export { path };