@egi/smart-log 1.0.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/smart-tools.js ADDED
@@ -0,0 +1,349 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __read = (this && this.__read) || function (o, n) {
14
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
15
+ if (!m) return o;
16
+ var i = m.call(o), r, ar = [], e;
17
+ try {
18
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
+ }
20
+ catch (error) { e = { error: error }; }
21
+ finally {
22
+ try {
23
+ if (r && !r.done && (m = i["return"])) m.call(i);
24
+ }
25
+ finally { if (e) throw e.error; }
26
+ }
27
+ return ar;
28
+ };
29
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
30
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
31
+ if (ar || !(i in from)) {
32
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
33
+ ar[i] = from[i];
34
+ }
35
+ }
36
+ return to.concat(ar || Array.prototype.slice.call(from));
37
+ };
38
+ var __values = (this && this.__values) || function(o) {
39
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
40
+ if (m) return m.call(o);
41
+ if (o && typeof o.length === "number") return {
42
+ next: function () {
43
+ if (o && i >= o.length) o = void 0;
44
+ return { value: o && o[i++], done: !o };
45
+ }
46
+ };
47
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
48
+ };
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.tools = exports.SmartTools = void 0;
51
+ var fs = require("node:fs");
52
+ // noinspection JSUnusedGlobalSymbols
53
+ var SmartTools = /** @class */ (function () {
54
+ function SmartTools() {
55
+ }
56
+ /**
57
+ * Captures the stack trace starting from the point just below the provided function.
58
+ *
59
+ * @param belowFn - A reference function to exclude from the stack trace.
60
+ * @returns An array of FrameData objects representing the captured stack trace.
61
+ */
62
+ SmartTools.prototype.getStackTrace = function (belowFn) {
63
+ var _a;
64
+ var myStackTrace = {};
65
+ var oldLimit = Error.stackTraceLimit;
66
+ Error.stackTraceLimit = Infinity;
67
+ var v8Handler = Error.prepareStackTrace;
68
+ Error.prepareStackTrace = function (_error, v8StackTrace) {
69
+ return v8StackTrace;
70
+ };
71
+ Error.captureStackTrace(myStackTrace, belowFn || this.getStackTrace);
72
+ // Extract the stack to avoid any changes by resetting operations below
73
+ var stack = (_a = myStackTrace.stack) !== null && _a !== void 0 ? _a : [];
74
+ Error.prepareStackTrace = v8Handler;
75
+ Error.stackTraceLimit = oldLimit;
76
+ return stack.map(function (callSite) {
77
+ return {
78
+ columnNumber: callSite.getColumnNumber(),
79
+ fileName: callSite.getFileName(),
80
+ functionName: callSite.getFunctionName(),
81
+ lineNumber: callSite.getLineNumber(),
82
+ methodName: callSite.getMethodName(),
83
+ native: callSite.isNative(),
84
+ typeName: callSite.getTypeName()
85
+ };
86
+ });
87
+ };
88
+ /**
89
+ * Parses an error's stack trace into an array of FrameData objects.
90
+ *
91
+ * @param err - An error object containing the stack trace.
92
+ * @returns An array of FrameData objects parsed from the error's stack trace.
93
+ */
94
+ SmartTools.prototype.parseStackTrace = function (err) {
95
+ var result = [];
96
+ if (err.stack) {
97
+ var lines = err.stack.split("\n").slice(1);
98
+ result = lines.map(function (line) {
99
+ var stackFrame = null;
100
+ if (line.match(/^\s*-{4,}$/)) {
101
+ stackFrame = {
102
+ fileName: line,
103
+ lineNumber: null,
104
+ functionName: null,
105
+ typeName: null,
106
+ methodName: null,
107
+ columnNumber: null,
108
+ native: null
109
+ };
110
+ }
111
+ else {
112
+ var matches = line.match(/at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/);
113
+ if (matches) {
114
+ var object = null;
115
+ var method = null;
116
+ var functionName = null;
117
+ var typeName = null;
118
+ var methodName = null;
119
+ var isNative = (matches[5] === "native");
120
+ if (matches[1]) {
121
+ functionName = matches[1];
122
+ var methodStart = functionName.lastIndexOf('.');
123
+ if (functionName[methodStart - 1] == ".")
124
+ methodStart--;
125
+ if (methodStart > 0) {
126
+ object = functionName.substring(0, methodStart);
127
+ method = functionName.substring(methodStart + 1);
128
+ var objectEnd = object.indexOf('.Module');
129
+ if (objectEnd > 0) {
130
+ functionName = functionName.substring(objectEnd + 1);
131
+ object = object.substring(0, objectEnd);
132
+ }
133
+ }
134
+ }
135
+ if (method) {
136
+ typeName = object;
137
+ methodName = method;
138
+ }
139
+ if (method === "<anonymous>") {
140
+ methodName = null;
141
+ functionName = null;
142
+ }
143
+ stackFrame = {
144
+ fileName: matches[2],
145
+ lineNumber: parseInt(matches[3], 10),
146
+ functionName: functionName,
147
+ typeName: typeName,
148
+ methodName: methodName,
149
+ columnNumber: parseInt(matches[4], 10),
150
+ "native": isNative
151
+ };
152
+ }
153
+ }
154
+ return stackFrame;
155
+ }, this).filter(function (frameData) {
156
+ return !!frameData;
157
+ });
158
+ }
159
+ return result;
160
+ };
161
+ /**
162
+ * Retrieves caller information from the stack trace, excluding specified files or patterns.
163
+ *
164
+ * @param {Error | (() => Error)} [errorOrStackProvider] - An error object or function returning an error,
165
+ * used to extract the stack trace. Defaults to capturing the current stack trace if omitted.
166
+ * @param options - Optional settings for filtering and formatting the caller information.
167
+ * @returns {string} A string with the format "fileName:lineNumber (functionName)",
168
+ * or "unknown location" if caller information cannot be determined.
169
+ */
170
+ SmartTools.prototype.getCallerFromStack = function (errorOrStackProvider, options) {
171
+ if (options === void 0) { options = {}; }
172
+ var stack;
173
+ if (errorOrStackProvider instanceof Error) {
174
+ stack = this.parseStackTrace(errorOrStackProvider);
175
+ }
176
+ else if (typeof errorOrStackProvider == "function") {
177
+ stack = this.parseStackTrace(errorOrStackProvider());
178
+ }
179
+ else {
180
+ stack = this.getStackTrace();
181
+ }
182
+ var ignoreFiles = [fs.realpathSync(__filename)];
183
+ if (options.ignoreFiles) {
184
+ ignoreFiles.push.apply(ignoreFiles, __spreadArray([], __read(options.ignoreFiles), false));
185
+ }
186
+ var frame = stack.find(function (f) {
187
+ var method = f.functionName;
188
+ return !ignoreFiles.includes(f.fileName) && !(method === null || method === void 0 ? void 0 : method.match(/stackprovider/i));
189
+ });
190
+ var callerLine;
191
+ if (frame) {
192
+ var method = frame.functionName;
193
+ var fileName = frame.fileName.replace("file://", "");
194
+ if (options.projectRoot) {
195
+ fileName = fileName.replace(options.projectRoot, ".");
196
+ }
197
+ if (method) {
198
+ callerLine = "".concat(fileName, ":").concat(frame.lineNumber, " (").concat(method, ")");
199
+ }
200
+ else {
201
+ callerLine = "".concat(fileName, ":").concat(frame.lineNumber);
202
+ }
203
+ }
204
+ else {
205
+ // Unlikely to happen unless the stack trace is empty or malformed
206
+ callerLine = "unknown location";
207
+ }
208
+ return callerLine;
209
+ };
210
+ SmartTools.prototype.kebabCase = function (s) {
211
+ return this.splitWords(s).join("-");
212
+ };
213
+ SmartTools.prototype.snakeCase = function (s) {
214
+ return this.splitWords(s).join("_");
215
+ };
216
+ SmartTools.prototype.camelCase = function (s) {
217
+ var ret;
218
+ if (s) {
219
+ var words = this.splitWords(s);
220
+ ret = words.shift();
221
+ if (words.length > 0) {
222
+ ret += words.map(function (w) { return w[0].toUpperCase() + w.substring(1); }).join("");
223
+ }
224
+ }
225
+ else {
226
+ ret = "";
227
+ }
228
+ return ret;
229
+ };
230
+ SmartTools.prototype.pascalCase = function (s) {
231
+ var ret;
232
+ if (s) {
233
+ var words = this.splitWords(s);
234
+ ret = words.map(function (w) {
235
+ if (w.length > 0) {
236
+ return w[0].toUpperCase() + w.substring(1);
237
+ }
238
+ else {
239
+ return "";
240
+ }
241
+ }).join("");
242
+ }
243
+ else {
244
+ ret = "";
245
+ }
246
+ return ret;
247
+ };
248
+ SmartTools.prototype.titleCase = function (s) {
249
+ var ret;
250
+ if (s) {
251
+ ret = this.splitWords(s).map(function (w) { return w[0].toUpperCase() + w.substring(1); }).join(" ");
252
+ }
253
+ else {
254
+ ret = "";
255
+ }
256
+ return ret;
257
+ };
258
+ SmartTools.prototype.constantCase = function (s) {
259
+ return this.snakeCase(s).toUpperCase();
260
+ };
261
+ SmartTools.prototype.clone = function (obj) {
262
+ return __assign({}, obj);
263
+ };
264
+ SmartTools.prototype.cloneDeep = function (obj) {
265
+ var e_1, _a;
266
+ var _this = this;
267
+ if (obj === null || typeof obj !== "object") {
268
+ return obj; // Return primitive values directly
269
+ }
270
+ if (Array.isArray(obj)) {
271
+ return obj.map(function (item) { return _this.cloneDeep(item); });
272
+ }
273
+ var clonedObj = {};
274
+ try {
275
+ for (var _b = __values(Object.keys(obj)), _c = _b.next(); !_c.done; _c = _b.next()) {
276
+ var key = _c.value;
277
+ clonedObj[key] = this.cloneDeep(obj[key]);
278
+ }
279
+ }
280
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
281
+ finally {
282
+ try {
283
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
284
+ }
285
+ finally { if (e_1) throw e_1.error; }
286
+ }
287
+ return clonedObj;
288
+ };
289
+ SmartTools.prototype.isSimpleObject = function (obj) {
290
+ return !!obj && obj.constructor === Object;
291
+ };
292
+ SmartTools.prototype.isObject = function (obj) {
293
+ return !!obj && obj === Object(obj);
294
+ };
295
+ SmartTools.prototype.isArray = function (obj) {
296
+ return Array.isArray(obj);
297
+ };
298
+ SmartTools.prototype.isString = function (obj) {
299
+ return typeof obj === "string" || obj instanceof String;
300
+ };
301
+ SmartTools.prototype.isNumber = function (obj) {
302
+ return typeof obj === 'number' && isFinite(obj);
303
+ };
304
+ SmartTools.prototype.isBoolean = function (obj) {
305
+ return typeof obj === "boolean";
306
+ };
307
+ SmartTools.prototype.isNull = function (obj) {
308
+ return obj === null;
309
+ };
310
+ SmartTools.prototype.isUndefined = function (obj) {
311
+ return obj === undefined;
312
+ };
313
+ SmartTools.prototype.isFunction = function (obj) {
314
+ return typeof obj === "function";
315
+ };
316
+ SmartTools.prototype.isDate = function (obj) {
317
+ return obj instanceof Date;
318
+ };
319
+ SmartTools.prototype.isRegExp = function (obj) {
320
+ return obj instanceof RegExp;
321
+ };
322
+ SmartTools.prototype.isError = function (obj) {
323
+ return obj instanceof Error;
324
+ };
325
+ SmartTools.prototype.isSymbol = function (obj) {
326
+ return typeof obj === "symbol";
327
+ };
328
+ SmartTools.prototype.isEmpty = function (obj) {
329
+ if (obj === null || obj === undefined) {
330
+ return true;
331
+ }
332
+ if (this.isString(obj) || this.isArray(obj)) {
333
+ return obj.length === 0;
334
+ }
335
+ if (this.isSimpleObject(obj)) {
336
+ return Object.keys(obj).length === 0;
337
+ }
338
+ return false;
339
+ };
340
+ SmartTools.prototype.isPrimitive = function (obj) {
341
+ return this.isString(obj) || this.isNumber(obj) || this.isBoolean(obj) || this.isNull(obj) || this.isUndefined(obj) || this.isSymbol(obj);
342
+ };
343
+ SmartTools.prototype.splitWords = function (s) {
344
+ return s.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/_/g, "-").toLowerCase().split("-");
345
+ };
346
+ return SmartTools;
347
+ }());
348
+ exports.SmartTools = SmartTools;
349
+ exports.tools = new SmartTools();
@@ -0,0 +1 @@
1
+ {"root":["../src/smart-error.ts","../src/smart-log-api.ts","../src/smart-log-date-utils.ts","../src/smart-log.ts","../src/smart-tools.ts"],"version":"5.6.3"}