@cobol-ts/debug 0.0.3 → 0.0.5
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/dist/index.js.map +1 -0
- package/dist/src/debug.d.ts +9 -0
- package/dist/src/debug.js +25 -0
- package/dist/src/debug.js.map +1 -0
- package/dist/src/debug.log.spec.d.ts +1 -0
- package/dist/src/debug.log.spec.js +62 -0
- package/dist/src/debug.log.spec.js.map +1 -0
- package/dist/src/debug.mock.d.ts +2 -0
- package/dist/src/debug.mock.js +12 -0
- package/dist/src/debug.mock.js.map +1 -0
- package/package.json +4 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAiC;AACjC,8CAA4B"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { NameAnd } from "@cobol-ts/records";
|
|
2
|
+
export type DebugState = NameAnd<boolean>;
|
|
3
|
+
export type DebugLog = DebugLogFn & {
|
|
4
|
+
debug: boolean;
|
|
5
|
+
debugError: DebugErrorFn;
|
|
6
|
+
};
|
|
7
|
+
export type DebugLogFn = (...msg: any[]) => void;
|
|
8
|
+
export type DebugErrorFn = (error: Error, ...msg: any[]) => void;
|
|
9
|
+
export declare function makeDebugLog(debugState: DebugState, name: string): DebugLog;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeDebugLog = makeDebugLog;
|
|
4
|
+
function makeDebugLog(debugState, name) {
|
|
5
|
+
const debug = debugState[name] ?? false;
|
|
6
|
+
if (debug) {
|
|
7
|
+
const debugLog = console.log.bind(console, name); // Attach component name to logs
|
|
8
|
+
const error = (error, ...msg) => {
|
|
9
|
+
console.error(`[ERROR] ${name}`, error, ...msg);
|
|
10
|
+
};
|
|
11
|
+
return Object.assign(debugLog, {
|
|
12
|
+
debug,
|
|
13
|
+
debugError: error,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
const debugLog = () => { };
|
|
18
|
+
const error = () => { };
|
|
19
|
+
return Object.assign(debugLog, {
|
|
20
|
+
debug,
|
|
21
|
+
debugError: error,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=debug.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug.js","sourceRoot":"","sources":["../../src/debug.ts"],"names":[],"mappings":";;AAeA,oCAqBC;AArBD,SAAgB,YAAY,CAAC,UAAsB,EAAE,IAAY;IAC7D,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC;IACxC,IAAI,KAAK,EAAE,CAAC;QACR,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAE,gCAAgC;QAEnF,MAAM,KAAK,GAAiB,CAAC,KAAY,EAAE,GAAG,GAAU,EAAE,EAAE;YACxD,OAAO,CAAC,KAAK,CAAC,WAAW,IAAI,EAAE,EAAE,KAAK,EAAE,GAAG,GAAG,CAAC,CAAC;QACpD,CAAC,CAAC;QAEF,OAAO,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE;YAC3B,KAAK;YACL,UAAU,EAAE,KAAK;SACpB,CAAC,CAAC;IACP,CAAC;SAAM,CAAC;QACJ,MAAM,QAAQ,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;QAC1B,MAAM,KAAK,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;QACvB,OAAO,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE;YAC3B,KAAK;YACL,UAAU,EAAE,KAAK;SACpB,CAAC,CAAC;IACP,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// Mock console methods
|
|
4
|
+
const debug_1 = require("./debug");
|
|
5
|
+
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();
|
|
6
|
+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
|
|
7
|
+
describe('makeDebugLog', () => {
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
jest.clearAllMocks();
|
|
10
|
+
});
|
|
11
|
+
it('logs messages when debug is enabled', () => {
|
|
12
|
+
const debugState = { componentA: true };
|
|
13
|
+
const log = (0, debug_1.makeDebugLog)(debugState, 'componentA');
|
|
14
|
+
log('Test message');
|
|
15
|
+
expect(consoleLogSpy).toHaveBeenCalledWith('componentA', 'Test message');
|
|
16
|
+
});
|
|
17
|
+
it('does not log messages when debug is disabled', () => {
|
|
18
|
+
const debugState = { componentA: false };
|
|
19
|
+
const log = (0, debug_1.makeDebugLog)(debugState, 'componentA');
|
|
20
|
+
log('Test message');
|
|
21
|
+
expect(consoleLogSpy).not.toHaveBeenCalled();
|
|
22
|
+
});
|
|
23
|
+
it('logs errors when debug is enabled', () => {
|
|
24
|
+
const debugState = { componentA: true };
|
|
25
|
+
const log = (0, debug_1.makeDebugLog)(debugState, 'componentA');
|
|
26
|
+
const error = new Error('Test error');
|
|
27
|
+
log.debugError(error, 'Additional info');
|
|
28
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith('[ERROR] componentA', error, 'Additional info');
|
|
29
|
+
});
|
|
30
|
+
it('does not log errors when debug is disabled', () => {
|
|
31
|
+
const debugState = { componentA: false };
|
|
32
|
+
const log = (0, debug_1.makeDebugLog)(debugState, 'componentA');
|
|
33
|
+
const error = new Error('Test error');
|
|
34
|
+
log.debugError(error, 'Additional info');
|
|
35
|
+
expect(consoleErrorSpy).not.toHaveBeenCalled();
|
|
36
|
+
});
|
|
37
|
+
it('defaults to no logging if debug state is undefined', () => {
|
|
38
|
+
const debugState = {};
|
|
39
|
+
const log = (0, debug_1.makeDebugLog)(debugState, 'componentB');
|
|
40
|
+
log('Should not log');
|
|
41
|
+
expect(consoleLogSpy).not.toHaveBeenCalled();
|
|
42
|
+
});
|
|
43
|
+
it('logs correctly for multiple components with different states', () => {
|
|
44
|
+
const debugState = { componentA: true, componentB: false };
|
|
45
|
+
const logA = (0, debug_1.makeDebugLog)(debugState, 'componentA');
|
|
46
|
+
const logB = (0, debug_1.makeDebugLog)(debugState, 'componentB');
|
|
47
|
+
logA('Message A');
|
|
48
|
+
logB('Message B');
|
|
49
|
+
expect(consoleLogSpy).toHaveBeenCalledWith('componentA', 'Message A');
|
|
50
|
+
expect(consoleLogSpy).not.toHaveBeenCalledWith('componentB', 'Message B');
|
|
51
|
+
});
|
|
52
|
+
it('preserves stack trace when logging', () => {
|
|
53
|
+
const debugState = { componentA: true };
|
|
54
|
+
const log = (0, debug_1.makeDebugLog)(debugState, 'componentA');
|
|
55
|
+
function logTest() {
|
|
56
|
+
log('Trace message');
|
|
57
|
+
}
|
|
58
|
+
logTest();
|
|
59
|
+
expect(consoleLogSpy.mock.calls[0][1]).toBe('Trace message');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
//# sourceMappingURL=debug.log.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug.log.spec.js","sourceRoot":"","sources":["../../src/debug.log.spec.ts"],"names":[],"mappings":";;AAEA,uBAAuB;AACvB,mCAAiD;AAEjD,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,kBAAkB,EAAE,CAAC;AACtE,MAAM,eAAe,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,kBAAkB,EAAE,CAAC;AAE1E,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC1B,SAAS,CAAC,GAAG,EAAE;QACX,IAAI,CAAC,aAAa,EAAE,CAAC;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AAE,YAAY,CAAC,CAAC;QAEnD,SAAS,OAAO;YACZ,GAAG,CAAC,eAAe,CAAC,CAAC;QACzB,CAAC;QAED,OAAO,EAAE,CAAC;QACV,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;IACjE,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createMockDebugLog = createMockDebugLog;
|
|
4
|
+
function createMockDebugLog() {
|
|
5
|
+
const mockLog = jest.fn();
|
|
6
|
+
const mockError = jest.fn();
|
|
7
|
+
return Object.assign(mockLog, {
|
|
8
|
+
debug: true,
|
|
9
|
+
debugError: mockError,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=debug.mock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug.mock.js","sourceRoot":"","sources":["../../src/debug.mock.ts"],"names":[],"mappings":";;AAEA,gDAOC;AAPD,SAAgB,kBAAkB;IAC9B,MAAM,OAAO,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC;IAC1B,MAAM,SAAS,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC;IAC5B,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE;QAC1B,KAAK,EAAE,IAAI;QACX,UAAU,EAAE,SAAS;KACxB,CAAC,CAAC;AACP,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cobol-ts/debug",
|
|
3
3
|
"description": "Debug utilities ",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.5",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"types": "dist/index",
|
|
7
7
|
"browserslist": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
]
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@cobol-ts/records": "0.0.
|
|
20
|
+
"@cobol-ts/records": "0.0.5"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"typescript": "^5.2.2",
|
|
@@ -46,7 +46,8 @@
|
|
|
46
46
|
"files": [
|
|
47
47
|
"dist/index.js",
|
|
48
48
|
"dist/index.d.ts",
|
|
49
|
-
"dist/src/"
|
|
49
|
+
"dist/src/",
|
|
50
|
+
"dist/**/*"
|
|
50
51
|
],
|
|
51
52
|
"keywords": [
|
|
52
53
|
"indexing"
|