@reldens/utils 0.40.0 → 0.42.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.
@@ -2,8 +2,6 @@
2
2
  *
3
3
  * Reldens - ErrorManager
4
4
  *
5
- * This module handle all the game errors.
6
- *
7
5
  */
8
6
 
9
7
  class ErrorManager
@@ -12,14 +10,18 @@ class ErrorManager
12
10
  constructor()
13
11
  {
14
12
  this.enableTrace = false;
13
+ this.callback = false;
15
14
  }
16
15
 
17
16
  error(message)
18
17
  {
19
- // @TODO - BETA.17: evaluate better ways to handle errors, implement email notifications, etc.
20
18
  if(this.enableTrace){
21
19
  console.trace();
22
20
  }
21
+ if('function' === typeof this.callback){
22
+ // use the callback to implement any customization here:
23
+ return this.callback(message);
24
+ }
23
25
  throw new Error(message);
24
26
  }
25
27
 
@@ -8,7 +8,7 @@
8
8
  * event will be referenced in a new property called "eventsByRemoveKey" so you can detach multiple listeners at the
9
9
  * time.
10
10
  *
11
- * - Then the normal "on" and "off" aliases were override to include a debug check with a log. Using the debug property
11
+ * - We override the "on" and "off" aliases override to include a debug check with a log. Using the debug property,
12
12
  * you can specify to log "all" the fire and listeners, or just by key. Note the key search will be applied in both
13
13
  * directions, it will check if the debug value exists in the event key or if the event key exists in the debug value.
14
14
  *
@@ -133,9 +133,9 @@ class AwaitEventEmitterExtended extends AwaitEventEmitter
133
133
  logDebugEvent(key, type)
134
134
  {
135
135
  if(
136
- this.debug.indexOf('all') !== -1
137
- || this.debug.indexOf(key) !== -1
138
- || key.indexOf(this.debug) !== -1
136
+ -1 !== this.debug.indexOf('all')
137
+ || -1 !== this.debug.indexOf(key)
138
+ || -1 !== key.indexOf(this.debug)
139
139
  ){
140
140
  Logger.debug(type+' Event:', key);
141
141
  }
@@ -2,8 +2,8 @@
2
2
  *
3
3
  * Reldens - InteractionArea
4
4
  *
5
- * This class is used to validate the area for a specific object or action, for example if player A target player B
6
- * by clicking on it and then run an action (by pressing space bar or using the action button), the action will
5
+ * This class is used to validate the area for a specific object or action. For example, if player A targets player B
6
+ * by clicking on it and then runs an action (by pressing space bar or using the action button), the action will
7
7
  * be only valid and executed if player A is inside the interactive area of player B.
8
8
  *
9
9
  */
@@ -21,11 +21,11 @@ class InteractionArea
21
21
 
22
22
  setupInteractionArea(margin = false, x = false, y = false)
23
23
  {
24
- // interaction area can be forced by parameter:
24
+ // the margin parameter can force the interaction area:
25
25
  if(margin){
26
26
  this.interactionArea = margin;
27
27
  }
28
- // if there's none interaction area just do nothing:
28
+ // if there's none interaction area, just do nothing:
29
29
  if(!this.interactionArea){
30
30
  return;
31
31
  }
@@ -51,10 +51,7 @@ class InteractionArea
51
51
 
52
52
  getPosition()
53
53
  {
54
- return {
55
- x: this.x,
56
- y: this.y
57
- }
54
+ return {x: this.x, y: this.y}
58
55
  }
59
56
 
60
57
  }
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){
@@ -2,18 +2,20 @@
2
2
  *
3
3
  * Reldens - SchemaValidator
4
4
  *
5
- * This module is to validate objects properties at runtime.
5
+ * This module is to validate the object properties at runtime.
6
6
  *
7
7
  */
8
8
 
9
+ const ValidatorInterface = require('./validator-interface');
9
10
  const sc = require('./shortcuts');
10
11
  const Logger = require('./logger');
11
12
 
12
- class SchemaValidator
13
+ class SchemaValidator extends ValidatorInterface
13
14
  {
14
15
 
15
16
  constructor(schema)
16
17
  {
18
+ super(schema);
17
19
  this.schema = schema;
18
20
  }
19
21
 
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)
@@ -427,6 +427,37 @@ class Shortcuts
427
427
  return isoLanguageCodeRegex.test(isoCode);
428
428
  }
429
429
 
430
+ sanitize(input)
431
+ {
432
+ if(!input){
433
+ return '';
434
+ }
435
+ return input
436
+ .replace(/&(?![a-zA-Z0-9#]+;)/g, '&')
437
+ .replace(/</g, '&lt;')
438
+ .replace(/>/g, '&gt;')
439
+ .replace(/"/g, '&quot;')
440
+ .replace(/'/g, '&#x27;')
441
+ .replace(/\//g, '&#x2F;')
442
+ .replace(/`/g, '&#x60;');
443
+ }
444
+
445
+ sanitizeUrl(url)
446
+ {
447
+ if(!url){
448
+ return '';
449
+ }
450
+ let sanitized = url.trim();
451
+ let urlPattern = /^(?:https?:\/\/|mailto:|tel:|#|\/|\.\/|\.\.\/)/i;
452
+ if(!urlPattern.test(sanitized)){
453
+ return '';
454
+ }
455
+ return sanitized
456
+ .replace(/javascript:/gi, '')
457
+ .replace(/data:/gi, '')
458
+ .replace(/vbscript:/gi, '');
459
+ }
460
+
430
461
  }
431
462
 
432
463
  module.exports = new Shortcuts();
@@ -0,0 +1,17 @@
1
+ /**
2
+ *
3
+ * Reldens - ValidatorInterface
4
+ *
5
+ */
6
+
7
+ class ValidatorInterface
8
+ {
9
+
10
+ validate()
11
+ {
12
+ return true;
13
+ }
14
+
15
+ }
16
+
17
+ module.exports.ValidatorInterface = ValidatorInterface;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/utils",
3
3
  "scope": "@reldens",
4
- "version": "0.40.0",
4
+ "version": "0.42.0",
5
5
  "description": "Reldens - Utils",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",