@fullstackunicorn/initialize 1.0.1 → 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.
Files changed (2) hide show
  1. package/index.js +39 -10
  2. package/package.json +17 -14
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.string = 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
  }
@@ -62,6 +66,12 @@ globalThis.F3.replacer = (string = '', values = {}) => {
62
66
  return newString
63
67
  }
64
68
 
69
+ globalThis.F3.promise = async (func, data) => {
70
+ return new Promise((resolve, reject) => {
71
+ func(data, (error, output) => { if (error) reject(error); else resolve(output) })
72
+ })
73
+ }
74
+
65
75
  globalThis.F3.res = {}
66
76
  globalThis.F3.res.isGood = res => {
67
77
  if (F3.isNil(res)) return false
@@ -84,22 +94,41 @@ globalThis.F3.log.obj = (object = {}, ...rest) => {
84
94
  const result = []
85
95
  for (const item of rest) result.push(String(item))
86
96
  for (const key of Object.keys(object)) {
87
- try { result.push(`\n${key}:\n${F3.obj.string(object[key])}\n`) }
97
+ try { result.push(`\n${key}:\n${F3.obj.toStr(object[key])}\n`) }
88
98
  catch { result.push(`\n${key}: [Error serializing value]\n`) }
89
99
  }
90
100
  console.log(result.join('\n'))
91
101
  }
92
102
 
103
+ globalThis.F3.throw = (...args) => { throw new F3Error(F3.service, ...args) }
104
+
93
105
  globalThis.F3.err = {}
94
106
  globalThis.F3.err.is500 = status => !!status && status >= 500
95
107
  globalThis.F3.err.is400 = status => !!status && status >= 400 && status < 500
96
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
+
97
125
  globalThis.F3.catch = {}
98
- globalThis.F3.catch.throw = error => { throw error }
126
+ globalThis.F3.catch.throw = (error, message) => {
127
+ if (error instanceof F3Error) throw error
128
+ F3.throw(message, null, F3.err.toStr(error))
129
+ }
99
130
  globalThis.F3.catch.log = (...args) => console.log(...args)
100
- globalThis.F3.catch.res = (first, ...rest) => {
101
- if (rest.length > 0) console.log(...rest)
131
+ globalThis.F3.catch.res = (first, error) => {
132
+ if (error) F3.err.logs(error)
102
133
  return first
103
- }
104
-
105
- globalThis.F3.throw = (message, params) => { throw new F3Error(F3.service, message, params) }
134
+ }
package/package.json CHANGED
@@ -1,22 +1,12 @@
1
1
  {
2
+ "version": "1.0.5",
2
3
  "name": "@fullstackunicorn/initialize",
3
- "author": "lucanigido[](https://fullstackunicorn.dev/author/lucanigido)",
4
- "version": "1.0.1",
4
+ "author": "lucanigido (https://fullstackunicorn.dev/author/lucanigido)",
5
+ "description": "A collection of global utility functions for JavaScript/Node.js projects (type checks, string helpers, logging, etc.). Side-effect import extends globals.",
6
+ "readme": "README.md",
5
7
  "main": "index.js",
6
8
  "type": "module",
7
9
  "license": "MIT",
8
- "private": false,
9
- "description": "A collection of global utility functions for JavaScript/Node.js projects (type checks, string helpers, logging, etc.). Side-effect import extends globals.",
10
- "readme": "README.md",
11
- "keywords": [
12
- "utilities",
13
- "helpers",
14
- "global",
15
- "javascript",
16
- "node",
17
- "type-checks",
18
- "logging"
19
- ],
20
10
  "repository": {
21
11
  "type": "git",
22
12
  "url": "https://gitlab.com/fullstackunicorn/initialize.git"
@@ -27,6 +17,19 @@
27
17
  "sideEffects": [
28
18
  "index.js"
29
19
  ],
20
+ "keywords": [
21
+ "utilities",
22
+ "helpers",
23
+ "global",
24
+ "javascript",
25
+ "node",
26
+ "type-checks",
27
+ "logging"
28
+ ],
29
+ "imports": {
30
+ "#@": "./",
31
+ "#@/*": "./*"
32
+ },
30
33
  "scripts": {
31
34
  "test": "echo \"Error: no test specified\" && exit 1"
32
35
  }