Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into main
  • Loading branch information
Coly010 committed Mar 4, 2021
2 parents a85f4a1 + a608a0f commit c18c6e9
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 33 deletions.
57 changes: 48 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ We've all had occasions where we've felt the need to simply pipe a `tap(console.

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

_P.S. This did originate as a meme idea :P_

## Usage

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

You can add a label to help identify the Observable:

```ts
const obs$ = source.pipe(debug('My Observable'));
// OUTPUT
// My Observable {value}
```

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:

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

### Examples

We can use it on it's own to simply log out values to the console
We can use it on its own to simply log out values to the console

```ts
const obs$ = of('my test value');
Expand All @@ -60,6 +66,38 @@ obs$.pipe(debug()).subscribe();
// my test value
```

We can add a label to the logs:

```ts
const obs$ = of('my test value');
obs$.pipe(debug('Obserable A')).subscribe();

// OUTPUT:
// Obserable A my test value

// We can label it using the config object syntax:
const obs$ = of('my test value');
obs$.pipe(debug({ label: 'Obserable A' })).subscribe();

// OUTPUT:
// Obserable A my test value

// However, if we add a label and custom notification handlers,
// we will not get the label in the logs by default:
const obs$ = of('my test value');
obs$
.pipe(
debug({
label: 'Obserable A',
next: (value) => console.log(value),
})
)
.subscribe();

// OUTPUT:
// my test value
```

We can also set up our own notification handlers if we prefer:

```ts
Expand Down Expand Up @@ -98,9 +136,10 @@ obs$

See the list of options available to configure the operator below

