@gesslar/toolkit 0.0.6 → 0.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gesslar/toolkit",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Get in, bitches, we're going toolkitting.",
5
5
  "main": "./src/index.js",
6
6
  "type": "module",
package/src/index.js CHANGED
@@ -6,6 +6,7 @@ export {default as File} from "./lib/File.js"
6
6
  // Utility classes
7
7
  export {default as Cache} from "./lib/Cache.js"
8
8
  export {default as Data} from "./lib/Data.js"
9
+ export {default as Glog} from "./lib/Glog.js"
9
10
  export {default as Sass} from "./lib/Sass.js"
10
11
  export {default as Term} from "./lib/Term.js"
11
12
  export {default as Type} from "./lib/Type.js"
@@ -0,0 +1,119 @@
1
+ import Data from "./Data.js"
2
+ import console from "node:console"
3
+
4
+ /**
5
+ * Global logging utility with configurable log levels and prefixes.
6
+ * Provides a flexible logging system that can be used as both a class and
7
+ * a callable function, with support for log level filtering and custom
8
+ * prefixes for better log organization.
9
+ *
10
+ * The Glog class uses a proxy to enable both class-style and function-style
11
+ * usage patterns, making it convenient for different coding preferences.
12
+ *
13
+ * @example
14
+ * // Set up logging configuration
15
+ * Glog.setLogLevel(3).setLogPrefix('[MyApp]')
16
+ *
17
+ * // Log messages with different levels
18
+ * Glog(0, 'Critical error') // Always shown
19
+ * Glog(2, 'Debug info') // Shown if logLevel >= 2
20
+ * Glog('Simple message') // Level 0 by default
21
+ */
22
+ class Glog {
23
+ /** @type {number} Current log level threshold (0-5) */
24
+ logLevel = 0
25
+ /** @type {string} Prefix to prepend to all log messages */
26
+ logPrefix = ""
27
+
28
+ /**
29
+ * Sets the log prefix for all subsequent log messages.
30
+ * The prefix helps identify the source of log messages in complex
31
+ * applications with multiple components.
32
+ *
33
+ * @param {string} prefix - The prefix string to prepend to log messages
34
+ * @returns {typeof Glog} Returns the Glog class for method chaining
35
+ * @example
36
+ * Glog.setLogPrefix('[Database]')
37
+ * Glog('Connection established') // Output: [Database] Connection established
38
+ */
39
+ static setLogPrefix(prefix) {
40
+ this.logPrefix = prefix
41
+
42
+ return Glog
43
+ }
44
+
45
+ /**
46
+ * Sets the minimum log level for messages to be displayed.
47
+ * Messages with a level higher than this threshold will be filtered out.
48
+ * Log levels range from 0 (critical) to 5 (verbose debug).
49
+ *
50
+ * @param {number} level - The minimum log level (0-5, clamped to range)
51
+ * @returns {typeof Glog} Returns the Glog class for method chaining
52
+ * @example
53
+ * Glog.setLogLevel(2) // Only show messages with level 0, 1, or 2
54
+ * Glog(1, 'Important') // Shown
55
+ * Glog(3, 'Verbose') // Hidden
56
+ */
57
+ static setLogLevel(level) {
58
+ this.logLevel = Data.clamp(level, 0, 5)
59
+
60
+ return Glog
61
+ }
62
+
63
+ /**
64
+ * Internal logging method that handles message formatting and level
65
+ * filtering.
66
+ *
67
+ * Parses arguments to determine log level and message content, then outputs
68
+ * the message if it meets the current log level threshold.
69
+ *
70
+ * @private
71
+ * @param {...unknown} args - Variable arguments: either (level, ...messages) or (...messages)
72
+ * @returns {void}
73
+ */
74
+ static #log(...args) {
75
+ let level, rest
76
+
77
+ if(args.length === 0) {
78
+ ;[level=0, rest=[""]] = null
79
+ } else if(args.length === 1) {
80
+ ;[rest, level=0] = [args, 0]
81
+ } else {
82
+ ;[level, ...rest] = args
83
+ }
84
+
85
+ if(level > this.logLevel)
86
+ return
87
+
88
+ if(this.logPrefix)
89
+ console.log(this.logPrefix, ...rest)
90
+ else
91
+ console.log(...rest)
92
+ }
93
+
94
+ /**
95
+ * Executes a log operation with the provided arguments.
96
+ * This method serves as the entry point for all logging operations,
97
+ * delegating to the private #log method for actual processing.
98
+ *
99
+ * @param {...unknown} args - Log level (optional) followed by message arguments
100
+ * @returns {void}
101
+ * @example
102
+ * Glog.execute(0, 'Error:', error.message)
103
+ * Glog.execute('Simple message') // Level 0 assumed
104
+ */
105
+ static execute(...args) {
106
+ this.#log(...args)
107
+ }
108
+ }
109
+
110
+ // Wrap the class in a proxy
111
+ export default new Proxy(Glog, {
112
+ apply(target, thisArg, argumentsList) {
113
+ // When called as function: MyClass(things, and, stuff)
114
+ return target.execute(...argumentsList)
115
+ },
116
+ construct(target, argumentsList) {
117
+ return new target(...argumentsList)
118
+ }
119
+ })
@@ -0,0 +1,72 @@
1
+ // Implementation: ../lib/Glog.js
2
+
3
+ /**
4
+ * Global logging utility with configurable log levels and prefixes.
5
+ * Provides a flexible logging system that can be used as both a class and
6
+ * a callable function, with support for log level filtering and custom
7
+ * prefixes for better log organization.
8
+ *
9
+ * The Glog class uses a proxy to enable both class-style and function-style
10
+ * usage patterns, making it convenient for different coding preferences.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * // Set up logging configuration
15
+ * Glog.setLogLevel(3).setLogPrefix('[MyApp]')
16
+ *
17
+ * // Log messages with different levels
18
+ * Glog(0, 'Critical error') // Always shown
19
+ * Glog(2, 'Debug info') // Shown if logLevel >= 2
20
+ * Glog('Simple message') // Level 0 by default
21
+ * ```
22
+ */
23
+ interface GlogInterface {
24
+ /**
25
+ * Sets the log prefix for all subsequent log messages.
26
+ * The prefix helps identify the source of log messages in complex
27
+ * applications with multiple components.
28
+ *
29
+ * @param prefix - The prefix string to prepend to log messages
30
+ * @returns Returns the Glog class for method chaining
31
+ */
32
+ setLogPrefix(prefix: string): typeof Glog
33
+
34
+ /**
35
+ * Sets the minimum log level for messages to be displayed.
36
+ * Messages with a level higher than this threshold will be filtered out.
37
+ * Log levels range from 0 (critical) to 5 (verbose debug).
38
+ *
39
+ * @param level - The minimum log level (0-5, clamped to range)
40
+ * @returns Returns the Glog class for method chaining
41
+ */
42
+ setLogLevel(level: number): typeof Glog
43
+
44
+ /**
45
+ * Executes a log operation with the provided arguments.
46
+ * This method serves as the entry point for all logging operations.
47
+ *
48
+ * @param args - Log level (optional) followed by message arguments
49
+ */
50
+ execute(...args: any[]): void
51
+ }
52
+
53
+ /**
54
+ * Callable interface for the proxied Glog class.
55
+ * Allows the class to be used as a function.
56
+ */
57
+ interface GlogCallable {
58
+ /**
59
+ * Log a message with optional level specification.
60
+ *
61
+ * @param args - Either (level: number, ...messages) or (...messages)
62
+ */
63
+ (...args: any[]): void
64
+ }
65
+
66
+ /**
67
+ * The Glog class with proxy functionality for callable behavior.
68
+ * Can be used both as a static class and as a callable function.
69
+ */
70
+ declare const Glog: GlogInterface & GlogCallable
71
+
72
+ export default Glog
@@ -7,6 +7,7 @@ export { default as File } from './File.js'
7
7
  // Utility classes
8
8
  export { default as Cache } from './Cache.js'
9
9
  export { default as Data } from './Data.js'
10
+ export { default as Glog } from './Glog.js'
10
11
  export { default as Sass } from './Sass.js'
11
12
  export { default as Term } from './Term.js'
12
13
  export { default as Type } from './Type.js'