@reldens/utils 0.53.0 → 0.54.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.
Files changed (3) hide show
  1. package/CLAUDE.md +87 -0
  2. package/README.md +93 -6
  3. package/package.json +1 -1
package/CLAUDE.md ADDED
@@ -0,0 +1,87 @@
1
+ # CLAUDE.md
2
+
3
+ This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
+
5
+ ## Package Overview
6
+
7
+ **@reldens/utils** is a core utility package used throughout the Reldens ecosystem. It provides essential utilities for:
8
+ - Object manipulation (Shortcuts class)
9
+ - Event management (EventsManager)
10
+ - Logging (Logger)
11
+ - Schema validation (SchemaValidator)
12
+ - Environment variables (EnvVar)
13
+ - Error handling (ErrorManager)
14
+ - Interaction area validation (InteractionArea)
15
+ - Pagination utilities (PageRangeProvider)
16
+ - Validator base interface (ValidatorInterface)
17
+
18
+ ## Key Commands
19
+
20
+ ```bash
21
+ # Run tests
22
+ npm test
23
+ ```
24
+
25
+ ## Architecture
26
+
27
+ ### Core Classes
28
+
29
+ **Shortcuts** (`lib/shortcuts.js`):
30
+ - Provides utility methods for object manipulation, property access, and validation
31
+ - Imported as `sc` throughout the Reldens codebase
32
+ - Key methods: `get`, `hasOwn`, `isObject`, `isArray`, `deepMerge`, `deepClone`, etc.
33
+ - Used instead of direct property access or Object.prototype methods
34
+
35
+ **EventsManager** (`lib/events-manager.js`):
36
+ - Singleton event system used across Reldens
37
+ - Supports both sync (`emitSync`) and async (`emit`) events
38
+ - Allows event listeners to modify data via reference
39
+ - Debug mode available via `debug` property
40
+
41
+ **Logger** (`lib/logger.js`):
42
+ - Centralized logging utility with multiple log levels
43
+ - Levels: emergency, alert, critical, error, warning, notice, info, debug
44
+ - Configurable log level and trace options
45
+ - Used instead of console.log
46
+
47
+ **SchemaValidator** (`lib/schema-validator.js`):
48
+ - JSON schema validation utility
49
+ - Validates data structures against defined schemas
50
+ - Returns validation results with errors
51
+
52
+ **EnvVar** (`lib/env-var.js`):
53
+ - Environment variable utilities
54
+ - Type conversion and validation
55
+ - Safe environment variable access
56
+
57
+ **ErrorManager** (`lib/error-manager.js`):
58
+ - Singleton error handling utility
59
+ - Configurable error tracing via `enableTrace` property
60
+ - Supports custom error callbacks via `callback` property
61
+ - Default behavior throws Error with a message
62
+
63
+ **InteractionArea** (`lib/interaction-area.js`):
64
+ - Validates if positions are within interaction range
65
+ - Used for player-to-player, player-to-object interactions
66
+ - Configurable interaction area/margin
67
+ - Key methods: `setupInteractionArea`, `isValidInteraction`, `getPosition`
68
+
69
+ **PageRangeProvider** (`lib/page-range-provider.js`):
70
+ - Singleton pagination utility
71
+ - Generates page ranges for UI pagination
72
+ - Key method: `fetch(page, totalPages, totalDisplayedPages, firstLabel, lastLabel)`
73
+ - Returns array of page objects with label and value
74
+
75
+ **ValidatorInterface** (`lib/validator-interface.js`):
76
+ - Base interface for validator implementations
77
+ - Provides default `validate()` method that returns true
78
+ - Extend this class to create custom validators
79
+
80
+ ## Important Notes
81
+
82
+ - This package has NO external dependencies
83
+ - All code must be pure JavaScript with no dependencies
84
+ - The Shortcuts class is used extensively across Reldens - any changes affect the entire ecosystem
85
+ - Singleton pattern is used for: ErrorManager, PageRangeProvider, and EventsManagerSingleton (exported alongside the EventsManager class)
86
+ - Logger methods should be used instead of console.log everywhere in Reldens
87
+ - InteractionArea is instantiable (not a singleton) - create instances for each interactive object
package/README.md CHANGED
@@ -78,14 +78,13 @@ Pagination helper that calculates page ranges for UI components, handling first/
78
78
 
79
79
  ```javascript
80
80
  const { PageRangeProvider } = require('@reldens/utils');
81
- let pageProvider = new PageRangeProvider();
82
81
 
83
- // Generate page range
84
- let range = pageProvider.fetch(5, 20, 7, 'First', 'Last');
82
+ // Generate page range (it's a singleton, use directly)
83
+ let range = PageRangeProvider.fetch(5, 20, 7, 'First', 'Last');
85
84
  // Returns: [{label: 'First', value: 1}, {label: 2, value: 2}, ...]
86
85
 
87
86
  // Simple range
88
- let simpleRange = pageProvider.fetch(3, 10, 5);
87
+ let simpleRange = PageRangeProvider.fetch(3, 10, 5);
89
88
  // Returns pages around current page 3
90
89
  ```
