@leostacowski/pkgs 0.0.0-stage → 0.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/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org>
package/README.md CHANGED
@@ -1,3 +1,74 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # @leostacowski/pkgs
2
+
3
+ _"It's ugly but it's mine."_
4
+
5
+ \- _Me describing my code._
6
+
7
+ ## Table of Contents
8
+
9
+ - [Motivation](#motivation)
10
+ - [Requirements](#requirements)
11
+ - [Installation](#installation)
12
+ - [Usage](#usage)
13
+ - [Modules](#modules)
14
+ - [@leostacowski/pkgs/logger](#leostacowskipkgslogger)
15
+
16
+ ## Motivation
17
+
18
+ After repeatedly starting things from scratch for a long time:
19
+
20
+ ```text
21
+ I decided to finally create a centralized repo for my scripts.
22
+ ```
23
+
24
+ This decision was directly inspired by Eskil Steenberg's talk: "_[You should finish your software](https://youtu.be/EGLoKbBn-VI?si=uFVlbKIcRolcOzCy)_".
25
+
26
+ If you are using any of these modules and something is breaking, or if you have a suggestion, please [open an issue](https://github.com/leostacowski/pkgs/issues/).
27
+
28
+ I intend to keep adding stuff to this repo as long as I continue to reinvent the wheel!
29
+
30
+ ## Requirements
31
+
32
+ `node >= 22.12.0` is a _safe choice_, but let me know your luck on older releases.
33
+
34
+ ## Installation
35
+
36
+ ```bash
37
+ npm i @leostacowski/pkgs
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ Everything begins at the `@leostacowski/pkgs` package:
43
+
44
+ ```js
45
+ import pkgs from '@leostacowski/pkgs'
46
+ // or
47
+ const pkgs = require('@leostacowski/pkgs').default
48
+ ```
49
+
50
+ Then import what you need:
51
+
52
+ ```js
53
+ import { ModuleX } from '@leostacowski/pkgs'
54
+ // or
55
+ const { ModuleX } = require('@leostacowski/pkgs')
56
+ ```
57
+
58
+ Or import a specific module and ignore the rest:
59
+
60
+ ```js
61
+ import ModuleY from '@leostacowski/pkgs/ModuleY'
62
+ // or
63
+ const ModuleY = require('@leostacowski/pkgs/ModuleY')
64
+ ```
65
+
66
+ For usage details on each module, refer to [Modules](#modules).
67
+
68
+ ## Modules
69
+
70
+ ### @leostacowski/pkgs/logger
71
+
72
+ A simple logger that supports aliases, types, colors and other knobs.
73
+
74
+ Check its [README.md](https://github.com/leostacowski/pkgs/blob/main/modules/logger/README.md) for details.
@@ -0,0 +1,7 @@
1
+ import LoggerModule from './logger/index.js'
2
+
3
+ export const Logger = LoggerModule
4
+
5
+ export default {
6
+ Logger,
7
+ }
@@ -0,0 +1,240 @@
1
+ # @leostacowski/pkgs/logger
2
+
3
+ Simple Logger that supports aliases, types, colors and other knobs.
4
+
5
+ > For the main package `@leostacowski/pkgs`: See the [main README.md](/README.md).
6
+
7
+ # Table of Contents
8
+
9
+ - [Usage](#usage)
10
+ - [Import](#import)
11
+ - [Options](#options)
12
+ - [_prefix_: `string`](#prefix-string)
13
+ - [_group_: `boolean`](#group-boolean)
14
+ - [_colors_: `boolean`](#colors-boolean)
15
+ - [_text_color_: `string`](#text_color-string)
16
+ - [_level_: `number`](#level-number)
17
+ - [Types](#types)
18
+ - [Examples](#examples)
19
+ - [Basic](#basic)
20
+ - [Prefix](#prefix)
21
+ - [Same Line](#same-line)
22
+ - [Level Filtering](#level-filtering)
23
+
24
+ # Usage
25
+
26
+ ## Import
27
+
28
+ From `@leostacowski/pkgs`:
29
+
30
+ ```js
31
+ import { Logger } from '@leostacowski/pkgs'
32
+ // or
33
+ const { Logger } = require('@leostacowski/pkgs')
34
+ ```
35
+
36
+ Or straight from the source:
37
+
38
+ ```js
39
+ import Logger from '@leostacowski/pkgs/logger'
40
+ // or
41
+ const Logger = require('@leostacowski/pkgs/logger')
42
+ ```
43
+
44
+ ## Options
45
+
46
+ When calling `Logger()`, you can customize your instance. Here are the defaults:
47
+
48
+ ```js
49
+ const logger = Logger({
50
+ prefix: '',
51
+ group: true,
52
+ colors: true,
53
+ text_color: '',
54
+ level: 0,
55
+ })
56
+ ```
57
+
58
+ Alternatively, pass a single `string` argument, and set just `prefix`:
59
+
60
+ ```js
61
+ const logger = Logger('My prefix 😉')
62
+ ```
63
+
64
+ You could just ignore everything:
65
+
66
+ ```js
67
+ const logger = Logger()
68
+ ```
69
+
70
+ ### _prefix_: `string`
71
+
72
+ Defines a value to append to the output prefix.
73
+
74
+ ### _group_: `boolean`
75
+
76
+ Toggles the use of [group](https://nodejs.org/api/console.html#consolegrouplabel) and [groupEnd](https://nodejs.org/api/console.html#consolegroupend).
77
+
78
+ If `true`, uses [_prefix_](#prefix-string) as the group label.
79
+
80
+ ### _colors_: `boolean`
81
+
82
+ Toggles the use of [ANSI colors](https://en.wikipedia.org/wiki/ANSI_escape_code) on console output. Here are the defaults:
83
+
84
+ ```js
85
+ {
86
+ RED: {
87
+ code: '\x1b[91m',
88
+ },
89
+ GREEN: {
90
+ code: '\x1b[92m',
91
+ },
92
+ YELLOW: {
93
+ code: '\x1b[93m',
94
+ },
95
+ DEFAULT: {
96
+ code: '\x1b[94m',
97
+ },
98
+ RESET: {
99
+ code: '\x1b[0m',
100
+ },
101
+ }
102
+ ```
103
+
104
+ ### _text_color_: `string`
105
+
106
+ Specify your own ANSI color code for console output text.
107
+
108
+ If [colors](#colors-boolean) is `false`, this option is ignored.
109
+
110
+ ### _level_: `number`
111
+
112
+ One of:
113
+
114
+ - `0`: Allows [ `DEBUG`, `SUCCESS`, `WARNING`, `ERROR` ]
115
+ - `1`: Allows [ `WARNING`, `ERROR` ]
116
+ - `2`: Allows [ `ERROR` ]
117
+
118
+ ## Types
119
+
120
+ The logger supports four types of output:
121
+
122
+ - **DEBUG**;
123
+ - **SUCCESS**;
124
+ - **WARNING**;
125
+ - **ERROR**.
126
+
127
+ Each level is aliased to support multiple ways of being emitted:
128
+
129
+ - **DEBUG**:
130
+ - `log`;
131
+ - `info`;
132
+ - `debug`;
133
+ - `l`;
134
+ - `i`;
135
+ - `d`.
136
+ - **SUCCESS**:
137
+ - `success`;
138
+ - `s`.
139
+ - **WARNING**:
140
+ - `warning`;
141
+ - `warn`;
142
+ - `w`.
143
+ - **ERROR**:
144
+ - `error`;
145
+ - `err`;
146
+ - `e`.
147
+
148
+ This means that all of this is game:
149
+
150
+ ```js
151
+ logger.log('"log" emits DEBUG')
152
+ logger.info('"info" emits DEBUG')
153
+ logger.debug('"debug" emits DEBUG')
154
+ logger.l('"l" emits DEBUG')
155
+ logger.i('"i" emits DEBUG')
156
+ logger.d('"d" emits DEBUG')
157
+ logger.success('"success" emits SUCESS')
158
+ logger.s('"s" emits SUCESS')
159
+ logger.warning('"warning" emits WARNING')
160
+ logger.warn('"warn" emits WARNING')
161
+ logger.w('"w" emits WARNING')
162
+ logger.error('"error" emits ERROR')
163
+ logger.err('"err" emits ERROR')
164
+ logger.e('"e" emits ERROR')
165
+ ```
166
+
167
+ ## Examples
168
+
169
+ ### Basic
170
+
171
+ ```js
172
+ import Logger from '@leostacowski/pkgs/logger'
173
+
174
+ const logger = Logger()
175
+
176
+ logger.log('Hello World!')
177
+ ```
178
+
179
+ Outputs:
180
+
181
+ ```bash
182
+ > [2026-11-07T04:30:28.965Z] DEBUG:
183
+ Hello World!
184
+ ```
185
+
186
+ ### Prefix
187
+
188
+ ```js
189
+ import Logger from '@leostacowski/pkgs/logger'
190
+
191
+ const logger = Logger('Hello')
192
+
193
+ logger.warn('World!')
194
+ ```
195
+
196
+ Outputs:
197
+
198
+ ```bash
199
+ > [2026-11-07T05:23:15.708Z] WARNING Hello:
200
+ World!
201
+ ```
202
+
203
+ ### Same Line
204
+
205
+ ```js
206
+ import Logger from '@leostacowski/pkgs/logger'
207
+
208
+ const logger = Logger({
209
+ group: false,
210
+ })
211
+
212
+ logger.info('Hello World!')
213
+ ```
214
+
215
+ Outputs:
216
+
217
+ ```bash
218
+ > [2026-11-07T05:25:23.193Z] DEBUG: Hello World!
219
+ ```
220
+
221
+ ### Level Filtering
222
+
223
+ ```js
224
+ import Logger from '@leostacowski/pkgs/logger'
225
+
226
+ const logger = Logger({
227
+ level: 2,
228
+ })
229
+
230
+ logger.log('0')
231
+ logger.warn('1')
232
+ logger.error('2')
233
+ ```
234
+
235
+ Outputs:
236
+
237
+ ```bash
238
+ > [2026-11-07T05:30:44.055Z] ERROR:
239
+ 2
240
+ ```
@@ -0,0 +1,152 @@
1
+ const LOGGER_COLORS = {
2
+ RED: {
3
+ code: '\x1b[91m',
4
+ },
5
+ GREEN: {
6
+ code: '\x1b[92m',
7
+ },
8
+ YELLOW: {
9
+ code: '\x1b[93m',
10
+ },
11
+ DEFAULT: {
12
+ code: '\x1b[94m',
13
+ },
14
+ RESET: {
15
+ code: '\x1b[0m',
16
+ },
17
+ }
18
+
19
+ const LOGGER_TYPES = [
20
+ {
21
+ level: 2,
22
+ prefix: 'ERROR',
23
+ emitter: console.error,
24
+ color: LOGGER_COLORS.RED,
25
+ aliases: ['error', 'err', 'e'],
26
+ },
27
+ {
28
+ level: 1,
29
+ prefix: 'WARNING',
30
+ emitter: console.warn,
31
+ color: LOGGER_COLORS.YELLOW,
32
+ aliases: ['warning', 'warn', 'w'],
33
+ },
34
+ {
35
+ level: 0,
36
+ prefix: 'SUCCESS',
37
+ emitter: console.log,
38
+ color: LOGGER_COLORS.GREEN,
39
+ aliases: ['success', 's'],
40
+ },
41
+ {
42
+ level: 0,
43
+ prefix: 'DEBUG',
44
+ emitter: console.log,
45
+ color: LOGGER_COLORS.DEFAULT,
46
+ aliases: ['log', 'info', 'debug', 'l', 'i', 'd'],
47
+ },
48
+ ]
49
+
50
+ const BASE_OPTS = {
51
+ prefix: '',
52
+ group: true,
53
+ colors: true,
54
+ text_color: '',
55
+ level: 0,
56
+ }
57
+
58
+ function emit(content = [], opts = {}, logger_type = {}) {
59
+ if (opts?.level > logger_type.level) return
60
+ if (!content.length) return
61
+
62
+ const prefix = (() => {
63
+ const target = []
64
+
65
+ target.push(`> [${new Date().toISOString()}] ${logger_type.prefix}`)
66
+
67
+ if (opts?.prefix) {
68
+ target.push(opts.prefix)
69
+ }
70
+
71
+ target[target.length - 1] = `${target[target.length - 1]}:`
72
+
73
+ if (opts?.colors) {
74
+ target.unshift(logger_type.color.code)
75
+ target.push(LOGGER_COLORS.RESET.code)
76
+ }
77
+
78
+ return target.join(' ')
79
+ })()
80
+
81
+ const message = (() => {
82
+ const target = []
83
+ let text_color = ''
84
+
85
+ for (const item of content) {
86
+ target.push(typeof item === 'string' ? item : JSON.stringify(item))
87
+ }
88
+
89
+ if (opts?.colors && opts?.text_color) {
90
+ text_color = opts.text_color
91
+ }
92
+
93
+ if (text_color) {
94
+ return `${text_color}${target.join()}${LOGGER_COLORS.RESET.code}`
95
+ }
96
+
97
+ return target.join(' ')
98
+ })()
99
+
100
+ if (opts.group) {
101
+ console.group(prefix)
102
+ logger_type.emitter(message)
103
+ console.groupEnd()
104
+ } else {
105
+ logger_type.emitter(prefix, message)
106
+ }
107
+
108
+ return
109
+ }
110
+
111
+ function get_emitters(args) {
112
+ const opts = {}
113
+
114
+ Object.keys(BASE_OPTS).forEach((key) => {
115
+ if (typeof args?.[key] === typeof BASE_OPTS[key]) {
116
+ opts[key] = args[key]
117
+ } else {
118
+ opts[key] = BASE_OPTS[key]
119
+ }
120
+ })
121
+
122
+ const target = {}
123
+
124
+ for (const logger_type of LOGGER_TYPES) {
125
+ for (const alias of logger_type.aliases) {
126
+ target[alias] = function () {
127
+ return emit(Array.from(arguments), opts, logger_type)
128
+ }
129
+ }
130
+ }
131
+
132
+ return target
133
+ }
134
+
135
+ const Logger = function (args) {
136
+ if (typeof args === 'string') {
137
+ args = {
138
+ prefix: args,
139
+ }
140
+ }
141
+
142
+ const emitters = get_emitters(args)
143
+ const methods = {}
144
+
145
+ for (const emitter of Object.keys(emitters)) {
146
+ methods[emitter] = emitters[emitter]
147
+ }
148
+
149
+ return methods
150
+ }
151
+
152
+ export default Logger
package/package.json CHANGED
@@ -1,6 +1,45 @@
1
- {
2
- "name": "@leostacowski/pkgs",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@leostacowski/pkgs",
3
+ "version": "0.0.1",
4
+ "description": "Centralized package of reusable JavaScript tools.",
5
+ "homepage": "https://www.npmjs.com/package/@leostacowski/pkgs",
6
+ "keywords": [
7
+ "nodejs",
8
+ "javascript",
9
+ "npm"
10
+ ],
11
+ "engines": {
12
+ "node": ">=22.12.0"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/leostacowski/pkgs/issues"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/leostacowski/pkgs.git"
20
+ },
21
+ "license": "Unlicense",
22
+ "author": {
23
+ "name": "leostacowski",
24
+ "email": "leonardo.stacowski@hotmail.com",
25
+ "url": "git+https://github.com/leostacowski/"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "type": "module",
31
+ "files": [
32
+ "modules"
33
+ ],
34
+ "exports": {
35
+ ".": "./modules/index.js",
36
+ "./logger": "./modules/logger/index.js"
37
+ },
38
+ "imports": {
39
+ "#logger/*": "./modules/logger/*"
40
+ },
41
+ "scripts": {
42
+ "dev": "node --watch ./modules/index.js",
43
+ "deploy": "npm publish"
44
+ }
45
+ }