@fullstackunicorn/initialize 1.0.2 → 1.0.5
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/index.js +33 -13
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -4,10 +4,13 @@ String.prototype.low = function () { return this.toLowerCase() }
|
|
|
4
4
|
String.prototype.cut = function (n = 1) { return this.slice(0, -n) }
|
|
5
5
|
|
|
6
6
|
class F3Error extends Error {
|
|
7
|
-
constructor(service, message, params
|
|
7
|
+
constructor(service, message, params, cause) {
|
|
8
8
|
super(message)
|
|
9
|
+
this.name = 'F3Error'
|
|
9
10
|
this.service = service
|
|
10
|
-
this.params = params
|
|
11
|
+
if (params) this.params = params
|
|
12
|
+
if (cause) this.cause = cause
|
|
13
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, F3Error)
|
|
11
14
|
}
|
|
12
15
|
}
|
|
13
16
|
|
|
@@ -15,6 +18,7 @@ globalThis.F3 = {}
|
|
|
15
18
|
|
|
16
19
|
globalThis.F3.service = ''
|
|
17
20
|
|
|
21
|
+
globalThis.F3.isErr = value => value instanceof Error
|
|
18
22
|
globalThis.F3.isArr = value => Array.isArray(value)
|
|
19
23
|
globalThis.F3.isNil = value => value === undefined || value === null
|
|
20
24
|
globalThis.F3.isFnc = value => typeof value === 'function'
|
|
@@ -42,7 +46,7 @@ globalThis.F3.obj.isGood = obj => !!obj && typeof obj === 'object' && Object.key
|
|
|
42
46
|
globalThis.F3.obj.safe = obj => (typeof obj === 'object' && obj !== null) ? obj : {}
|
|
43
47
|
globalThis.F3.obj.isPlain = obj => obj !== null && typeof obj === 'object' && Object.getPrototypeOf(obj) === Object.prototype
|
|
44
48
|
globalThis.F3.obj.inKb = obj => new TextEncoder().encode(JSON.stringify(obj)).length / 1000
|
|
45
|
-
globalThis.F3.obj.
|
|
49
|
+
globalThis.F3.obj.toStr = obj => {
|
|
46
50
|
if (typeof obj === 'string') return obj
|
|
47
51
|
try { return JSON.stringify(obj, null, 2) } catch { return String(obj) }
|
|
48
52
|
}
|
|
@@ -90,25 +94,41 @@ globalThis.F3.log.obj = (object = {}, ...rest) => {
|
|
|
90
94
|
const result = []
|
|
91
95
|
for (const item of rest) result.push(String(item))
|
|
92
96
|
for (const key of Object.keys(object)) {
|
|
93
|
-
try { result.push(`\n${key}:\n${F3.obj.
|
|
97
|
+
try { result.push(`\n${key}:\n${F3.obj.toStr(object[key])}\n`) }
|
|
94
98
|
catch { result.push(`\n${key}: [Error serializing value]\n`) }
|
|
95
99
|
}
|
|
96
100
|
console.log(result.join('\n'))
|
|
97
101
|
}
|
|
98
102
|
|
|
103
|
+
globalThis.F3.throw = (...args) => { throw new F3Error(F3.service, ...args) }
|
|
104
|
+
|
|
99
105
|
globalThis.F3.err = {}
|
|
100
106
|
globalThis.F3.err.is500 = status => !!status && status >= 500
|
|
101
107
|
globalThis.F3.err.is400 = status => !!status && status >= 400 && status < 500
|
|
102
108
|
|
|
109
|
+
globalThis.F3.err.toObj = err => {
|
|
110
|
+
const base = { name:err.name, message:err.message, stack:err.stack }
|
|
111
|
+
try { return Object.assign(base, Object.fromEntries(Object.entries(err)))}
|
|
112
|
+
catch { return base }
|
|
113
|
+
}
|
|
114
|
+
globalThis.F3.err.toStr = err => {
|
|
115
|
+
if (F3.isErr(err)) return F3.obj.toStr(F3.err.toObj(err))
|
|
116
|
+
if (F3.isObj(err)) return F3.obj.toStr(err)
|
|
117
|
+
if (F3.isStr(err)) return err
|
|
118
|
+
return String(err)
|
|
119
|
+
}
|
|
120
|
+
globalThis.F3.err.logs = err => {
|
|
121
|
+
if (err instanceof F3Error) console.error(F3.obj.toStr(err))
|
|
122
|
+
else console.error('non-F3-error',F3.obj.toStr(err))
|
|
123
|
+
}
|
|
124
|
+
|
|
103
125
|
globalThis.F3.catch = {}
|
|
104
|
-
globalThis.F3.catch.throw = (error,
|
|
105
|
-
if (
|
|
106
|
-
throw error
|
|
107
|
-
|
|
126
|
+
globalThis.F3.catch.throw = (error, message) => {
|
|
127
|
+
if (error instanceof F3Error) throw error
|
|
128
|
+
F3.throw(message, null, F3.err.toStr(error))
|
|
129
|
+
}
|
|
108
130
|
globalThis.F3.catch.log = (...args) => console.log(...args)
|
|
109
|
-
globalThis.F3.catch.res = (first,
|
|
110
|
-
if (
|
|
131
|
+
globalThis.F3.catch.res = (first, error) => {
|
|
132
|
+
if (error) F3.err.logs(error)
|
|
111
133
|
return first
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
globalThis.F3.throw = (message, params) => { throw new F3Error(F3.service, message, params) }
|
|
134
|
+
}
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.0.
|
|
2
|
+
"version": "1.0.5",
|
|
3
3
|
"name": "@fullstackunicorn/initialize",
|
|
4
4
|
"author": "lucanigido (https://fullstackunicorn.dev/author/lucanigido)",
|
|
5
5
|
"description": "A collection of global utility functions for JavaScript/Node.js projects (type checks, string helpers, logging, etc.). Side-effect import extends globals.",
|