91
90
 
@@ -98,8 +97,8 @@ const { SchemaValidator } = require('@reldens/utils');
98
97
  let schema = {
99
98
  username: { type: 'string', min: 3, max: 20 },
100
99
  age: { type: 'int', min: 18 },
101
- profile: {
102
- type: 'object',
100
+ profile: {
101
+ type: 'object',
103
102
  nested: {
104
103
  email: { type: 'string', required: true }
105
104
  }
@@ -111,6 +110,94 @@ let userData = { username: 'player1', age: 25, profile: { email: 'test@example.c
111
110
  let isValid = validator.validate(userData);
112
111
  ```
113
112
 
113
+ ### Shortcuts (sc)
114
+ Core utility class providing essential object manipulation, property access, and validation methods used throughout Reldens.
115
+
116
+ ```javascript
117
+ const { sc } = require('@reldens/utils');
118
+
119
+ // Safe property access
120
+ let value = sc.get(obj, 'nested.property.path', 'defaultValue');
121
+
122
+ // Check property ownership
123
+ if (sc.hasOwn(obj, 'propertyName')) {
124
+ // Property exists
125
+ }
126
+
127
+ // Type checking
128
+ sc.isObject(value); // true/false
129
+ sc.isArray(value); // true/false
130
+ sc.isFunction(value); // true/false
131
+
132
+ // Deep operations
133
+ let cloned = sc.deepClone(originalObject);
134
+ let merged = sc.deepMerge(obj1, obj2);
135
+
136
+ // Property manipulation
137
+ sc.getDef(obj, 'key', 'default'); // Get with default
138
+ sc.getOneDef(obj, ['key1', 'key2'], 'default'); // Get first found key
139
+ ```
140
+
141
+ ### ErrorManager
142
+ Singleton error handling utility with configurable tracing and custom error callbacks.
143
+
144
+ ```javascript
145
+ const { ErrorManager } = require('@reldens/utils');
146
+
147
+ // Enable stack traces
148
+ ErrorManager.enableTrace = true;
149
+
150
+ // Custom error handling
151
+ ErrorManager.callback = (message) => {
152
+ // Custom error handling logic
153
+ console.error('Custom error:', message);
154
+ // Return false to prevent default throw
155
+ return false;
156
+ };
157
+
158
+ // Trigger error
159
+ ErrorManager.error('Something went wrong');
160
+ ```
161
+
162
+ ### EnvVar
163
+ Environment variable utilities with type conversion and validation for safe environment variable access.
164
+
165
+ ```javascript
166
+ const { EnvVar } = require('@reldens/utils');
167
+
168
+ // Get environment variables with type conversion
169
+ let port = EnvVar.integer(process.env, 'PORT', 3000); // Returns integer or default
170
+ let enabled = EnvVar.boolean(process.env, 'FEATURE_ENABLED', false); // Returns boolean
171
+ let apiUrl = EnvVar.string(process.env, 'API_URL', 'http://localhost'); // Returns string
172
+
173
+ // Other type helpers
174
+ let portNumber = EnvVar.port(process.env, 'PORT', 8080); // Validates port range 1-65535
175
+ let config = EnvVar.json(process.env, 'CONFIG', {}); // Parse JSON string
176
+ let items = EnvVar.array(process.env, 'ITEMS', [], ','); // Split string to array
177
+ let url = EnvVar.url(process.env, 'API_URL', 'http://localhost'); // Validate URL
178
+ ```
179
+
180
+ ### ValidatorInterface
181
+ Base interface for creating custom validators with a standard validate method.
182
+
183
+ ```javascript
184
+ const { ValidatorInterface } = require('@reldens/utils');
185
+
186
+ // Extend to create custom validators
187
+ class CustomValidator extends ValidatorInterface {
188
+ validate(data) {
189
+ // Custom validation logic
190
+ if (!data.requiredField) {
191
+ return false;
192
+ }
193
+ return true;
194
+ }
195
+ }
196
+
197
+ let validator = new CustomValidator();
198
+ let isValid = validator.validate({ requiredField: 'value' });
199
+ ```
200
+
114
201
  ---
115
202
 
116
203
  Need something specific?
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/utils",
3
3
  "scope": "@reldens",
4
- "version": "0.53.0",
4
+ "version": "0.54.0",
5
5
  "description": "Reldens - Utils",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",