Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/first.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Returns the first element to be returned by an Observable
*
* @param {Observable} input Input Observable
* @return {Observable} New Observable
*/

const first = input => {
let out;
input.subscribe(x => {
if (!out) out = x;
});
return out;
};

first._name = 'first';

export default first;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need it to propagate errors and complete messages as well. What if it errors or completes before the first one?

3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import defer from './defer';
import delay from './delay';
import error from './error';
import filter from './filter';
import first from './first';
import flatMap from './flatMap';
import forEach from './forEach';
import fromEvent from './fromEvent';
Expand Down Expand Up @@ -37,6 +38,7 @@ const addAll = (target = Observable) => {
debounce,
delay,
filter,
first,
flatMap,
forEach,
map,
Expand Down Expand Up @@ -67,6 +69,7 @@ export {
delay,
error,
filter,
first,
flatMap,
forEach,
fromEvent,
Expand Down
11 changes: 11 additions & 0 deletions test/first.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect } from 'chai';
import first from '../src/first';
import apiCheck from './common/apiCheck';

describe('(First) defer', () => {
apiCheck(first);

it('emits the first element of an Observable', () => {
expect(first(Observable.of(1, 2, 3))).to.eql(1);
});
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can add test cases for what I mentioned above