@putkoff/abstract-utilities 0.1.171 → 0.1.172
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 +54 -13
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +53 -14
- package/dist/esm/index.js.map +1 -1
- package/dist/functions/safe_utils/src/index.d.ts +3 -2
- package/dist/functions/safe_utils/src/safe_globals.d.ts +7 -0
- package/dist/functions/safe_utils/src/safe_storage.d.ts +21 -0
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -4,6 +4,19 @@ var react = require('react');
|
|
|
4
4
|
var jsxRuntime = require('react/jsx-runtime');
|
|
5
5
|
|
|
6
6
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
7
|
+
function getSafeDocument() {
|
|
8
|
+
return typeof document !== 'undefined' ? document : undefined;
|
|
9
|
+
}
|
|
10
|
+
function getDocumentProp(...keys) {
|
|
11
|
+
let obj = getSafeDocument();
|
|
12
|
+
for (const k of keys) {
|
|
13
|
+
if (obj == null || typeof obj !== 'object')
|
|
14
|
+
return undefined;
|
|
15
|
+
obj = obj[k];
|
|
16
|
+
}
|
|
17
|
+
return obj;
|
|
18
|
+
}
|
|
19
|
+
|
|
7
20
|
/**
|
|
8
21
|
* Returns `window` if running in a browser, otherwise `undefined`.
|
|
9
22
|
*/
|
|
@@ -39,6 +52,45 @@ function callStorage(method, ...args) {
|
|
|
39
52
|
return undefined;
|
|
40
53
|
}
|
|
41
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Safely call localStorage / sessionStorage / any Storage API.
|
|
57
|
+
*
|
|
58
|
+
* @param storageName "localStorage" or "sessionStorage"
|
|
59
|
+
* @param method one of the Storage methods, e.g. "getItem", "setItem", etc.
|
|
60
|
+
* @param args arguments to pass along (e.g. key, value)
|
|
61
|
+
* @returns whatever the underlying method returns, or undefined if unavailable/fails
|
|
62
|
+
*/
|
|
63
|
+
function safeStorage(storageName, method, ...args) {
|
|
64
|
+
try {
|
|
65
|
+
const storage = globalThis[storageName];
|
|
66
|
+
if (!storage)
|
|
67
|
+
return undefined;
|
|
68
|
+
const fn = storage[method];
|
|
69
|
+
if (typeof fn !== "function")
|
|
70
|
+
return undefined;
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
return fn.apply(storage, args);
|
|
73
|
+
}
|
|
74
|
+
catch (_a) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Safely walk `window` or `document` to grab nested props without blowing up in Node.
|
|
81
|
+
*
|
|
82
|
+
* @param rootName "window" or "document"
|
|
83
|
+
* @param path sequence of property names, e.g. ["location","host"] or ["baseURI"]
|
|
84
|
+
*/
|
|
85
|
+
function safeGlobalProp(rootName, ...path) {
|
|
86
|
+
let obj = globalThis[rootName];
|
|
87
|
+
for (const key of path) {
|
|
88
|
+
if (obj == null)
|
|
89
|
+
return undefined;
|
|
90
|
+
obj = obj[key];
|
|
91
|
+
}
|
|
92
|
+
return obj;
|
|
93
|
+
}
|
|
42
94
|
|
|
43
95
|
/**
|
|
44
96
|
* Returns the global window object if it exists, otherwise undefined.
|
|
@@ -83,19 +135,6 @@ function getWindowHost() {
|
|
|
83
135
|
return getWindowProp('location', 'host');
|
|
84
136
|
}
|
|
85
137
|
|
|
86
|
-
function getSafeDocument() {
|
|
87
|
-
return typeof document !== 'undefined' ? document : undefined;
|
|
88
|
-
}
|
|
89
|
-
function getDocumentProp(...keys) {
|
|
90
|
-
let obj = getSafeDocument();
|
|
91
|
-
for (const k of keys) {
|
|
92
|
-
if (obj == null || typeof obj !== 'object')
|
|
93
|
-
return undefined;
|
|
94
|
-
obj = obj[k];
|
|
95
|
-
}
|
|
96
|
-
return obj;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
138
|
/**
|
|
100
139
|
***Changes**:
|
|
101
140
|
*- Updated import path for `InputProps` to `../../types/interfaces`.
|
|
@@ -1583,6 +1622,8 @@ exports.readFileContents = readFileContents;
|
|
|
1583
1622
|
exports.readJsonFile = readJsonFile;
|
|
1584
1623
|
exports.requestPatch = requestPatch;
|
|
1585
1624
|
exports.requireToken = requireToken;
|
|
1625
|
+
exports.safeGlobalProp = safeGlobalProp;
|
|
1626
|
+
exports.safeStorage = safeStorage;
|
|
1586
1627
|
exports.sanitizeFilename = sanitizeFilename;
|
|
1587
1628
|
exports.secureFetchIt = secureFetchIt;
|
|
1588
1629
|
exports.stripPrefixes = stripPrefixes;
|