@gerync/utils 1.0.2 → 1.1.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 CHANGED
@@ -21,37 +21,41 @@ npm install @gerync/utils
21
21
 
22
22
  ### 1. coloredlog
23
23
 
24
- Log messages to the console with color support. Accepts hex, RGB, or CSS named colors. Optional bold styling works in both browser (`%c`) and Node (ANSI) consoles.
24
+ Log messages to the console with color support. Accepts hex, RGB, or CSS named colors. Optional bold styling works in both browser (`%c`) and Node (ANSI) consoles. Supports multi-color output on a single line using arrays.
25
25
 
26
26
  **Import:**
27
27
  ```typescript
28
- import utils from '@gerync/utils';
29
- utils.coloredlog(message, color, bold?);
28
+ import { coloredlog } from '@gerync/utils';
29
+ coloredlog(message, color, bold?);
30
30
  ```
31
31
 
32
32
  **Usage:**
33
33
  ```typescript
34
- // Named colors
35
- utils.coloredlog('This is an error', 'red');
36
- utils.coloredlog('This is a warning', 'orange');
37
- utils.coloredlog('Success!', 'green');
38
- utils.coloredlog('Bold success!', 'green', true);
39
-
40
- // Hex colors
41
- utils.coloredlog('Custom color', '#FF5733');
42
- utils.coloredlog('Another shade', '#FF5733CC'); // with alpha
43
-
44
- // RGB colors
45
- utils.coloredlog('RGB color', 'rgb(255, 87, 51)');
46
- utils.coloredlog('RGBA with alpha', 'rgba(255, 87, 51, 0.8)');
34
+ // Single color - Named colors
35
+ coloredlog('This is an error', 'red');
36
+ coloredlog('This is a warning', 'orange');
37
+ coloredlog('Success!', 'green');
38
+ coloredlog('Bold success!', 'green', true);
39
+
40
+ // Single color - Hex colors
41
+ coloredlog('Custom color', '#FF5733');
42
+ coloredlog('Another shade', '#FF5733CC'); // with alpha
43
+ // Single color - RGB colors
44
+ coloredlog('RGB color', 'rgb(255, 87, 51)');
45
+ coloredlog('RGBA with alpha', 'rgba(255, 87, 51, 0.8)');
46
+
47
+ // Multi-color on same line (array input)
48
+ coloredlog(['This is red', ' and this is green'], ['red', 'green']);
49
+ coloredlog(['Red', ' Blue', ' Yellow'], ['red', 'blue', 'yellow']);
50
+ coloredlog(['Bold red', ' bold blue'], ['red', 'blue'], true);
47
51
 
48
52
  // If color is invalid, logs without styling
49
- utils.coloredlog('No styling', 'invalid-color');
53
+ coloredlog('No styling', 'invalid-color');
50
54
  ```
51
55
 
52
56
  **Parameters:**
53
- - `message` (string): The text to log
54
- - `color` (string): A valid hex (`#XXX`, `#XXXX`, `#XXXXXX`, `#XXXXXXXX`), RGB (`rgb(r, g, b)`), RGBA (`rgba(r, g, b, a)`), or CSS named color
57
+ - `message` (string | string[]): The text to log. Use array for multi-color output on same line
58
+ - `color` (string | string[]): A valid hex (`#XXX`, `#XXXX`, `#XXXXXX`, `#XXXXXXXX`), RGB (`rgb(r, g, b)`), RGBA (`rgba(r, g, b, a)`), or CSS named color. Use array to match message array for multi-color
55
59
  - `bold` (boolean, default `false`): When true, renders bold text (ANSI in Node, `font-weight` in browsers)
56
60
 
57
61
  ---
@@ -63,7 +67,9 @@ Manage application preferences and localized response messages.
63
67
  **Import:**
64
68
  ```typescript
65
69
  import utils from '@gerync/utils';
66
- utils.configure({ ... });
70
+ utils.config({ ... });
71
+ // or use the shorthand:
72
+ utils.conf({ ... });
67
73
  ```
