@putkoff/abstract-utilities 0.1.242 → 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.
- package/dist/functions/auth_utils/imports.js +1 -0
- package/dist/functions/auth_utils/index.js +1 -0
- package/dist/functions/auth_utils/src/index.js +1 -0
- package/dist/functions/auth_utils/src/token_utils.js +104 -0
- package/dist/functions/config_utils/imports.js +2 -0
- package/dist/functions/config_utils/index.js +1 -0
- package/dist/functions/config_utils/src/config_utils.js +16 -0
- package/dist/functions/config_utils/src/index.js +1 -0
- package/dist/functions/constants_utils/index.js +1 -0
- package/dist/functions/constants_utils/src/constants.js +7 -0
- package/dist/functions/constants_utils/src/index.js +1 -0
- package/dist/functions/env_utils/imports.js +1 -0
- package/dist/functions/env_utils/index.js +1 -0
- package/dist/functions/env_utils/src/index.js +1 -0
- package/dist/functions/env_utils/src/window_utils.js +47 -0
- package/dist/functions/fetch_utils/imports.js +2 -0
- package/dist/functions/fetch_utils/index.js +1 -0
- package/dist/functions/fetch_utils/src/fetch_utils.js +65 -0
- package/dist/functions/fetch_utils/src/index.js +3 -0
- package/dist/functions/fetch_utils/src/url_utils.js +83 -0
- package/dist/functions/fetch_utils/src/utils.js +101 -0
- package/dist/functions/index.js +17 -0
- package/dist/functions/math_utils/index.js +1 -0
- package/dist/functions/math_utils/safe_math.js +26 -0
- package/dist/functions/path_utils/imports.js +28 -0
- package/dist/functions/path_utils/index.js +1 -0
- package/dist/functions/path_utils/src/base_dirs.js +17 -0
- package/dist/functions/path_utils/src/function_dirs.js +42 -0
- package/dist/functions/path_utils/src/index.js +6 -0
- package/dist/functions/path_utils/src/misc_dirs.js +15 -0
- package/dist/functions/path_utils/src/path_utils.browser.js +3 -0
- package/dist/functions/path_utils/src/path_utils.js +112 -0
- package/dist/functions/path_utils/src/path_utils.node.js +3 -0
- package/dist/functions/path_utils/src/paths.js +12 -0
- package/dist/functions/path_utils/src/src_dirs.js +25 -0
- package/dist/functions/read_utils/imports.js +1 -0
- package/dist/functions/read_utils/index.js +1 -0
- package/dist/functions/read_utils/src/index.js +1 -0
- package/dist/functions/read_utils/src/read_utils.browser.js +17 -0
- package/dist/functions/read_utils/src/utils.browser.js +32 -0
- package/dist/functions/read_utils/src/utils.js +70 -0
- package/dist/functions/rndm_utils/imports.js +1 -0
- package/dist/functions/rndm_utils/index.js +1 -0
- package/dist/functions/rndm_utils/src/index.js +1 -0
- package/dist/functions/rndm_utils/src/utils.js +13 -0
- package/dist/functions/safe_utils/imports.js +1 -0
- package/dist/functions/safe_utils/index.js +1 -0
- package/dist/functions/safe_utils/src/index.js +4 -0
- package/dist/functions/safe_utils/src/safe_document.js +12 -0
- package/dist/functions/safe_utils/src/safe_globals.js +14 -0
- package/dist/functions/safe_utils/src/safe_storage.js +52 -0
- package/dist/functions/safe_utils/src/safe_window.js +42 -0
- package/dist/functions/string_utils/index.js +1 -0
- package/dist/functions/string_utils/src/index.js +1 -0
- package/dist/functions/string_utils/src/string_utils.js +153 -0
- package/dist/functions/type_utils/imports.js +1 -0
- package/dist/functions/type_utils/index.js +1 -0
- package/dist/functions/type_utils/src/clean_utils.js +38 -0
- package/dist/functions/type_utils/src/ensure_utils.js +33 -0
- package/dist/functions/type_utils/src/index.js +5 -0
- package/dist/functions/type_utils/src/json_utils.js +22 -0
- package/dist/functions/type_utils/src/mime_utils.js +311 -0
- package/dist/functions/type_utils/src/type_utils.js +39 -0
- package/dist/functions/ui_utils/imports.js +5 -0
- package/dist/functions/ui_utils/index.js +1 -0
- package/dist/functions/ui_utils/src/button.js +22 -0
- package/dist/functions/ui_utils/src/checkbox.js +6 -0
- package/dist/functions/ui_utils/src/index.js +4 -0
- package/dist/functions/ui_utils/src/input.js +6 -0
- package/dist/functions/ui_utils/src/spinner.js +4 -0
- package/dist/functions/url_utils/index.js +1 -0
- package/dist/functions/url_utils/uri_utils.js +69 -0
- package/dist/functions/variable_utils/imports.js +1 -0
- package/dist/functions/variable_utils/index.js +1 -0
- package/dist/functions/variable_utils/src/index.js +1 -0
- package/dist/functions/variable_utils/src/variable_utils.js +34 -0
- package/dist/functions.json +1727 -0
- package/dist/index.js +2 -0
- package/dist/types/index.js +1 -0
- package/dist/types/src/ChangePassword.js +1 -0
- package/dist/types/src/Files.js +1 -0
- package/dist/types/src/index.js +5 -0
- package/dist/types/src/login.js +1 -0
- package/dist/types/src/logout.js +1 -0
- package/dist/types/src/utils.js +1 -0
- package/dist/utils/imports.js +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/src/Input.js +2 -0
- package/dist/utils/src/config.js +12 -0
- package/dist/utils/src/index.js +3 -0
- package/package.json +4 -1
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// path_utils.browser.ts
|
|
2
|
+
import * as path from 'path-browserify';
|
|
3
|
+
import { eatOuter, eatInner, ensure_list } from './../imports.js';
|
|
4
|
+
export function get_dirname(filePath) {
|
|
5
|
+
if (!filePath)
|
|
6
|
+
return '';
|
|
7
|
+
return path.dirname(filePath);
|
|
8
|
+
}
|
|
9
|
+
export function get_basename(filePath) {
|
|
10
|
+
if (!filePath)
|
|
11
|
+
return '';
|
|
12
|
+
return path.basename(filePath);
|
|
13
|
+
}
|
|
14
|
+
export function get_filename(file_path) {
|
|
15
|
+
const ext = path.extname(file_path);
|
|
16
|
+
return path.basename(file_path, ext);
|
|
17
|
+
}
|
|
18
|
+
export function get_extname(filePath) {
|
|
19
|
+
if (!filePath)
|
|
20
|
+
return '';
|
|
21
|
+
return path.extname(filePath);
|
|
22
|
+
}
|
|
23
|
+
export function get_splitext(filePath) {
|
|
24
|
+
if (!filePath)
|
|
25
|
+
return { filename: '', extname: '' };
|
|
26
|
+
const ext = path.extname(filePath);
|
|
27
|
+
// Get the basename without the extension
|
|
28
|
+
const filename = path.basename(filePath, ext);
|
|
29
|
+
return { filename, ext };
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Join multiple path segments, normalizing leading/trailing slashes.
|
|
33
|
+
*
|
|
34
|
+
* Usage:
|
|
35
|
+
* make_path('/foo','bar','baz')
|
|
36
|
+
* make_path(['/foo','bar','baz'])
|
|
37
|
+
*/
|
|
38
|
+
export function make_path(...paths) {
|
|
39
|
+
// If someone passed a single array, unwrap it:
|
|
40
|
+
const pathArray = (paths.length === 1 && Array.isArray(paths[0])
|
|
41
|
+
? paths[0]
|
|
42
|
+
: paths);
|
|
43
|
+
let real_path = '';
|
|
44
|
+
for (let i = 0; i < pathArray.length; i++) {
|
|
45
|
+
let segment = pathArray[i];
|
|
46
|
+
if (i === 0) {
|
|
47
|
+
real_path = segment;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
// remove any leading slash on this segment
|
|
51
|
+
segment = segment.replace(/^\/+/, '');
|
|
52
|
+
// remove any trailing slash on the accumulator
|
|
53
|
+
real_path = real_path.replace(/\/+$/, '');
|
|
54
|
+
real_path = `${real_path}/${segment}`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return real_path;
|
|
58
|
+
}
|
|
59
|
+
export function makePath(...paths) {
|
|
60
|
+
var _a;
|
|
61
|
+
const pathArray = ensure_list(paths);
|
|
62
|
+
let real_path = '';
|
|
63
|
+
for (let i = 0; i < pathArray.length; i++) {
|
|
64
|
+
let seg = String((_a = pathArray[i]) !== null && _a !== void 0 ? _a : '');
|
|
65
|
+
if (i === 0) {
|
|
66
|
+
real_path = seg;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
seg = eatInner(seg, ['/']);
|
|
70
|
+
real_path = eatOuter(real_path, ['/']);
|
|
71
|
+
real_path = `${real_path}/${seg}`;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return real_path || '';
|
|
75
|
+
}
|
|
76
|
+
export function sanitizeFilename(filename) {
|
|
77
|
+
return filename
|
|
78
|
+
.toLowerCase()
|
|
79
|
+
.replace(/\s+|-/g, '-') // Replace spaces and hyphens with single hyphen
|
|
80
|
+
.replace(/_/g, '-') // Replace underscores with hyphens
|
|
81
|
+
.replace(/[^a-z0-9-.]/g, '') // Remove all non-alphanumeric chars except hyphen and dot
|
|
82
|
+
.replace(/-+/g, '-') // Collapse multiple hyphens into one
|
|
83
|
+
.replace(/^-|-$/, ''); // Remove leading/trailing hyphens
|
|
84
|
+
}
|
|
85
|
+
export function get_relative_path(directory, fullPath) {
|
|
86
|
+
return path.relative(directory, fullPath);
|
|
87
|
+
}
|
|
88
|
+
// Safer resolver that strips .. at the front only, but prefer server-side checks.
|
|
89
|
+
export function get_safe_path(p) {
|
|
90
|
+
return path.normalize(p).replace(/^(\.\.[/\\])+/, '');
|
|
91
|
+
}
|
|
92
|
+
export function make_sanitized_path(...paths) {
|
|
93
|
+
let real_path = '';
|
|
94
|
+
for (let i = 0; i < paths.length; i++) {
|
|
95
|
+
const sanitized = sanitizeFilename(eatInner(paths[i], ['/']));
|
|
96
|
+
if (i === 0) {
|
|
97
|
+
real_path = sanitized;
|
|
98
|
+
}
|
|
99
|
+
else if (sanitized) { // Only append if there's a non-empty segment
|
|
100
|
+
real_path = `${real_path}/${sanitized}`;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return real_path || '';
|
|
104
|
+
}
|
|
105
|
+
/** FIXED: your regexes were strings. This correctly joins without duplicate slashes. */
|
|
106
|
+
export function normalizeUrl(base, p) {
|
|
107
|
+
if (!p)
|
|
108
|
+
return base.replace(/\/+$/g, '');
|
|
109
|
+
const cleanBase = base.replace(/\/+$/g, '');
|
|
110
|
+
const cleanPath = p.replace(/^\/+/g, '');
|
|
111
|
+
return `${cleanBase}/${cleanPath}`.replace(/([^:])\/{2,}/g, '$1/'); // keep protocol //
|
|
112
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { getAbsolutePath } from './../imports.js';
|
|
2
|
+
import { get_dirname, make_path } from './path_utils.js';
|
|
3
|
+
/**
|
|
4
|
+
* Returns the absolute path of the current file.
|
|
5
|
+
*/
|
|
6
|
+
export function getAbsDir() {
|
|
7
|
+
return get_dirname(getAbsolutePath());
|
|
8
|
+
}
|
|
9
|
+
;
|
|
10
|
+
export function getAbsPath(subPath) {
|
|
11
|
+
return make_path(getAbsDir(), subPath);
|
|
12
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { make_path, get_dirname } from './path_utils.js';
|
|
2
|
+
import { getFunctionsDir } from './function_dirs.js';
|
|
3
|
+
export function getSrcDir() {
|
|
4
|
+
return get_dirname(getFunctionsDir());
|
|
5
|
+
}
|
|
6
|
+
;
|
|
7
|
+
export function geStaticDirectory() {
|
|
8
|
+
return make_path(getSrcDir(), 'static');
|
|
9
|
+
}
|
|
10
|
+
;
|
|
11
|
+
export function getLibUtilsDirectory() {
|
|
12
|
+
return make_path(getSrcDir(), 'lib');
|
|
13
|
+
}
|
|
14
|
+
export function getHooksUtilsDirectory() {
|
|
15
|
+
return make_path(getSrcDir(), 'hooks');
|
|
16
|
+
}
|
|
17
|
+
;
|
|
18
|
+
export function getFunctionsUtilsDirectory() {
|
|
19
|
+
return make_path(getSrcDir(), 'functions');
|
|
20
|
+
}
|
|
21
|
+
;
|
|
22
|
+
export function getComponentsUtilsDirectory() {
|
|
23
|
+
return make_path(getSrcDir(), 'components');
|
|
24
|
+
}
|
|
25
|
+
;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { safeGlobalProp } from './../safe_utils';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './utils';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
2
|
+
// No Node imports!
|
|
3
|
+
import { safeGlobalProp } from './../imports.js';
|
|
4
|
+
export function readJsonFile(url) {
|
|
5
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
6
|
+
try {
|
|
7
|
+
const fetchFn = safeGlobalProp('fetch') || fetch;
|
|
8
|
+
const res = yield fetchFn(url);
|
|
9
|
+
if (!res.ok)
|
|
10
|
+
return null;
|
|
11
|
+
return yield res.json();
|
|
12
|
+
}
|
|
13
|
+
catch (_a) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
2
|
+
import { safeGlobalProp } from './../imports.js';
|
|
3
|
+
export function readJsonFile(url) {
|
|
4
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
5
|
+
const fetchFn = safeGlobalProp('fetch');
|
|
6
|
+
if (typeof fetchFn !== 'function')
|
|
7
|
+
return null;
|
|
8
|
+
try {
|
|
9
|
+
const res = yield fetchFn(url);
|
|
10
|
+
if (!res.ok)
|
|
11
|
+
return null;
|
|
12
|
+
return yield res.json();
|
|
13
|
+
}
|
|
14
|
+
catch (_a) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
export function getConfigContent() {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
return readJsonFile('config.json');
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
export function getConfigVar() {
|
|
25
|
+
return __awaiter(this, arguments, void 0, function* (key = null) {
|
|
26
|
+
const cfg = yield getConfigContent();
|
|
27
|
+
if (cfg && typeof cfg === 'object' && key in cfg) {
|
|
28
|
+
return cfg[key];
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
2
|
+
import { safeGlobalProp } from './../imports.js';
|
|
3
|
+
// src/functions/read_utils/src/read_utils.ts
|
|
4
|
+
/**
|
|
5
|
+
* Reads a JSON file, either via Node’s fs (if available)
|
|
6
|
+
* or via window.fetch in the browser. Never throws — returns
|
|
7
|
+
* the parsed object or null on any error.
|
|
8
|
+
*/
|
|
9
|
+
export function readJsonFile(relativeOrAbsolutePath) {
|
|
10
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
11
|
+
if (typeof window === 'undefined') {
|
|
12
|
+
try {
|
|
13
|
+
const { readFile } = yield import('fs/promises');
|
|
14
|
+
const { isAbsolute, resolve } = yield import('path');
|
|
15
|
+
const filePath = isAbsolute(relativeOrAbsolutePath)
|
|
16
|
+
? relativeOrAbsolutePath
|
|
17
|
+
: resolve(import.meta.url ? new URL('.', import.meta.url).pathname : '', relativeOrAbsolutePath);
|
|
18
|
+
const text = yield readFile(filePath, 'utf8');
|
|
19
|
+
return JSON.parse(text);
|
|
20
|
+
}
|
|
21
|
+
catch (_a) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// Browser fallback
|
|
26
|
+
const fetchFn = safeGlobalProp('fetch');
|
|
27
|
+
if (typeof fetchFn !== 'function')
|
|
28
|
+
return null;
|
|
29
|
+
let url = relativeOrAbsolutePath;
|
|
30
|
+
const baseURI = safeGlobalProp('document', 'baseURI');
|
|
31
|
+
if (baseURI) {
|
|
32
|
+
try {
|
|
33
|
+
url = new URL(relativeOrAbsolutePath, baseURI).href;
|
|
34
|
+
}
|
|
35
|
+
catch (_b) { }
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const res = yield fetchFn(url);
|
|
39
|
+
if (!res.ok)
|
|
40
|
+
return null;
|
|
41
|
+
return yield res.json();
|
|
42
|
+
}
|
|
43
|
+
catch (_c) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
export function getConfigContent() {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
try {
|
|
51
|
+
// `readJsonFile` should throw if the file isn’t there or isn’t valid JSON
|
|
52
|
+
const cfg = yield readJsonFile('./config.json');
|
|
53
|
+
return cfg;
|
|
54
|
+
}
|
|
55
|
+
catch (_a) {
|
|
56
|
+
// swallow errors & return null so callers can detect “no config”
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
// 2) Pull a single key out of that object
|
|
62
|
+
export function getConfigVar() {
|
|
63
|
+
return __awaiter(this, arguments, void 0, function* (key = null) {
|
|
64
|
+
const cfg = yield getConfigContent();
|
|
65
|
+
if (cfg && typeof cfg === 'object' && key in cfg) {
|
|
66
|
+
return cfg[key];
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './utils';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/functions/rndm_utils/utils.ts
|
|
2
|
+
export function alertit(obj = null) {
|
|
3
|
+
let msg;
|
|
4
|
+
try {
|
|
5
|
+
msg = JSON.stringify(obj);
|
|
6
|
+
}
|
|
7
|
+
catch (_a) {
|
|
8
|
+
// If JSON.stringify fails (circular refs, etc.), fall back to a simple string
|
|
9
|
+
msg = String(obj);
|
|
10
|
+
}
|
|
11
|
+
alert(msg);
|
|
12
|
+
}
|
|
13
|
+
export const alertIt = alertit;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function getSafeDocument() {
|
|
2
|
+
return typeof document !== 'undefined' ? document : undefined;
|
|
3
|
+
}
|
|
4
|
+
export function getDocumentProp(...keys) {
|
|
5
|
+
let obj = getSafeDocument();
|
|
6
|
+
for (const k of keys) {
|
|
7
|
+
if (obj == null || typeof obj !== 'object')
|
|
8
|
+
return undefined;
|
|
9
|
+
obj = obj[k];
|
|
10
|
+
}
|
|
11
|
+
return obj;
|
|
12
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safely walk `globalThis` (or window/document) by a chain of property names.
|
|
3
|
+
* Returns `undefined` if any step is missing.
|
|
4
|
+
*/
|
|
5
|
+
export function safeGlobalProp(...path) {
|
|
6
|
+
let obj = globalThis;
|
|
7
|
+
for (const key of path) {
|
|
8
|
+
if (obj == null || typeof obj !== "object" || !(key in obj)) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
obj = obj[key];
|
|
12
|
+
}
|
|
13
|
+
return obj;
|
|
14
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { safeGlobalProp } from './safe_globals.js';
|
|
2
|
+
/**
|
|
3
|
+
* Returns `window` if running in a browser, otherwise `undefined`.
|
|
4
|
+
*/
|
|
5
|
+
export function getSafeLocalStorage() {
|
|
6
|
+
if (typeof window === 'undefined')
|
|
7
|
+
return undefined;
|
|
8
|
+
try {
|
|
9
|
+
return window.localStorage;
|
|
10
|
+
}
|
|
11
|
+
catch (_a) {
|
|
12
|
+
return undefined; // e.g. Safari private-mode block
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Call a Storage method by name, silencing any errors or missing storage.
|
|
17
|
+
*
|
|
18
|
+
* @param method One of the keys of the Storage interface: "getItem", "setItem", etc.
|
|
19
|
+
* @param args The arguments you’d normally pass to that method.
|
|
20
|
+
* @returns The method’s return value, or undefined if storage/method isn’t available.
|
|
21
|
+
*/
|
|
22
|
+
export function callStorage(method, ...args) {
|
|
23
|
+
const storage = getSafeLocalStorage();
|
|
24
|
+
if (!storage)
|
|
25
|
+
return undefined;
|
|
26
|
+
const fn = storage[method];
|
|
27
|
+
if (typeof fn !== 'function')
|
|
28
|
+
return undefined;
|
|
29
|
+
try {
|
|
30
|
+
// @ts-ignore – TS can’t infer that this is callable
|
|
31
|
+
return fn.apply(storage, args);
|
|
32
|
+
}
|
|
33
|
+
catch (_a) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Safely call storage methods (`localStorage` or `sessionStorage`) without blowing up.
|
|
39
|
+
* Returns `undefined` on any error.
|
|
40
|
+
*/
|
|
41
|
+
export function safeStorage(storageName, method, ...args) {
|
|
42
|
+
try {
|
|
43
|
+
const store = safeGlobalProp(storageName);
|
|
44
|
+
if (!store || typeof store[method] !== "function")
|
|
45
|
+
return undefined;
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
return store[method](...args);
|
|
48
|
+
}
|
|
49
|
+
catch (_a) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the global window object if it exists, otherwise undefined.
|
|
3
|
+
*/
|
|
4
|
+
export function getSafeWindow() {
|
|
5
|
+
return typeof window !== 'undefined' ? window : undefined;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Safely call a method on window by name.
|
|
9
|
+
*
|
|
10
|
+
* @param method The Window method to call (e.g. "alert", "open", etc.).
|
|
11
|
+
* @param args Arguments to pass to that method.
|
|
12
|
+
* @returns The method’s return value, or undefined if
|
|
13
|
+
* window/method isn’t available or throws.
|
|
14
|
+
*/
|
|
15
|
+
export function callWindowMethod(method, ...args) {
|
|
16
|
+
const w = getSafeWindow();
|
|
17
|
+
if (!w)
|
|
18
|
+
return undefined;
|
|
19
|
+
const fn = w[method];
|
|
20
|
+
if (typeof fn !== 'function')
|
|
21
|
+
return undefined;
|
|
22
|
+
try {
|
|
23
|
+
// cast to any so TS doesn’t complain about apply/invoke
|
|
24
|
+
return fn(...args);
|
|
25
|
+
}
|
|
26
|
+
catch (_a) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** implementation */
|
|
31
|
+
export function getWindowProp(...keys) {
|
|
32
|
+
let obj = getSafeWindow();
|
|
33
|
+
for (const k of keys) {
|
|
34
|
+
if (obj == null || typeof obj !== 'object')
|
|
35
|
+
return undefined;
|
|
36
|
+
obj = obj[k];
|
|
37
|
+
}
|
|
38
|
+
return obj;
|
|
39
|
+
}
|
|
40
|
+
export function getWindowHost() {
|
|
41
|
+
return getWindowProp('location', 'host');
|
|
42
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './string_utils';
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { assureString } from './../../type_utils/index.js';
|
|
2
|
+
export function getSubstring(obj, maxLength = null, minLength = null) {
|
|
3
|
+
const objLength = obj.length;
|
|
4
|
+
const effectiveMaxLength = maxLength !== null && maxLength !== void 0 ? maxLength : objLength; // Use nullish coalescing for clarity
|
|
5
|
+
const effectiveMinLength = minLength !== null && minLength !== void 0 ? minLength : 0;
|
|
6
|
+
// Ensure bounds are valid
|
|
7
|
+
const clampedMaxLength = Math.min(Math.max(effectiveMaxLength, 0), objLength);
|
|
8
|
+
const clampedMinLength = Math.min(Math.max(effectiveMinLength, 0), objLength);
|
|
9
|
+
// If minLength exceeds maxLength, return empty string or adjust logic as needed
|
|
10
|
+
if (clampedMinLength >= clampedMaxLength) {
|
|
11
|
+
return '';
|
|
12
|
+
}
|
|
13
|
+
return obj.substring(clampedMinLength, clampedMaxLength);
|
|
14
|
+
}
|
|
15
|
+
export function truncateString(obj, maxLength = 20) {
|
|
16
|
+
const objLength = obj.length;
|
|
17
|
+
if (objLength > maxLength && maxLength) {
|
|
18
|
+
obj = getSubstring(obj, maxLength) + '...';
|
|
19
|
+
}
|
|
20
|
+
return obj;
|
|
21
|
+
}
|
|
22
|
+
export function capitalize_str(string) {
|
|
23
|
+
string = assureString(string);
|
|
24
|
+
const string_len = string.length;
|
|
25
|
+
let init_char = string.toUpperCase();
|
|
26
|
+
if (string_len > 0) {
|
|
27
|
+
init_char = string[0].toUpperCase();
|
|
28
|
+
}
|
|
29
|
+
let rest_chars = '';
|
|
30
|
+
if (string_len > 1) {
|
|
31
|
+
rest_chars = string.slice(1).toLowerCase();
|
|
32
|
+
}
|
|
33
|
+
const fin_chars = `${init_char}${rest_chars}`;
|
|
34
|
+
return fin_chars;
|
|
35
|
+
}
|
|
36
|
+
export function capitalize(string) {
|
|
37
|
+
let nu_string = '';
|
|
38
|
+
string = assureString(string);
|
|
39
|
+
let objs = string.replace('-', '_').split('_');
|
|
40
|
+
for (const obj of objs) {
|
|
41
|
+
let str_obj = capitalize_str(obj);
|
|
42
|
+
nu_string = `${nu_string} ${str_obj}`;
|
|
43
|
+
}
|
|
44
|
+
return eatAll(nu_string, [' ']);
|
|
45
|
+
}
|
|
46
|
+
// string_utils/src/string_utils.ts
|
|
47
|
+
export function stripPrefixes(str, bases = []) {
|
|
48
|
+
/* NEW: coerce whatever arrives into a string */
|
|
49
|
+
str = String(str);
|
|
50
|
+
const prefixes = (Array.isArray(bases) ? bases : [bases])
|
|
51
|
+
.filter(Boolean)
|
|
52
|
+
.sort((a, b) => b.length - a.length); // longest first
|
|
53
|
+
let changed = true;
|
|
54
|
+
while (changed) {
|
|
55
|
+
changed = false;
|
|
56
|
+
for (const prefix of prefixes) {
|
|
57
|
+
if (str.startsWith(prefix)) {
|
|
58
|
+
str = str.slice(prefix.length);
|
|
59
|
+
changed = true;
|
|
60
|
+
break; // restart from longest prefix
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return str;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Removes characters from the beginning of the string
|
|
68
|
+
* if they are found in the list of characters.
|
|
69
|
+
*
|
|
70
|
+
* @param str - The input string.
|
|
71
|
+
* @param listObjects - A string or an array of characters to remove.
|
|
72
|
+
* @returns The modified string.
|
|
73
|
+
*/
|
|
74
|
+
export function eatInner(str, listObjects) {
|
|
75
|
+
if (!Array.isArray(listObjects)) {
|
|
76
|
+
listObjects = [listObjects];
|
|
77
|
+
}
|
|
78
|
+
// Ensure str is a string
|
|
79
|
+
str = String(str);
|
|
80
|
+
// Remove characters from the beginning while they are in listObjects
|
|
81
|
+
while (str.length > 0 && listObjects.includes(str[0])) {
|
|
82
|
+
str = str.slice(1);
|
|
83
|
+
}
|
|
84
|
+
return str;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Removes characters from the end of the string
|
|
88
|
+
* if they are found in the list of characters.
|
|
89
|
+
*
|
|
90
|
+
* @param str - The input string.
|
|
91
|
+
* @param listObjects - A string or an array of characters to remove.
|
|
92
|
+
* @returns The modified string.
|
|
93
|
+
*/
|
|
94
|
+
export function eatOuter(str, listObjects) {
|
|
95
|
+
if (!Array.isArray(listObjects)) {
|
|
96
|
+
listObjects = [listObjects];
|
|
97
|
+
}
|
|
98
|
+
// Ensure str is a string
|
|
99
|
+
str = String(str);
|
|
100
|
+
// Remove characters from the end while they are in listObjects
|
|
101
|
+
while (str.length > 0 && listObjects.includes(str[str.length - 1])) {
|
|
102
|
+
str = str.slice(0, -1);
|
|
103
|
+
}
|
|
104
|
+
return str;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Removes characters from both the beginning and the end of the string
|
|
108
|
+
* if they are found in the list of characters.
|
|
109
|
+
*
|
|
110
|
+
* @param str - The input string.
|
|
111
|
+
* @param listObjects - A string or an array of characters to remove.
|
|
112
|
+
* @returns The modified string.
|
|
113
|
+
*/
|
|
114
|
+
export function eatAll(str, listObjects) {
|
|
115
|
+
return eatOuter(eatInner(str, listObjects), listObjects);
|
|
116
|
+
}
|
|
117
|
+
export function eatEnd(obj, endings = ['/']) {
|
|
118
|
+
let result = obj;
|
|
119
|
+
let modified = true;
|
|
120
|
+
while (modified) {
|
|
121
|
+
modified = false;
|
|
122
|
+
for (const ending of endings) {
|
|
123
|
+
if (result.endsWith(ending)) {
|
|
124
|
+
result = result.slice(0, -ending.length);
|
|
125
|
+
modified = true;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
export function tryParse(obj) {
|
|
133
|
+
try {
|
|
134
|
+
obj = JSON.stringify(obj);
|
|
135
|
+
}
|
|
136
|
+
catch (err) {
|
|
137
|
+
try {
|
|
138
|
+
obj = JSON.parse(obj);
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return obj;
|
|
144
|
+
}
|
|
145
|
+
export function create_list_string(array_obj) {
|
|
146
|
+
let string = '';
|
|
147
|
+
for (const obj in array_obj) {
|
|
148
|
+
const array_value = array_obj[obj];
|
|
149
|
+
const parsed_value = tryParse(array_value);
|
|
150
|
+
string += `${obj} == ${parsed_value}\n`;
|
|
151
|
+
}
|
|
152
|
+
return string;
|
|
153
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src';
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ensure_string, ensure_array } from './ensure_utils.js';
|
|
2
|
+
export function cleanText(input) {
|
|
3
|
+
// Replace delimiters with spaces and split
|
|
4
|
+
const str = ensure_string(input);
|
|
5
|
+
const words = str.replace(/[_.-]/g, ' '); // Replace _, -, . with space
|
|
6
|
+
return words;
|
|
7
|
+
}
|
|
8
|
+
export function cleanArray(obj) {
|
|
9
|
+
obj = ensure_array(obj);
|
|
10
|
+
return Array.from(new Set(obj));
|
|
11
|
+
}
|
|
12
|
+
export function getCleanArray(obj) {
|
|
13
|
+
obj = obj.split(/\s+/) // Split on any whitespace
|
|
14
|
+
.filter((item) => typeof item === 'string' && item !== '');
|
|
15
|
+
// Get basename
|
|
16
|
+
// Remove duplicates using Set
|
|
17
|
+
const uniqueWords = cleanArray(obj);
|
|
18
|
+
return uniqueWords;
|
|
19
|
+
}
|
|
20
|
+
// Cache a formatter instance for slightly better performance
|
|
21
|
+
const US_DECIMAL_FORMATTER = new Intl.NumberFormat('en-US', {
|
|
22
|
+
minimumFractionDigits: 2,
|
|
23
|
+
maximumFractionDigits: 2,
|
|
24
|
+
});
|
|
25
|
+
export function formatNumber(value) {
|
|
26
|
+
try {
|
|
27
|
+
// coerce to number
|
|
28
|
+
const num = Number(value);
|
|
29
|
+
// if it's NaN or infinite, bail out
|
|
30
|
+
if (!isFinite(num))
|
|
31
|
+
return value;
|
|
32
|
+
return US_DECIMAL_FORMATTER.format(num);
|
|
33
|
+
}
|
|
34
|
+
catch (_a) {
|
|
35
|
+
// if anything goes wrong, return the original
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
}
|