@gerync/utils 1.0.0 → 1.0.1

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,12 +21,12 @@ 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.
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.
25
25
 
26
26
  **Import:**
27
27
  ```typescript
28
28
  import utils from '@gerync/utils';
29
- utils.coloredlog(message, color);
29
+ utils.coloredlog(message, color, bold?);
30
30
  ```
31
31
 
32
32
  **Usage:**
@@ -35,6 +35,7 @@ utils.coloredlog(message, color);
35
35
  utils.coloredlog('This is an error', 'red');
36
36
  utils.coloredlog('This is a warning', 'orange');
37
37
  utils.coloredlog('Success!', 'green');
38
+ utils.coloredlog('Bold success!', 'green', true);
38
39
 
39
40
  // Hex colors
40
41
  utils.coloredlog('Custom color', '#FF5733');
@@ -51,6 +52,7 @@ utils.coloredlog('No styling', 'invalid-color');
51
52
  **Parameters:**
52
53
  - `message` (string): The text to log
53
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
55
+ - `bold` (boolean, default `false`): When true, renders bold text (ANSI in Node, `font-weight` in browsers)
54
56
 
55
57
  ---
56
58
 
@@ -172,7 +174,7 @@ app.use(utils.errorHandler);
172
174
 
173
175
  ```json
174
176
  {
175
- "status": "error",
177
+ "status": 409,
176
178
  "code": "ER_DUP_ENTRY",
177
179
  "message": "Email address already exists."
178
180
  }
@@ -13,5 +13,5 @@
13
13
  * coloredlog('Success', '#00FF00');
14
14
  * coloredlog('Info', 'rgb(0, 0, 255)');
15
15
  */
16
- export default function Coloredlog(message: string, color: string): void;
16
+ export default function Coloredlog(message: string, color: string, bolded?: boolean): void;
17
17
  //# 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,GAAG,IAAI,CA8BvE"}
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"}
@@ -13,7 +13,7 @@
13
13
  * coloredlog('Success', '#00FF00');
14
14
  * coloredlog('Info', 'rgb(0, 0, 255)');
15
15
  */
16
- export default function Coloredlog(message, color) {
16
+ export default function Coloredlog(message, color, bolded = false) {
17
17
  // #region Color Validation Regexes
18
18
  /** Regex patterns to validate different color formats */
19
19
  const ColorRegex = {
@@ -22,7 +22,67 @@ export default function Coloredlog(message, color) {
22
22
  /** RGB/RGBA colors: rgb(r, g, b) or rgba(r, g, b, a) */
23
23
  rgb: /^rgba?\(\s*(\d{1,3}%?)\s*[\s,]\s*(\d{1,3}%?)\s*[\s,]\s*(\d{1,3}%?)(?:\s*[\s,/]\s*((?:0|1|0?\.\d+)(?:%?)))?\s*\)$/,
24
24
  /** CSS named colors (e.g., 'red', 'blue', 'aliceblue') */
25
- named: /^(?:aliceblue|antiquewhite|aqua|aquamarine|azure|beige|bisque|black|blanchedalmond|blue|blueviolet|brown|burlywood|cadetblue|chartreuse|chocolate|coral|cornflowerblue|cornsilk|crimson|cyan|darkblue|darkcyan|darkgoldenrod|darkgray|darkgreen|darkgrey|darkkhaki|darkmagenta|darkolivegreen|darkorange|darkorchid|darkred|darksalmon|darkseagreen|darkslateblue|darkslategray|darkslategrey|darkturquoise|darkviolet|deeppink|deepskyblue|dimgray|dimgrey|dodgerblue|firebrick|floralwhite|forestgreen|fuchsia|gainsboro|ghostwhite|gold|goldenrod|gray|green|greenyellow|grey|honeydew|hotpink|indianred|indigo|ivory|khaki|lavender|lavenderblush|lawngreen|lemonchiffon|lightblue|lightcoral|lightcyan|lightgoldenrodyellow|lightgray|lightgreen|lightgrey|lightpink|lightsalmon|lightseagreen|lightskyblue|mineskyblue|lightslategray|lightslategrey|lightsteelblue|lightyellow|limegreen|linen|magenta|mediumaquamarine|mediumblue|mediumorchid|mediumpurple|mediumseagreen|mediumslateblue|mediumspringgreen|mediumturquoise|mediumvioletred|midnightblue|mintcream|mistyrose|moccasin|navajowhite|oldlace|olivedrab|orangered|palegoldenrod|palegreen|palevioletred)$/i,
25
+ named: /^(?:aliceblue|antiquewhite|aqua|aquamarine|azure|beige|bisque|black|blanchedalmond|blue|blueviolet|brown|burlywood|cadetblue|chartreuse|chocolate|coral|cornflowerblue|cornsilk|crimson|cyan|darkblue|darkcyan|darkgoldenrod|darkgray|darkgreen|darkgrey|darkkhaki|darkmagenta|darkolivegreen|darkorange|darkorchid|darkred|darksalmon|darkseagreen|darkslateblue|darkslategray|darkslategrey|darkturquoise|darkviolet|deeppink|deepskyblue|dimgray|dimgrey|dodgerblue|firebrick|floralwhite|forestgreen|fuchsia|gainsboro|ghostwhite|gold|goldenrod|gray|green|greenyellow|grey|honeydew|hotpink|indianred|indigo|ivory|khaki|lavender|lavenderblush|lawngreen|lemonchiffon|lightblue|lightcoral|lightcyan|lightgoldenrodyellow|lightgray|lightgreen|lightgrey|lightpink|lightsalmon|lightseagreen|lightskyblue|mineskyblue|lightslategray|lightslategrey|lightsteelblue|lightyellow|limegreen|linen|magenta|mediumaquamarine|mediumblue|mediumorchid|mediumpurple|mediumseagreen|mediumslateblue|mediumspringgreen|mediumturquoise|mediumvioletred|midnightblue|mincream|mistyrose|moccasin|navajowhite|oldlace|olivedrab|orangered|palegoldenrod|palegreen|palevioletred)$/i,
26
+ };
27
+ // #endregion
28
+ // #region Helpers for Node console coloring
29
+ const isBrowser = typeof globalThis !== 'undefined' && typeof globalThis.document !== 'undefined';
30
+ const namedAnsi = {
31
+ black: '\x1b[30m',
32
+ red: '\x1b[31m',
33
+ green: '\x1b[32m',
34
+ yellow: '\x1b[33m',
35
+ blue: '\x1b[34m',
36
+ magenta: '\x1b[35m',
37
+ cyan: '\x1b[36m',
38
+ white: '\x1b[37m',
39
+ gray: '\x1b[90m',
40
+ grey: '\x1b[90m'
41
+ };
42
+ const hexToRgb = (value) => {
43
+ const clean = value.replace('#', '');
44
+ if (clean.length === 3 || clean.length === 4) {
45
+ const r = parseInt(clean[0] + clean[0], 16);
46
+ const g = parseInt(clean[1] + clean[1], 16);
47
+ const b = parseInt(clean[2] + clean[2], 16);
48
+ return { r, g, b };
49
+ }
50
+ if (clean.length === 6 || clean.length === 8) {
51
+ const r = parseInt(clean.slice(0, 2), 16);
52
+ const g = parseInt(clean.slice(2, 4), 16);
53
+ const b = parseInt(clean.slice(4, 6), 16);
54
+ return { r, g, b };
55
+ }
56
+ return null;
57
+ };
58
+ const rgbStringToRgb = (value) => {
59
+ const match = ColorRegex.rgb.exec(value);
60
+ if (!match)
61
+ return null;
62
+ const toNum = (input) => {
63
+ if (input.endsWith('%')) {
64
+ const pct = parseFloat(input.replace('%', ''));
65
+ return Math.max(0, Math.min(255, Math.round((pct / 100) * 255)));
66
+ }
67
+ return Math.max(0, Math.min(255, parseInt(input, 10)));
68
+ };
69
+ return { r: toNum(match[1]), g: toNum(match[2]), b: toNum(match[3]) };
70
+ };
71
+ const colorToAnsi = (value) => {
72
+ const lower = value.toLowerCase();
73
+ if (namedAnsi[lower])
74
+ return namedAnsi[lower];
75
+ if (ColorRegex.hex.test(value)) {
76
+ const rgb = hexToRgb(value);
77
+ if (rgb)
78
+ return `\x1b[38;2;${rgb.r};${rgb.g};${rgb.b}m`;
79
+ }
80
+ if (ColorRegex.rgb.test(value)) {
81
+ const rgb = rgbStringToRgb(value);
82
+ if (rgb)
83
+ return `\x1b[38;2;${rgb.r};${rgb.g};${rgb.b}m`;
84
+ }
85
+ return null;
26
86
  };
27
87
  // #endregion
28
88
  // #region Validate Color Format
@@ -33,12 +93,31 @@ export default function Coloredlog(message, color) {
33
93
  // #endregion
34
94
  // #region Output
35
95
  if (isValid) {
36
- /** Use CSS styling for valid colors */
37
- console.log(`%c${message}`, `color: ${color}; font-weight: bold;`);
96
+ if (isBrowser) {
97
+ const weight = bolded ? 'bold' : 'normal';
98
+ console.log(`%c${message}`, `color: ${color}; font-weight: ${weight};`);
99
+ }
100
+ else {
101
+ const ansi = colorToAnsi(color);
102
+ const boldAnsi = bolded ? '\x1b[1m' : '';
103
+ if (ansi) {
104
+ console.log(`${ansi}${boldAnsi}${message}\x1b[0m`);
105
+ }
106
+ else if (bolded) {
107
+ console.log(`\x1b[1m${message}\x1b[0m`);
108
+ }
109
+ else {
110
+ console.log(message);
111
+ }
112
+ }
38
113
  }
39
114
  else {
40
- /** Fallback: plain console.log if color is invalid */
41
- console.log(message);
115
+ if (bolded) {
116
+ console.log(`\x1b[1m${message}\x1b[0m`);
117
+ }
118
+ else {
119
+ console.log(message);
120
+ }
42
121
  }
43
122
  // #endregion
44
123
  }
@@ -1,5 +1,5 @@
1
- import Configs from './Config';
2
- import Coloredlog from './Colorlog';
1
+ import Configs from './Config.js';
2
+ import Coloredlog from './Colorlog.js';
3
3
  /**
4
4
  * Express error handler middleware for centralized error handling
5
5
  * with multi-language support, smart logging, and user-friendly messages.
@@ -104,7 +104,7 @@ export default function errorHandler(err, req, res, next) {
104
104
  const requestId = (req.id || req.requestId || '');
105
105
  /** Log detailed, color-coded error report for maintainers (skip for user-caused errors) */
106
106
  if (!isUserError) {
107
- Coloredlog('===================== ERROR REPORT =====================', 'red');
107
+ Coloredlog('===================== ERROR REPORT =====================', 'red', true);
108
108
  // Timestamp
109
109
  let exactTime = new Date().toISOString();
110
110
  exactTime = exactTime.replace('T', ' ').replace('Z', '');
@@ -115,21 +115,21 @@ export default function errorHandler(err, req, res, next) {
115
115
  if (requestId)
116
116
  Coloredlog(`RequestId: ${requestId}`, 'yellow');
117
117
  // Error details
118
- Coloredlog(`Status: ${statusCode}`, 'magenta');
119
- Coloredlog(`Code: ${code}`, 'magenta');
118
+ Coloredlog(`Status: ${statusCode}`, 'magenta', true);
119
+ Coloredlog(`Code: ${code}`, 'magenta', true);
120
120
  // Messages for comparison
121
121
  Coloredlog(`UserMessage: ${responseMessage}`, 'cyan');
122
122
  Coloredlog(`RawMessage: ${rawErrorMessage}`, 'white');
123
123
  // Stack trace for debugging
124
- Coloredlog('Stack:', 'gray');
124
+ Coloredlog('Stack:', 'gray', true);
125
125
  Coloredlog(err && err.stack ? String(err.stack) : 'N/A', 'gray');
126
- Coloredlog('========================================================', 'red');
126
+ Coloredlog('========================================================', 'red', true);
127
127
  }
128
128
  // #endregion
129
129
  // #region Send Response to Client
130
130
  /** Return JSON response with error details (safe for client) */
131
131
  res.status(statusCode).json({
132
- status: 'error',
132
+ status: statusCode,
133
133
  code: code,
134
134
  message: responseMessage
135
135
  });
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import coloredlog from "./functions/Colorlog";
2
- import errorHandler from "./functions/handleError";
1
+ import coloredlog from "./functions/Colorlog.js";
2
+ import errorHandler from "./functions/handleError.js";
3
3
  declare const _default: {
4
4
  coloredlog: typeof coloredlog;
5
5
  errorHandler: typeof errorHandler;
@@ -18,10 +18,10 @@ declare const _default: {
18
18
  prefs: any;
19
19
  }) => void;
20
20
  object: {
21
- Keysamount: typeof import("./functions/ObjectKeys").Keysamount;
22
- KeysInRange: typeof import("./functions/ObjectKeys").KeysInRange;
23
- KeysInRangeDetailed: typeof import("./functions/ObjectKeys").KeysInRangeDetailed;
24
- AllowedKeys: typeof import("./functions/ObjectKeys").AllowedKeys;
21
+ Keysamount: typeof import("./functions/ObjectKeys.js").Keysamount;
22
+ KeysInRange: typeof import("./functions/ObjectKeys.js").KeysInRange;
23
+ KeysInRangeDetailed: typeof import("./functions/ObjectKeys.js").KeysInRangeDetailed;
24
+ AllowedKeys: typeof import("./functions/ObjectKeys.js").AllowedKeys;
25
25
  };
26
26
  };
27
27
  export default _default;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,sBAAsB,CAAC;AAC9C,OAAO,YAAY,MAAM,yBAAyB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;AAKnD,wBAME"}
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;;;;;;;;;;;;;;;;;;;;;;;;;AAKtD,wBAME"}
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import coloredlog from "./functions/Colorlog";
2
- import errorHandler from "./functions/handleError";
3
- import config from "./functions/Config";
4
- import object from "./functions/ObjectKeys";
1
+ import coloredlog from "./functions/Colorlog.js";
2
+ import errorHandler from "./functions/handleError.js";
3
+ import config from "./functions/Config.js";
4
+ import object from "./functions/ObjectKeys.js";
5
5
  export default {
6
6
  coloredlog,
7
7
  errorHandler,
package/package.json CHANGED
@@ -1,62 +1,62 @@
1
- {
2
- "name": "@gerync/utils",
3
- "version": "1.0.0",
4
- "description": "A collection of utilities for any type of project, providing logging, error handling, configuration management, and object manipulation helpers.",
5
- "main": "./dist/index.js",
6
- "types": "./dist/index.d.ts",
7
- "type": "module",
8
- "exports": {
9
- ".": {
10
- "import": "./dist/index.js",
11
- "types": "./dist/index.d.ts"
12
- }
13
- },
14
- "files": [
15
- "dist",
16
- "README.md",
17
- "LICENSE",
18
- "SECURITY.md"
19
- ],
20
- "scripts": {
21
- "build": "tsc",
22
- "prepublishOnly": "npm run build",
23
- "test": "vitest run",
24
- "test:watch": "vitest"
25
- },
26
- "keywords": [
27
- "utilities",
28
- "error-handling",
29
- "logging",
30
- "express",
31
- "middleware",
32
- "validation",
33
- "configuration",
34
- "i18n",
35
- "object-manipulation",
36
- "typescript",
37
- "multi-language",
38
- "utils",
39
- "multilingual"
40
- ],
41
- "author": "gerync",
42
- "license": "MIT",
43
- "repository": {
44
- "type": "git",
45
- "url": "https://github.com/gerync/utils.git"
46
- },
47
- "bugs": {
48
- "url": "https://github.com/gerync/utils/issues"
49
- },
50
- "homepage": "https://github.com/gerync/utils#readme",
51
- "peerDependencies": {
52
- "express": "^5.0.0"
53
- },
54
- "dependencies": {},
55
- "devDependencies": {
56
- "@types/node": "^20.11.30",
57
- "@types/express": "^4.17.21",
58
- "express": "^5.2.1",
59
- "typescript": "^5.6.3",
60
- "vitest": "^1.6.0"
61
- }
1
+ {
2
+ "name": "@gerync/utils",
3
+ "version": "1.0.1",
4
+ "description": "A collection of utilities for any type of project, providing logging, error handling, configuration management, and object manipulation helpers.",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "README.md",
17
+ "LICENSE",
18
+ "SECURITY.md"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "prepublishOnly": "npm run build",
23
+ "test": "vitest run",
24
+ "test:watch": "vitest"
25
+ },
26
+ "keywords": [
27
+ "utilities",
28
+ "error-handling",
29
+ "logging",
30
+ "express",
31
+ "middleware",
32
+ "validation",
33
+ "configuration",
34
+ "i18n",
35
+ "object-manipulation",
36
+ "typescript",
37
+ "multi-language",
38
+ "utils",
39
+ "multilingual"
40
+ ],
41
+ "author": "gerync",
42
+ "license": "MIT",
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "https://github.com/gerync/utils.git"
46
+ },
47
+ "bugs": {
48
+ "url": "https://github.com/gerync/utils/issues"
49
+ },
50
+ "homepage": "https://github.com/gerync/utils#readme",
51
+ "peerDependencies": {
52
+ "express": "^5.0.0"
53
+ },
54
+ "dependencies": {},
55
+ "devDependencies": {
56
+ "@types/node": "^20.11.30",
57
+ "@types/express": "^4.17.21",
58
+ "express": "^5.2.1",
59
+ "typescript": "^5.6.3",
60
+ "vitest": "^1.6.0"
61
+ }
62
62
  }