68
74
 
69
75
  **Usage:**
@@ -73,7 +79,7 @@ utils.configure({ ... });
73
79
  ```typescript
74
80
  import utils from '@gerync/utils';
75
81
 
76
- utils.configure({
82
+ utils.config({
77
83
  prefs: {
78
84
  acceptedLanguages: ['en', 'fr', 'de'],
79
85
  noDupesAllowedof: ['email', 'username', 'phone']
@@ -114,9 +120,9 @@ console.log(prefs.acceptedLanguages); // ['en', 'fr', 'de']
114
120
  ```
115
121
 
116
122
  **Methods:**
117
- - `configure(options)`: Set preferences and responses at runtime
118
- - `getResponses()`: Return the current response messages object
119
- - `getPrefs()`: Return the current preferences object
123
+ - `config(options)` or `conf(options)`: Set preferences and responses at runtime
124
+ - `config.getResponses()`: Return the current response messages object
125
+ - `config.getPrefs()`: Return the current preferences object
120
126
 
121
127
  ---
122
128
 
@@ -139,7 +145,7 @@ import utils from '@gerync/utils';
139
145
  const app = express();
140
146
 
141
147
  // Configure responses and preferences BEFORE using the middleware
142
- utils.configure({
148
+ utils.config({
143
149
  prefs: {
144
150
  acceptedLanguages: ['en', 'hu', 'es'],
145
151
  noDupesAllowedof: ['email', 'username', 'phone']
@@ -304,77 +310,7 @@ utils.object.AllowedKeys(invalidPayload, requiredKeys, optionalKeys); // fal
304
310
 
305
311
  ---
306
312
 
307
- ## Complete Example
308
313
 
309
- ```typescript
310
- import express from 'express';
311
- import utils from '@gerync/utils';
312
-
313
- const app = express();
314
-
315
- // Setup Config
316
- utils.configure({
317
- prefs: {
318
- acceptedLanguages: ['en', 'es'],
319
- noDupesAllowedof: ['email', 'username']
320
- },
321
- responses: {
322
- en: {
323
- dupes: {
324
- email: 'Email already exists.',
325
- username: 'Username is taken.'
326
- },
327
- general: {
328
- error: 'An error occurred.'
329
- }
330
- },
331
- es: {
332
- dupes: {
333
- email: 'El email ya existe.',
334
- username: 'El nombre de usuario ya está en uso.'
335
- },
336
- general: {
337
- error: 'Ocurrió un error.'
338
- }
339
- }
340
- }
341
- });
342
-
343
- app.use(express.json());
344
-
345
- // Routes
346
- app.post('/users', (req, res, next) => {
347
- try {
348
- // Validate request payload
349
- const requiredKeys = ['name', 'email'];
350
- const optionalKeys = ['phone'];
351
-
352
- if (!utils.object.AllowedKeys(req.body, requiredKeys, optionalKeys)) {
353
- return res.status(400).json({
354
- status: 'error',
355
- message: 'Invalid request payload'
356
- });
357
- }
358
-
359
- utils.coloredlog(`User creation requested: ${req.body.email}`, 'cyan');
360
-
361
- // Simulate user creation
362
- const user = { id: 1, ...req.body };
363
- res.json(user);
364
- } catch (error) {
365
- next(error);
366
- }
367
- });
368
-
369
- // Error handler middleware (must be last)
370
- app.use(utils.errorHandler);
371
-
372
- app.listen(3000, () => {
373
- utils.coloredlog('Server running on port 3000', 'green');
374
- });
375
- ```
376
-
377
- ---
378
314
 
379
315
  ## License
380
316
 
@@ -3,15 +3,17 @@
3
3
  * Accepts hex, RGB, RGBA, and CSS named colors.
4
4
  * Falls back to unstyled console.log if color is invalid.
5
5
  *
6
- * @param message - Text to log
6
+ * @param message - Text to log (string or array of strings for multi-color)
7
7
  * @param color - Valid hex (#XXX, #XXXX, #XXXXXX, #XXXXXXXX),
8
8
  * RGB (rgb(r, g, b)), RGBA (rgba(r, g, b, a)),
9
- * or CSS named color
9
+ * or CSS named color (string or array matching message array)
10
+ * @param bolded - Apply bold styling (default: false)
10
11
  *
11
12
  * @example
12
13
  * coloredlog('Error!', 'red');
13
14
  * coloredlog('Success', '#00FF00');
14
15
  * coloredlog('Info', 'rgb(0, 0, 255)');
16
+ * coloredlog(['Red text', 'Blue text'], ['red', 'blue']);
15
17
  */
16
- export default function Coloredlog(message: string, color: string, bolded?: boolean): void;
18
+ export default function Coloredlog(message: string | string[], color: string | string[], bolded?: boolean): void;
17
19
  //# sourceMappingURL=Colorlog.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Colorlog.d.ts","sourceRoot":"","sources":["../../functions/Colorlog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,GAAE,OAAe,GAAG,IAAI,CA6GhG"}
1
+ {"version":3,"file":"Colorlog.d.ts","sourceRoot":"","sources":["../../functions/Colorlog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,OAAO,UAAU,UAAU,CAAE,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,MAAM,GAAE,OAAe,GAAG,IAAI,CAuJvH"}
@@ -3,15 +3,17 @@
3
3
  * Accepts hex, RGB, RGBA, and CSS named colors.
4
4
  * Falls back to unstyled console.log if color is invalid.
5
5
  *
6
- * @param message - Text to log
6
+ * @param message - Text to log (string or array of strings for multi-color)
7
7
  * @param color - Valid hex (#XXX, #XXXX, #XXXXXX, #XXXXXXXX),
8
8
  * RGB (rgb(r, g, b)), RGBA (rgba(r, g, b, a)),
9
- * or CSS named color
9
+ * or CSS named color (string or array matching message array)
10
+ * @param bolded - Apply bold styling (default: false)
10
11
  *
11
12
  * @example
12
13
  * coloredlog('Error!', 'red');
13
14
  * coloredlog('Success', '#00FF00');
14
15
  * coloredlog('Info', 'rgb(0, 0, 255)');
16
+ * coloredlog(['Red text', 'Blue text'], ['red', 'blue']);
15
17
  */
16
18
  export default function Coloredlog(message, color, bolded = false) {
17
19
  // #region Color Validation Regexes
@@ -85,20 +87,58 @@ export default function Coloredlog(message, color, bolded = false) {
85
87
  return null;
86
88
  };
87
89
  // #endregion
88
- // #region Validate Color Format
89
- /** Test if color matches any valid format */
90
- const isValid = ColorRegex.hex.test(color) ||
91
- ColorRegex.rgb.test(color) ||
92
- ColorRegex.named.test(color);
90
+ // #region Handle Multi-Color Array Input
91
+ if (Array.isArray(message)) {
92
+ const messages = message;
93
+ const colors = Array.isArray(color) ? color : Array(messages.length).fill(color);
94
+ if (isBrowser) {
95
+ // Build format string with %c placeholders
96
+ const formatString = messages.map(msg => `%c${msg}`).join('');
97
+ const styles = colors.map(c => {
98
+ const weight = bolded ? 'bold' : 'normal';
99
+ const isValidColor = ColorRegex.hex.test(c) ||
100
+ ColorRegex.rgb.test(c) ||
101
+ ColorRegex.named.test(c);
102
+ return isValidColor ? `color: ${c}; font-weight: ${weight};` : `font-weight: ${weight};`;
103
+ });
104
+ console.log(formatString, ...styles);
105
+ }
106
+ else {
107
+ // Build ANSI string for Node.js
108
+ const boldAnsi = bolded ? '\x1b[1m' : '';
109
+ let output = '';
110
+ for (let i = 0; i < messages.length; i++) {
111
+ const msg = messages[i];
112
+ const col = colors[i] || '';
113
+ const ansi = colorToAnsi(col);
114
+ if (ansi) {
115
+ output += `${ansi}${boldAnsi}${msg}\x1b[0m`;
116
+ }
117
+ else if (boldAnsi) {
118
+ output += `${boldAnsi}${msg}\x1b[0m`;
119
+ }
120
+ else {
121
+ output += msg;
122
+ }
123
+ }
124
+ console.log(output);
125
+ }
126
+ return;
127
+ }
93
128
  // #endregion
94
- // #region Output
129
+ // #region Single Color Output (Original Behavior)
130
+ const colorStr = Array.isArray(color) ? color[0] : color;
131
+ /** Test if color matches any valid format */
132
+ const isValid = ColorRegex.hex.test(colorStr) ||
133
+ ColorRegex.rgb.test(colorStr) ||
134
+ ColorRegex.named.test(colorStr);
95
135
  if (isValid) {
96
136
  if (isBrowser) {
97
137
  const weight = bolded ? 'bold' : 'normal';
98
- console.log(`%c${message}`, `color: ${color}; font-weight: ${weight};`);
138
+ console.log(`%c${message}`, `color: ${colorStr}; font-weight: ${weight};`);
99
139
  }
100
140
  else {
101
- const ansi = colorToAnsi(color);
141
+ const ansi = colorToAnsi(colorStr);
102
142
  const boldAnsi = bolded ? '\x1b[1m' : '';
103
143
  if (ansi) {
104
144
  console.log(`${ansi}${boldAnsi}${message}\x1b[0m`);
@@ -9,7 +9,16 @@ declare const Configs: {
9
9
  * @param options.responses - Localized message objects by language
10
10
  * @param options.prefs - Preferences including acceptedLanguages and noDupesAllowedof
11
11
  */
12
- configure(options: {
12
+ config(options: {
13
+ responses: any;
14
+ prefs: any;
15
+ }): void;
16
+ /**
17
+ * Alias for config(). Initialize configuration with custom responses and preferences.
18
+ * @param options.responses - Localized message objects by language
19
+ * @param options.prefs - Preferences including acceptedLanguages and noDupesAllowedof
20
+ */
21
+ conf(options: {
13
22
  responses: any;
14
23
  prefs: any;
15
24
  }): void;
@@ -1 +1 @@
1
- {"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../functions/Config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAaH,QAAA,MAAM,OAAO;IAET;;;;;OAKG;uBACgB;QAAE,SAAS,EAAE,GAAG,CAAC;QAAC,KAAK,EAAE,GAAG,CAAA;KAAE;IAOjD;;;OAGG;;IAOH;;;OAGG;;IAOH;;;;;;OAMG;qBACc,MAAM,QAAQ,MAAM,WAAW,MAAM;IAetD;;;;;;;OAOG;qBACc,MAAM,QAAQ,MAAM,aAAa,MAAM,GAAG,MAAM,GAAG,SAAS;CAehF,CAAC;AAEF,eAAe,OAAO,CAAC"}
1
+ {"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../functions/Config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAaH,QAAA,MAAM,OAAO;IAET;;;;;OAKG;oBACa;QAAE,SAAS,EAAE,GAAG,CAAC;QAAC,KAAK,EAAE,GAAG,CAAA;KAAE;IAO9C;;;;OAIG;kBACW;QAAE,SAAS,EAAE,GAAG,CAAC;QAAC,KAAK,EAAE,GAAG,CAAA;KAAE;IAM5C;;;OAGG;;IAOH;;;OAGG;;IAOH;;;;;;OAMG;qBACc,MAAM,QAAQ,MAAM,WAAW,MAAM;IAetD;;;;;;;OAOG;qBACc,MAAM,QAAQ,MAAM,aAAa,MAAM,GAAG,MAAM,GAAG,SAAS;CAehF,CAAC;AAEF,eAAe,OAAO,CAAC"}
@@ -12,18 +12,28 @@ let runtimePrefs = {
12
12
  };
13
13
  // #endregion
14
14
  const Configs = {
15
- // #region Configure
15
+ // #region Config
16
16
  /**
17
17
  * Initialize configuration with custom responses and preferences.
18
18
  * Merges or replaces the runtime state.
19
19
  * @param options.responses - Localized message objects by language
20
20
  * @param options.prefs - Preferences including acceptedLanguages and noDupesAllowedof
21
21
  */
22
- configure(options) {
22
+ config(options) {
23
23
  runtimeResponses = options.responses;
24
24
  runtimePrefs = options.prefs;
25
25
  },
26
26
  // #endregion
27
+ // #region Conf Alias
28
+ /**
29
+ * Alias for config(). Initialize configuration with custom responses and preferences.
30
+ * @param options.responses - Localized message objects by language
31
+ * @param options.prefs - Preferences including acceptedLanguages and noDupesAllowedof
32
+ */
33
+ conf(options) {
34
+ return this.config(options);
35
+ },
36
+ // #endregion
27
37
  // #region Get Responses
28
38
  /**
29
39
  * Retrieve all configured localized response messages.
package/dist/index.d.ts CHANGED
@@ -3,6 +3,10 @@ import errorHandler from "./functions/handleError.js";
3
3
  import config from "./functions/Config.js";
4
4
  import object from "./functions/ObjectKeys.js";
5
5
  export { coloredlog, errorHandler, object, config };
6
+ export declare const conf: (options: {
7
+ responses: any;
8
+ prefs: any;
9
+ }) => void;
6
10
  export declare const configure: (options: {
7
11
  responses: any;
8
12
  prefs: any;
@@ -11,7 +15,11 @@ declare const _default: {
11
15
  coloredlog: typeof coloredlog;
12
16
  errorHandler: typeof errorHandler;
13
17
  config: {
14
- configure(options: {
18
+ config(options: {
19
+ responses: any;
20
+ prefs: any;
21
+ }): void;
22
+ conf(options: {
15
23
  responses: any;
16
24
  prefs: any;
17
25
  }): void;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,yBAAyB,CAAC;AACjD,OAAO,YAAY,MAAM,4BAA4B,CAAC;AACtD,OAAO,MAAM,MAAM,uBAAuB,CAAC;AAC3C,OAAO,MAAM,MAAM,2BAA2B,CAAC;AAE/C,OAAO,EACH,UAAU,EACV,YAAY,EACZ,MAAM,EACN,MAAM,EACT,CAAC;AAEF,eAAO,MAAM,SAAS;;;UAAmB,CAAC;;;;;;;;;;;;;;;;;;;;;AAE1C,wBAKE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,yBAAyB,CAAC;AACjD,OAAO,YAAY,MAAM,4BAA4B,CAAC;AACtD,OAAO,MAAM,MAAM,uBAAuB,CAAC;AAC3C,OAAO,MAAM,MAAM,2BAA2B,CAAC;AAE/C,OAAO,EACH,UAAU,EACV,YAAY,EACZ,MAAM,EACN,MAAM,EACT,CAAC;AAEF,eAAO,MAAM,IAAI;;;UAAc,CAAC;AAChC,eAAO,MAAM,SAAS;;;UAAgB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;AAEvC,wBAKE"}
package/dist/index.js CHANGED
@@ -3,7 +3,8 @@ import errorHandler from "./functions/handleError.js";
3
3
  import config from "./functions/Config.js";
4
4
  import object from "./functions/ObjectKeys.js";
5
5
  export { coloredlog, errorHandler, object, config };
6
- export const configure = config.configure;
6
+ export const conf = config.conf;
7
+ export const configure = config.config;
7
8
  export default {
8
9
  coloredlog,
9
10
  errorHandler,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gerync/utils",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "A collection of utilities for any type of project, providing logging, error handling, configuration management, and object manipulation helpers.",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",