@reldens/utils 0.39.0 → 0.41.0

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/lib/logger.js CHANGED
@@ -9,7 +9,7 @@ class Logger
9
9
 
10
10
  logLevels = {
11
11
  none: 0,
12
- emergency: 1, // system is unusable
12
+ emergency: 1, // the system is unusable
13
13
  alert: 2, // action must be taken immediately
14
14
  critical: 3, // critical conditions
15
15
  error: 4, // error conditions
@@ -29,6 +29,7 @@ class Logger
29
29
  this.logLevelBack = 3;
30
30
  this.forcedDisabled = Boolean(context.RELDENS_FORCED_DISABLED_LOGS || false);
31
31
  this.addTimeStamp = Boolean(context.RELDENS_INCLUDE_LOGS_TIMESTAMP || true);
32
+ this.callback = false;
32
33
  }
33
34
 
34
35
  context()
@@ -93,19 +94,27 @@ class Logger
93
94
  log(levelLabel, ...args)
94
95
  {
95
96
  let date = !this.addTimeStamp ? '' : (new Date()).toISOString().slice(0, 19).replace('T', ' ')+' - ';
96
- console.log(date+levelLabel.toUpperCase()+' -', ...args);
97
+ this.logWithCallback(date+levelLabel.toUpperCase()+' -', ...args);
97
98
  if(-1 !== this.enableTraceFor().indexOf('all') || -1 !== this.enableTraceFor().indexOf(levelLabel)){
98
99
  if('function' !== typeof Error?.captureStackTrace){
99
- console.log('Error.captureStackTrace is not available.', typeof Error?.captureStackTrace);
100
+ this.logWithCallback('Error.captureStackTrace is not available.', typeof Error?.captureStackTrace);
100
101
  return this;
101
102
  }
102
103
  let stackHolder = {};
103
104
  Error.captureStackTrace(stackHolder, levelLabel);
104
- console.log(stackHolder.stack);
105
+ this.logWithCallback(stackHolder.stack);
105
106
  }
106
107
  return this;
107
108
  }
108
109
 
110
+ logWithCallback(...args)
111
+ {
112
+ if('function' === typeof this.callback){
113
+ this.callback(...args);
114
+ }
115
+ console.log(...args);
116
+ }
117
+
109
118
  debug(...args)
110
119
  {
111
120
  if(this.forcedDisabled){
package/lib/shortcuts.js CHANGED
@@ -49,7 +49,7 @@ class Shortcuts
49
49
 
50
50
  isFunction(callback)
51
51
  {
52
- return callback && 'function' === typeof callback;
52
+ return (callback && 'function' === typeof callback);
53
53
  }
54
54
 
55
55
  isObjectFunction(obj, property)
@@ -115,6 +115,16 @@ class Shortcuts
115
115
  return Object.keys(obj).length;
116
116
  }
117
117
 
118
+ startsWith(term, startWith)
119
+ {
120
+ return this.isString(term) && 0 === term.indexOf(startWith);
121
+ }
122
+
123
+ contains(term, needle)
124
+ {
125
+ return this.isString(term) && -1 !== term.indexOf(needle);
126
+ }
127
+
118
128
  convertObjectsArrayToObjectByKeys(dataArray, keyProperty)
119
129
  {
120
130
  if(!this.isArray(dataArray) || 0 === dataArray.length){
@@ -165,18 +175,23 @@ class Shortcuts
165
175
  return this.parseJson(jsonString) || defaultReturn;
166
176
  }
167
177
 
168
- parseJson(jsonString)
178
+ parseJson(jsonString, defaultReturn = false)
169
179
  {
170
180
  try {
171
181
  return JSON.parse(jsonString);
172
182
  } catch (e) {
173
- return false;
183
+ return defaultReturn;
174
184
  }
175
185
  }
176
186
 
177
187
  deepJsonClone(obj)
178
188
  {
179
- return JSON.parse(JSON.stringify(obj));
189
+ return JSON.parse(this.toJsonString(obj));
190
+ }
191
+
192
+ toJsonString(obj)
193
+ {
194
+ return JSON.stringify(obj);
180
195
  }
181
196
 
182
197
  get(obj, prop, defaultReturn)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/utils",
3
3
  "scope": "@reldens",
4
- "version": "0.39.0",
4
+ "version": "0.41.0",
5
5
  "description": "Reldens - Utils",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",