debug-fabulous 2.0.43 → 2.0.44

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 (2) hide show
  1. package/README.md +115 -37
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,54 +1,132 @@
1
- # debug-fabulous [![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Test coverage][coveralls-image]][coveralls-url]
1
+ # debug-fabulous
2
+
3
+ **Lazy-evaluation wrapper for [`debug`](https://github.com/debug-js/debug) — skip string building entirely when logging is disabled.**
4
+
5
+ [![npm version](https://img.shields.io/npm/v/debug-fabulous.svg)](https://www.npmjs.com/package/debug-fabulous)
6
+ [![npm downloads](https://img.shields.io/npm/dw/debug-fabulous.svg)](https://www.npmjs.com/package/debug-fabulous)
7
+ [![tests](https://github.com/brickhouse-tech/debug-fabulous/actions/workflows/tests.yml/badge.svg)](https://github.com/brickhouse-tech/debug-fabulous/actions/workflows/tests.yml)
8
+
9
+ ## Why?
10
+
11
+ The `debug` module always evaluates its arguments, even when the namespace is disabled. If you're building expensive strings — JSON serialization, large object inspection, string concatenation — you pay the cost even when nobody's reading the output.
12
+
13
+ **debug-fabulous** wraps `debug` with lazy evaluation. Pass a function instead of a string, and it only runs when the namespace is actually enabled. When logging is off, the call is a no-op — no string allocation, no concatenation, no wasted cycles.
14
+
15
+ This matters at scale. Libraries like [`gulp-sourcemaps`](https://www.npmjs.com/package/gulp-sourcemaps) (700K+ weekly downloads) use debug-fabulous as a transitive dependency for exactly this reason.
2
16
 
3
17
  ## Install
4
18
 
5
- `npm install --save debug-fabulous`
19
+ ```bash
20
+ npm install debug-fabulous
21
+ ```
22
+
23
+ ## Quick Start
6
24
 
7
- ## Purpose:
25
+ ```js
26
+ const debugFab = require('debug-fabulous');
27
+ const debug = debugFab()('my-app');
8
28
 
9
- Wrapper / Extension around [visionmedia's debug](https://github.com/visionmedia/debug) to allow lazy evaluation of debugging via closure handling.
29
+ // Lazy evaluation the function only runs if 'my-app' is enabled
30
+ debug(() => 'user object: ' + JSON.stringify(largeUserObject));
10
31
 
11
- ## Why would I consider using this library?
32
+ // Plain strings still work
33
+ debug('server started on port %d', 3000);
34
+ ```
12
35
 
13
- The main utilities added to this library is lazy log level evaluation. This allows whatever logged strings to only be created and evaluated if a log level is active. This can considerably reduce the amount of memory used in logging when you are not using.
36
+ ### Spawning Child Debuggers
14
37
 
15
- With this in mind, there are no excuses to not log anything and everything as performance can be kept in check easily (via log levels).
38
+ Create hierarchical namespaces without string juggling:
16
39
 
17
- ### Proof
40
+ ```js
41
+ const debugFab = require('debug-fabulous');
42
+ const debug = debugFab()('my-app');
18
43
 
19
- For analysis of the performance results are in [perfWith.out](./test/perf/perfWith.out) and [perfWithout.out](./test/perf/perfWithout.out).
44
+ const dbDebug = debug.spawn('db'); // my-app:db
45
+ const queryDebug = dbDebug.spawn('query'); // my-app:db:query
20
46
 
21
- In summary, the tests using this library are using 3 times less memory for the same logging statements (when the log levels are disabled).
47
+ dbDebug('connected');
48
+ queryDebug(() => `SELECT took ${ms}ms, returned ${rows.length} rows`);
49
+ ```
22
50
 
23
- ## This library essentially wraps two things:
51
+ ### Standalone Spawnable
24
52
 
25
- - [lazy-eval](./src/lazy-eval.js): debug closure handling
26
- - [spawn](./src/spawn.js): spawns off existing namespaces for a sub namespace.
53
+ If you just need hierarchical debuggers without the factory:
27
54
 
28
- ## Example:
55
+ ```js
56
+ const { spawnable } = require('debug-fabulous');
57
+ const debug = spawnable('my-app');
58
+
59
+ const child = debug.spawn('worker'); // my-app:worker
60
+ child('processing job %d', jobId);
61
+ ```
62
+
63
+ ## TypeScript
64
+
65
+ debug-fabulous is written in TypeScript and ships type declarations.
66
+
67
+ ```ts
68
+ import debugFab, { spawnable } from 'debug-fabulous';
69
+
70
+ const debug = debugFab()('my-app');
71
+
72
+ // Lazy eval with type safety
73
+ debug(() => `processed ${items.length} items`);
74
+
75
+ // Spawn children
76
+ const child = debug.spawn('worker');
77
+ child('ready');
78
+
79
+ // Return an array for format strings
80
+ debug(() => ['found %d results in %dms', count, elapsed]);
81
+ ```
29
82
 
30
- For usage see the [tests](./test) or the example below.
83
+ ## API
84
+
85
+ ### `debugFab(debugApi?)`
86
+
87
+ Returns a wrapped `debug` factory with lazy evaluation and namespace caching.
88
+
89
+ - **`debugApi`** *(optional)* — a custom `debug` instance. Defaults to `require('debug')`.
90
+
91
+ The returned factory has the same API as `debug` (`enable()`, `disable()`, `load()`, `save()`, etc.) plus lazy evaluation support.
92
+
93
+ ### `debug(fn)` — Lazy Evaluation
94
+
95
+ Pass a function instead of a string. It's only called when the namespace is enabled:
96
+
97
+ ```js
98
+ // Function returns a string
99
+ debug(() => expensiveStringOperation());
100
+
101
+ // Function returns [formatter, ...args] array
102
+ debug(() => ['user %s performed %d actions', userName, count]);
103
+ ```
104
+
105
+ ### `debug.spawn(namespace)`
106
+
107
+ Creates a child debugger under the current namespace:
108
+
109
+ ```js
110
+ const root = debug('app'); // app
111
+ const db = root.spawn('db'); // app:db
112
+ const cache = db.spawn('cache'); // app:db:cache
113
+ ```
114
+
115
+ ### `spawnable(namespace, debugFabFactory?)`
116
+
117
+ Standalone function that creates a spawnable debugger directly:
31
118
 
32
119
  ```js
33
- var debug = require('')();
34
- // force namespace to be enabled otherwise it assumes process.env.DEBUG is setup
35
- // debug.save('namespace');
36
- // debug.enable(debug.load())
37
- debug = debug('namespace'); // debugger in the namespace
38
- debug(function() {
39
- return 'something to log' + someLargeHarryString;
40
- });
41
- debug(() => 'something to log ${someLargeHarryString}');
42
- debug('small out'); // prints namespace small out
43
- var childDbg = debug.spawn('child'); // debugger in the namespace:child
44
- childDbg('small out'); // prints namespace:child small out
45
- var grandChildDbg = debug.spawn('grandChild'); // debugger in the namespace:child:grandChild
46
- grandChildDbg('small out'); // prints namespace:child:grandChild small out
47
- ```
48
-
49
- [npm-image]: https://img.shields.io/npm/v/debug-fabulous.svg
50
- [npm-url]: https://www.npmjs.com/package/debug-fabulous
51
- [travis-image]: https://img.shields.io/travis/nmccready/debug-fabulous.svg
52
- [travis-url]: https://travis-ci.org/nmccready/debug-fabulous
53
- [coveralls-image]: https://coveralls.io/repos/github/nmccready/debug-fabulous/badge.svg
54
- [coveralls-url]: https://coveralls.io/github/nmccready/debug-fabulous?branch=master
120
+ const { spawnable } = require('debug-fabulous');
121
+ const debug = spawnable('app');
122
+ ```
123
+
124
+ ## How It Works
125
+
126
+ 1. **Namespace caching** — `Map`-based memoization means repeated `debug('same-ns')` calls return the same instance instantly.
127
+ 2. **Singleton no-op** — Disabled namespaces get a shared no-op function instead of allocating per-instance.
128
+ 3. **Lazy closures** — When you pass a function, it's never invoked if the namespace is disabled. No string allocation, no concatenation, no `JSON.stringify()`.
129
+
130
+ ## License
131
+
132
+ [MIT](./LICENSE) Nicholas McCready and [contributors](https://github.com/brickhouse-tech/debug-fabulous/graphs/contributors).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "debug-fabulous",
3
- "version": "2.0.43",
3
+ "version": "2.0.44",
4
4
  "description": "visionmedia debug extensions rolled into one",
5
5
  "keywords": [
6
6
  "debug",