Skip to content

Commit c18c6e9

Browse files
committed
Merge branch 'main' of https://github.com/Coly010/rxjs-debug-operator into main
2 parents a85f4a1 + a608a0f commit c18c6e9

6 files changed

Lines changed: 250 additions & 33 deletions

File tree

README.md

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ We've all had occasions where we've felt the need to simply pipe a `tap(console.
1515

1616
This operator aims to reduce the amount of typing you'll have to do!
1717

18-
_P.S. This did originate as a meme idea :P_
19-
2018
## Usage
2119

2220
### Installation
@@ -42,6 +40,14 @@ Then pipe it to your Observables:
4240
const obs$ = source.pipe(debug());
4341
```
4442

43+
You can add a label to help identify the Observable:
44+
45+
```ts
46+
const obs$ = source.pipe(debug('My Observable'));
47+
// OUTPUT
48+
// My Observable {value}
49+
```
50+
4551
It even allows you to turn it off if you are in a production environment, or for any other reason you wouldn't want to log to the console:
4652

4753
```ts
@@ -50,7 +56,7 @@ const obs$ = source.pipe(debug({ shouldIgnore: true }));
5056

5157
### Examples
5258

53-
We can use it on it's own to simply log out values to the console
59+
We can use it on its own to simply log out values to the console
5460

5561
```ts
5662
const obs$ = of('my test value');
@@ -60,6 +66,38 @@ obs$.pipe(debug()).subscribe();
6066
// my test value
6167
```
6268

69+
We can add a label to the logs:
70+
71+
```ts
72+
const obs$ = of('my test value');
73+
obs$.pipe(debug('Obserable A')).subscribe();
74+
75+
// OUTPUT:
76+
// Obserable A my test value
77+
78+
// We can label it using the config object syntax:
79+
const obs$ = of('my test value');
80+
obs$.pipe(debug({ label: 'Obserable A' })).subscribe();
81+
82+
// OUTPUT:
83+
// Obserable A my test value
84+
85+
// However, if we add a label and custom notification handlers,
86+
// we will not get the label in the logs by default:
87+
const obs$ = of('my test value');
88+
obs$
89+
.pipe(
90+
debug({
91+
label: 'Obserable A',
92+
next: (value) => console.log(value),
93+
})
94+
)
95+
.subscribe();
96+
97+
// OUTPUT:
98+
// my test value
99+
```
100+
63101
We can also set up our own notification handlers if we prefer:
64102

65103
```ts
@@ -98,9 +136,10 @@ obs$
98136

99137
See the list of options available to configure the operator below
100138