| Option | Description | Type | Default |
| -------------- | :----------------------------------------------------------------: | ------------------------- | --------------- |
| `shouldIgnore` | Do not perform the Debug actions | `boolean` | `false` |
| `next` | Action to perform when Observer receives a Next notification | `(value: T) => void` | `console.log` |
| `error` | Action to perform when Observer receives an Error notification | `(value: unkown) => void` | `console.error` |
| `complete` | Action to perform when Observer receives a Completion notification | `() => void` | `() => null` |
| Option | Description | Type | Default |
| -------------- | :----------------------------------------------------------------: | -------------------------- | --------------- |
| `shouldIgnore` | Do not perform the Debug actions | `boolean` | `false` |
| `label` | Add a label to the logs to help identify the Observable | `string` | `null` |
| `next` | Action to perform when Observer receives a Next notification | `(value: T) => void` | `console.log` |
| `error` | Action to perform when Observer receives an Error notification | `(value: unknown) => void` | `console.error` |
| `complete` | Action to perform when Observer receives a Completion notification | `() => void` | `() => null` |
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rxjs-debug-operator",
"version": "1.1.1",
"version": "1.2.0",
"license": "MIT",
"scripts": {
"nx": "nx",
Expand Down
57 changes: 48 additions & 9 deletions packages/operators/debug/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ We've all had occasions where we've felt the need to simply pipe a `tap(console.

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

_P.S. This did originate as a meme idea :P_

## Usage

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

You can add a label to help identify the Observable:

```ts
const obs$ = source.pipe(debug('My Observable'));
// OUTPUT
// My Observable {value}
```

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:

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

### Examples

We can use it on it's own to simply log out values to the console
We can use it on its own to simply log out values to the console

```ts
const obs$ = of('my test value');
Expand All @@ -60,6 +66,38 @@ obs$.pipe(debug()).subscribe();
// my test value
```

We can add a label to the logs:

```ts
const obs$ = of('my test value');
obs$.pipe(debug('Obserable A')).subscribe();

// OUTPUT:
// Obserable A my test value

// We can label it using the config object syntax:
const obs$ = of('my test value');
obs$.pipe(debug({ label: 'Obserable A' })).subscribe();

// OUTPUT:
// Obserable A my test value

// However, if we add a label and custom notification handlers,
// we will not get the label in the logs by default:
const obs$ = of('my test value');
obs$
.pipe(
debug({
label: 'Obserable A',
next: (value) => console.log(value),
})
)
.subscribe();

// OUTPUT:
// my test value
```

We can also set up our own notification handlers if we prefer:

```ts
Expand Down Expand Up @@ -98,9 +136,10 @@ obs$

See the list of options available to configure the operator below

| Option | Description | Type | Default |
| -------------- | :----------------------------------------------------------------: | ------------------------- | --------------- |
| `shouldIgnore` | Do not perform the Debug actions | `boolean` | `false` |
| `next` | Action to perform when Observer receives a Next notification | `(value: T) => void` | `console.log` |
| `error` | Action to perform when Observer receives an Error notification | `(value: unkown) => void` | `console.error` |
| `complete` | Action to perform when Observer receives a Completion notification | `() => void` | `() => null` |
| Option | Description | Type | Default |
| -------------- | :----------------------------------------------------------------: | -------------------------- | --------------- |
| `shouldIgnore` | Do not perform the Debug actions | `boolean` | `false` |
| `label` | Add a label to the logs to help identify the Observable | `string` | `null` |
| `next` | Action to perform when Observer receives a Next notification | `(value: T) => void` | `console.log` |
| `error` | Action to perform when Observer receives an Error notification | `(value: unknown) => void` | `console.error` |
| `complete` | Action to perform when Observer receives a Completion notification | `() => void` | `() => null` |
2 changes: 1 addition & 1 deletion packages/operators/debug/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rxjs-debug-operator",
"version": "1.1.1",
"version": "1.2.0",
"license": "MIT",
"repository": {
"url": "https://github.com/Coly010/rxjs-debug-operator"
Expand Down
143 changes: 139 additions & 4 deletions packages/operators/debug/src/lib/operators-debug.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('operators - debug', () => {
});

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

beforeEach(
() =>
Expand Down Expand Up @@ -223,7 +223,7 @@ describe('operators - debug', () => {
describe('and when shouldIgnore is true', () => {
beforeEach(() => (config.shouldIgnore = true));

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

Expand All @@ -237,7 +237,7 @@ describe('operators - debug', () => {
);
});

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

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

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

Expand All @@ -264,4 +264,139 @@ describe('operators - debug', () => {
});
});
});

describe('when a label is used', () => {
describe('using a string param', () => {
it('should log value to the console when next notification receieved with the label', () => {
// ARRANGE
const obs$ = of('my test value');

// ACT
obs$.pipe(debug('Test Label')).subscribe();

// ASSERT
expect(console.log).toHaveBeenCalledWith('Test Label', 'my test value');
});

it('should error value to the console when error notification receieved with the label', () => {
// ARRANGE
const obs$ = throwError('my error value');

// ACT
obs$.pipe(debug('Test Label')).subscribe();

// ASSERT
expect(console.error).toHaveBeenCalledWith(
'Test Label',
'my error value'
);
});
});

describe('using the config object', () => {
let config: Partial<DebugOperatorConfig<string>>;

beforeEach(
() =>
(config = {
shouldIgnore: false,
label: 'Test Label',
})
);

describe('and when shouldIgnore is false', () => {
beforeEach(() => (config.shouldIgnore = false));

it('should log value to the console when next notification receieved', () => {
// ARRANGE
const obs$ = of('my test value');

// ACT
obs$.pipe(debug(config)).subscribe();

// ASSERT
expect(console.log).toHaveBeenCalledWith(
'Test Label',
'my test value'
);
});

it('should error value to the console when error notification receieved', () => {
// ARRANGE
const obs$ = throwError('my error value');

// ACT
obs$.pipe(debug(config)).subscribe();

// ASSERT
expect(console.error).toHaveBeenCalledWith(
'Test Label',
'my error value'
);
});

it('should log value to the console when complete notification receieved', () => {
// ARRANGE
const obs$ = of('my test value');

// ACT
obs$.pipe(debug(config)).subscribe();

// ASSERT
expect(console.log).toHaveBeenCalledTimes(2);
expect(console.log).toHaveBeenNthCalledWith(
2,
'Test Label completed'
);
});
});

describe('and when shouldIgnore is true', () => {
beforeEach(() => (config.shouldIgnore = true));

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

// ACT
obs$.pipe(debug(config)).subscribe();

// ASSERT
expect(console.log).not.toHaveBeenCalledWith(
'Test Label',
'my test value'
);
});

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

// ACT
obs$.pipe(debug(config)).subscribe();

// ASSERT
expect(console.error).not.toHaveBeenCalledWith(
'Test Label',
'my error value'
);
});

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

// ACT
obs$.pipe(debug(config)).subscribe();

// ASSERT
expect(console.log).not.toHaveBeenCalledTimes(2);
expect(console.log).not.toHaveBeenNthCalledWith(
2,
'Test Label completed'
);
});
});
});
});
});

0 comments on commit c18c6e9

Please sign in to comment.