@reldens/utils 0.51.0 → 0.52.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/README.md +101 -1
- package/lib/logger.js +24 -57
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# Reldens - Utils
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Features
|
|
6
6
|
|
|
7
7
|
- Interaction area calculation helper.
|
|
8
8
|
- Page range provider helper.
|
|
@@ -13,6 +13,106 @@
|
|
|
13
13
|
- Error Manager.
|
|
14
14
|
- Events Manager and Events Manager Singleton.
|
|
15
15
|
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Usage examples
|
|
19
|
+
|
|
20
|
+
### EventsManager
|
|
21
|
+
Advanced event emitter with features like event sanitization, memory leak detection, key-based event management, and support for async/sync execution.
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
const { EventsManager } = require('@reldens/utils');
|
|
25
|
+
let eventsManager = new EventsManager();
|
|
26
|
+
|
|
27
|
+
// Listen to events
|
|
28
|
+
eventsManager.on('userLogin', (userData) => {
|
|
29
|
+
console.log('User logged in:', userData.username);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Emit events
|
|
33
|
+
eventsManager.emit('userLogin', { username: 'player1', id: 123 });
|
|
34
|
+
|
|
35
|
+
// Listen with unique keys
|
|
36
|
+
eventsManager.onWithKey('gameStart', callback, 'uniqueKey');
|
|
37
|
+
eventsManager.offWithKey('uniqueKey'); // Remove by key
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### InteractionArea
|
|
41
|
+
Spatial interaction validation helper that determines if objects are within interaction range based on coordinates and area margins.
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
const { InteractionArea } = require('@reldens/utils');
|
|
45
|
+
let interactionArea = new InteractionArea();
|
|
46
|
+
|
|
47
|
+
// Setup interaction area
|
|
48
|
+
interactionArea.setupInteractionArea(50, 100, 200); // margin=50, x=100, y=200
|
|
49
|
+
|
|
50
|
+
// Validate interaction
|
|
51
|
+
let isValid = interactionArea.isValidInteraction(120, 210); // true
|
|
52
|
+
let isInvalid = interactionArea.isValidInteraction(200, 300); // false
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Logger
|
|
56
|
+
Advanced logging utility with multiple log levels, custom levels support, active level filtering, and environment-based configuration.
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
const { Logger } = require('@reldens/utils');
|
|
60
|
+
|
|
61
|
+
// Standard logging
|
|
62
|
+
Logger.info('Application started');
|
|
63
|
+
Logger.error('Database connection failed', errorDetails);
|
|
64
|
+
Logger.debug('Processing user request', requestData);
|
|
65
|
+
|
|
66
|
+
// Custom levels
|
|
67
|
+
Logger.activeLogLevels = [3, 4, 10]; // Only critical, error, and custom level 10
|
|
68
|
+
Logger.customLevels = [10, 11, 12];
|
|
69
|
+
Logger.log(10, 'custom', 'Custom log message');
|
|
70
|
+
|
|
71
|
+
// Configuration
|
|
72
|
+
Logger.setForcedDisabled(false);
|
|
73
|
+
Logger.setAddTimeStamp(true);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### PageRangeProvider
|
|
77
|
+
Pagination helper that calculates page ranges for UI components, handling first/last page navigation and customizable display options.
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
const { PageRangeProvider } = require('@reldens/utils');
|
|
81
|
+
let pageProvider = new PageRangeProvider();
|
|
82
|
+
|
|
83
|
+
// Generate page range
|
|
84
|
+
let range = pageProvider.fetch(5, 20, 7, 'First', 'Last');
|
|
85
|
+
// Returns: [{label: 'First', value: 1}, {label: 2, value: 2}, ...]
|
|
86
|
+
|
|
87
|
+
// Simple range
|
|
88
|
+
let simpleRange = pageProvider.fetch(3, 10, 5);
|
|
89
|
+
// Returns pages around current page 3
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### SchemaValidator
|
|
93
|
+
Object validation utility that validates object properties against defined schemas with support for nested objects and custom validation rules.
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
const { SchemaValidator } = require('@reldens/utils');
|
|
97
|
+
|
|
98
|
+
let schema = {
|
|
99
|
+
username: { type: 'string', min: 3, max: 20 },
|
|
100
|
+
age: { type: 'int', min: 18 },
|
|
101
|
+
profile: {
|
|
102
|
+
type: 'object',
|
|
103
|
+
nested: {
|
|
104
|
+
email: { type: 'string', required: true }
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
let validator = new SchemaValidator(schema);
|
|
110
|
+
let userData = { username: 'player1', age: 25, profile: { email: 'test@example.com' } };
|
|
111
|
+
let isValid = validator.validate(userData);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
16
116
|
Need something specific?
|
|
17
117
|
|
|
18
118
|
[Request a feature here: https://www.reldens.com/features-request](https://www.reldens.com/features-request)
|
package/lib/logger.js
CHANGED
|
@@ -32,6 +32,8 @@ class Logger
|
|
|
32
32
|
this.maxLogArgLength = sc.get(context, 'RELDENS_LOGS_MAX_ARGUMENT_LENGTH', 0);
|
|
33
33
|
this.maxStackTraceLength = sc.get(context, 'RELDENS_LOGS_MAX_STACK_TRACE_LENGTH', 0);
|
|
34
34
|
this.applySanitizer = Boolean(sc.get(context, 'RELDENS_LOGS_APPLY_SANITIZER', false));
|
|
35
|
+
this.activeLogLevels = [];
|
|
36
|
+
this.customLevels = [];
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
context()
|
|
@@ -91,8 +93,21 @@ class Logger
|
|
|
91
93
|
return (this.context().RELDENS_ENABLE_TRACE_FOR || '').split(',');
|
|
92
94
|
}
|
|
93
95
|
|
|
94
|
-
log(levelLabel, ...args)
|
|
96
|
+
log(levelNumber, levelLabel, ...args)
|
|
95
97
|
{
|
|
98
|
+
if(this.forcedDisabled){
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
if(0 < this.activeLogLevels.length){
|
|
102
|
+
if(-1 === this.activeLogLevels.indexOf(levelNumber)){
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if(0 === this.activeLogLevels.length){
|
|
107
|
+
if(levelNumber > this.logLevel()){
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
96
111
|
let date = !this.addTimeStamp ? '' : (new Date()).toISOString().slice(0, 19).replace('T', ' ')+' - ';
|
|
97
112
|
let processedArgs = args;
|
|
98
113
|
if(this.applySanitizer){
|
|
@@ -125,90 +140,42 @@ class Logger
|
|
|
125
140
|
|
|
126
141
|
debug(...args)
|
|
127
142
|
{
|
|
128
|
-
|
|
129
|
-
return this;
|
|
130
|
-
}
|
|
131
|
-
if(8 > this.logLevel()){
|
|
132
|
-
return this;
|
|
133
|
-
}
|
|
134
|
-
return this.log('debug', ...args);
|
|
143
|
+
return this.log(8, 'debug', ...args);
|
|
135
144
|
}
|
|
136
145
|
|
|
137
146
|
info(...args)
|
|
138
147
|
{
|
|
139
|
-
|
|
140
|
-
return this;
|
|
141
|
-
}
|
|
142
|
-
if(7 > this.logLevel()){
|
|
143
|
-
return this;
|
|
144
|
-
}
|
|
145
|
-
return this.log('info', ...args);
|
|
148
|
+
return this.log(7, 'info', ...args);
|
|
146
149
|
}
|
|
147
150
|
|
|
148
151
|
notice(...args)
|
|
149
152
|
{
|
|
150
|
-
|
|
151
|
-
return this;
|
|
152
|
-
}
|
|
153
|
-
if(6 > this.logLevel()){
|
|
154
|
-
return this;
|
|
155
|
-
}
|
|
156
|
-
return this.log('notice', ...args);
|
|
153
|
+
return this.log(6, 'notice', ...args);
|
|
157
154
|
}
|
|
158
155
|
|
|
159
156
|
warning(...args)
|
|
160
157
|
{
|
|
161
|
-
|
|
162
|
-
return this;
|
|
163
|
-
}
|
|
164
|
-
if(5 > this.logLevel()){
|
|
165
|
-
return this;
|
|
166
|
-
}
|
|
167
|
-
return this.log('warning', ...args);
|
|
158
|
+
return this.log(5, 'warning', ...args);
|
|
168
159
|
}
|
|
169
160
|
|
|
170
161
|
error(...args)
|
|
171
162
|
{
|
|
172
|
-
|
|
173
|
-
return this;
|
|
174
|
-
}
|
|
175
|
-
if(4 > this.logLevel()){
|
|
176
|
-
return this;
|
|
177
|
-
}
|
|
178
|
-
return this.log('error', ...args);
|
|
163
|
+
return this.log(4, 'error', ...args);
|
|
179
164
|
}
|
|
180
165
|
|
|
181
166
|
critical(...args)
|
|
182
167
|
{
|
|
183
|
-
|
|
184
|
-
return this;
|
|
185
|
-
}
|
|
186
|
-
if(3 > this.logLevel()){
|
|
187
|
-
return this;
|
|
188
|
-
}
|
|
189
|
-
return this.log('critical', ...args);
|
|
168
|
+
return this.log(3, 'critical', ...args);
|
|
190
169
|
}
|
|
191
170
|
|
|
192
171
|
alert(...args)
|
|
193
172
|
{
|
|
194
|
-
|
|
195
|
-
return this;
|
|
196
|
-
}
|
|
197
|
-
if(2 > this.logLevel()){
|
|
198
|
-
return this;
|
|
199
|
-
}
|
|
200
|
-
return this.log('alert', ...args);
|
|
173
|
+
return this.log(2, 'alert', ...args);
|
|
201
174
|
}
|
|
202
175
|
|
|
203
176
|
emergency(...args)
|
|
204
177
|
{
|
|
205
|
-
|
|
206
|
-
return this;
|
|
207
|
-
}
|
|
208
|
-
if(1 > this.logLevel()){
|
|
209
|
-
return this;
|
|
210
|
-
}
|
|
211
|
-
return this.log('emergency', ...args);
|
|
178
|
+
return this.log(1, 'emergency', ...args);
|
|
212
179
|
}
|
|
213
180
|
|
|
214
181
|
}
|