101-
| Option | Description | Type | Default |
102-
| -------------- | :----------------------------------------------------------------: | ------------------------- | --------------- |
103-
| `shouldIgnore` | Do not perform the Debug actions | `boolean` | `false` |
104-
| `next` | Action to perform when Observer receives a Next notification | `(value: T) => void` | `console.log` |
105-
| `error` | Action to perform when Observer receives an Error notification | `(value: unkown) => void` | `console.error` |
106-
| `complete` | Action to perform when Observer receives a Completion notification | `() => void` | `() => null` |
139+
| Option | Description | Type | Default |
140+
| -------------- | :----------------------------------------------------------------: | -------------------------- | --------------- |
141+
| `shouldIgnore` | Do not perform the Debug actions | `boolean` | `false` |
142+
| `label` | Add a label to the logs to help identify the Observable | `string` | `null` |
143+
| `next` | Action to perform when Observer receives a Next notification | `(value: T) => void` | `console.log` |
144+
| `error` | Action to perform when Observer receives an Error notification | `(value: unknown) => void` | `console.error` |
145+
| `complete` | Action to perform when Observer receives a Completion notification | `() => void` | `() => null` |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rxjs-debug-operator",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"license": "MIT",
55
"scripts": {
66
"nx": "nx",

packages/operators/debug/README.md

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ We've all had occasions where we've felt the need to simply pipe a `tap(console.
1515

1616
This operator aims to reduce the amount of typing you'll have to do!
1717

18-
_P.S. This did originate as a meme idea :P_
19-
2018
## Usage
2119

2220
### Installation
@@ -42,6 +40,14 @@ Then pipe it to your Observables:
4240
const obs$ = source.pipe(debug());
4341
```
4442

43+
You can add a label to help identify the Observable:
44+
45+
```ts
46+
const obs$ = source.pipe(debug('My Observable'));
47+
// OUTPUT
48+
// My Observable {value}
49+
```
50+
4551
It even allows you to turn it off if you are in a production environment, or for any other reason you wouldn't want to log to the console:
4652

4753
```ts
@@ -50,7 +56,7 @@ const obs$ = source.pipe(debug({ shouldIgnore: true }));
5056

5157
### Examples
5258

53-
We can use it on it's own to simply log out values to the console
59+
We can use it on its own to simply log out values to the console
5460

5561
```ts
5662
const obs$ = of('my test value');
@@ -60,6 +66,38 @@ obs$.pipe(debug()).subscribe();
6066
// my test value
6167
```
6268

69+
We can add a label to the logs:
70+
71+
```ts
72+
const obs$ = of('my test value');
73+
obs$.pipe(debug('Obserable A')).subscribe();
74+
75+
// OUTPUT:
76+
// Obserable A my test value
77+
78+
// We can label it using the config object syntax:
79+
const obs$ = of('my test value');
80+
obs$.pipe(debug({ label: 'Obserable A' })).subscribe();
81+
82+
// OUTPUT:
83+
// Obserable A my test value
84+
85+
// However, if we add a label and custom notification handlers,
86+
// we will not get the label in the logs by default:
87+
const obs$ = of('my test value');
88+
obs$
89+
.pipe(
90+
debug({
91+
label: 'Obserable A',
92+
next: (value) => console.log(value),
93+
})
94+
)
95+
.subscribe();
96+
97+
// OUTPUT:
98+
// my test value
99+
```
100+
63101
We can also set up our own notification handlers if we prefer:
64102

65103
```ts
@@ -98,9 +136,10 @@ obs$
98136

99137
See the list of options available to configure the operator below
100138

101-
| Option | Description | Type | Default |
102-
| -------------- | :----------------------------------------------------------------: | ------------------------- | --------------- |
103-
| `shouldIgnore` | Do not perform the Debug actions | `boolean` | `false` |
104-
| `next` | Action to perform when Observer receives a Next notification | `(value: T) => void` | `console.log` |
105-
| `error` | Action to perform when Observer receives an Error notification | `(value: unkown) => void` | `console.error` |
106-
| `complete` | Action to perform when Observer receives a Completion notification | `() => void` | `() => null` |
139+
| Option | Description | Type | Default |
140+
| -------------- | :----------------------------------------------------------------: | -------------------------- | --------------- |
141+
| `shouldIgnore` | Do not perform the Debug actions | `boolean` | `false` |
142+
| `label` | Add a label to the logs to help identify the Observable | `string` | `null` |
143+
| `next` | Action to perform when Observer receives a Next notification | `(value: T) => void` | `console.log` |
144+
| `error` | Action to perform when Observer receives an Error notification | `(value: unknown) => void` | `console.error` |
145+
| `complete` | Action to perform when Observer receives a Completion notification | `() => void` | `() => null` |

packages/operators/debug/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rxjs-debug-operator",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"license": "MIT",
55
"repository": {
66
"url": "https://github.com/Coly010/rxjs-debug-operator"

packages/operators/debug/src/lib/operators-debug.spec.ts

Lines changed: 139 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('operators - debug', () => {
5757
});
5858

5959
describe('when custom handlers are used', () => {
60-
let config: DebugOperatorConfig<string>;
60+
let config: Partial<DebugOperatorConfig<string>>;
6161

6262
beforeEach(
6363
() =>
@@ -223,7 +223,7 @@ describe('operators - debug', () => {
223223
describe('and when shouldIgnore is true', () => {
224224
beforeEach(() => (config.shouldIgnore = true));
225225

226-
it('should log value to the console when next notification receieved', () => {
226+
it('should not log value to the console when next notification receieved', () => {
227227
// ARRANGE
228228
const obs$ = of('my test value');
229229

@@ -237,7 +237,7 @@ describe('operators - debug', () => {
237237
);
238238
});
239239

240-
it('should error value to the console when error notification receieved', () => {
240+
it('should not error value to the console when error notification receieved', () => {
241241
// ARRANGE
242242
const obs$ = throwError('my error value');
243243

@@ -248,7 +248,7 @@ describe('operators - debug', () => {
248248
expect(console.error).not.toHaveBeenCalledWith('my error value');
249249
});
250250

251-
it('should log value to the console when complete notification receieved', () => {
251+
it('should not log value to the console when complete notification receieved', () => {
252252
// ARRANGE
253253
const obs$ = of('my test value');
254254

@@ -264,4 +264,139 @@ describe('operators - debug', () => {
264264
});
265265
});
266266
});
267+
268+
describe('when a label is used', () => {
269+
describe('using a string param', () => {
270+
it('should log value to the console when next notification receieved with the label', () => {
271+
// ARRANGE
272+
const obs$ = of('my test value');
273+
274+
// ACT
275+
obs$.pipe(debug('Test Label')).subscribe();
276+
277+
// ASSERT
278+
expect(console.log).toHaveBeenCalledWith('Test Label', 'my test value');
279+
});
280+
281+
it('should error value to the console when error notification receieved with the label', () => {
282+
// ARRANGE
283+
const obs$ = throwError('my error value');
284+
285+
// ACT
286+
obs$.pipe(debug('Test Label')).subscribe();
287+
288+
// ASSERT
289+
expect(console.error).toHaveBeenCalledWith(
290+
'Test Label',
291+
'my error value'
292+
);
293+
});
294+
});
295+
296+
describe('using the config object', () => {
297+
let config: Partial<DebugOperatorConfig<string>>;
298+
299+
beforeEach(
300+
() =>
301+
(config = {
302+
shouldIgnore: false,
303+
label: 'Test Label',
304+
})
305+
);
306+
307+
describe('and when shouldIgnore is false', () => {
308+
beforeEach(() => (config.shouldIgnore = false));
309+
310+
it('should log value to the console when next notification receieved', () => {
311+
// ARRANGE
312+
const obs$ = of('my test value');
313+
314+
// ACT
315+
obs$.pipe(debug(config)).subscribe();
316+
317+
// ASSERT
318+
expect(console.log).toHaveBeenCalledWith(
319+
'Test Label',
320+
'my test value'
321+
);
322+
});
323+
324+
it('should error value to the console when error notification receieved', () => {
325+
// ARRANGE
326+
const obs$ = throwError('my error value');
327+
328+
// ACT
329+
obs$.pipe(debug(config)).subscribe();
330+
331+
// ASSERT
332+
expect(console.error).toHaveBeenCalledWith(
333+
'Test Label',
334+
'my error value'
335+
);
336+
});
337+
338+
it('should log value to the console when complete notification receieved', () => {
339+
// ARRANGE
340+
const obs$ = of('my test value');
341+
342+
// ACT
343+
obs$.pipe(debug(config)).subscribe();
344+
345+
// ASSERT
346+
expect(console.log).toHaveBeenCalledTimes(2);
347+
expect(console.log).toHaveBeenNthCalledWith(
348+
2,
349+
'Test Label completed'
350+
);
351+
});
352+
});
353+
354+
describe('and when shouldIgnore is true', () => {
355+
beforeEach(() => (config.shouldIgnore = true));
356+
357+
it('should not log value to the console when next notification receieved', () => {
358+
// ARRANGE
359+
const obs$ = of('my test value');
360+
361+
// ACT
362+
obs$.pipe(debug(config)).subscribe();
363+
364+
// ASSERT
365+
expect(console.log).not.toHaveBeenCalledWith(
366+
'Test Label',
367+
'my test value'
368+
);
369+
});
370+
371+
it('should not error value to the console when error notification receieved', () => {
372+
// ARRANGE
373+
const obs$ = throwError('my error value');
374+
375+
// ACT
376+
obs$.pipe(debug(config)).subscribe();
377+
378+
// ASSERT
379+
expect(console.error).not.toHaveBeenCalledWith(
380+
'Test Label',
381+
'my error value'
382+
);
383+
});
384+
385+
it('should not log value to the console when complete notification receieved', () => {
386+
// ARRANGE
387+
const obs$ = of('my test value');
388+
389+
// ACT
390+
obs$.pipe(debug(config)).subscribe();
391+
392+
// ASSERT
393+
expect(console.log).not.toHaveBeenCalledTimes(2);
394+
expect(console.log).not.toHaveBeenNthCalledWith(
395+
2,
396+
'Test Label completed'
397+
);
398+
});
399+
});
400+
});
401+
});
267402
});

0 commit comments

Comments
 (0)