-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ofType): expose ofType as lettable operator (#343)
Supports using ofType directly in lettable operator pipelines. Closes #186
- Loading branch information
Showing
8 changed files
with
114 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { filter } from 'rxjs/operator/filter'; | ||
|
||
export function ofType(...keys) { | ||
return function ofTypeOperatorFunction(source) { | ||
return source::filter(({ type }) => { | ||
const len = keys.length; | ||
if (len === 1) { | ||
return type === keys[0]; | ||
} else { | ||
for (let i = 0; i < len; i++) { | ||
if (keys[i] === type) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* globals describe it */ | ||
import { expect } from 'chai'; | ||
import { Subject } from 'rxjs/Subject'; | ||
import { ofType } from '../'; | ||
|
||
describe('operators', () => { | ||
describe('ofType', () => { | ||
it('should filter by action type', () => { | ||
let actions = new Subject(); | ||
let lulz = []; | ||
let haha = []; | ||
|
||
actions.pipe(ofType('LULZ')).subscribe(x => lulz.push(x)); | ||
actions.pipe(ofType('HAHA')).subscribe(x => haha.push(x)); | ||
|
||
actions.next({ type: 'LULZ', i: 0 }); | ||
|
||
expect(lulz).to.deep.equal([{ type: 'LULZ', i: 0 }]); | ||
expect(haha).to.deep.equal([]); | ||
|
||
actions.next({ type: 'LULZ', i: 1 }); | ||
|
||
expect(lulz).to.deep.equal([{ type: 'LULZ', i: 0 }, { type: 'LULZ', i: 1 }]); | ||
expect(haha).to.deep.equal([]); | ||
|
||
actions.next({ type: 'HAHA', i: 0 }); | ||
|
||
expect(lulz).to.deep.equal([{ type: 'LULZ', i: 0 }, { type: 'LULZ', i: 1 }]); | ||
expect(haha).to.deep.equal([{ type: 'HAHA', i: 0 }]); | ||
}); | ||
|
||
it('should filter by multiple action types', () => { | ||
let actions = new Subject(); | ||
let lulz = []; | ||
let haha = []; | ||
|
||
actions.pipe(ofType('LULZ', 'LARF')).subscribe(x => lulz.push(x)); | ||
actions.pipe(ofType('HAHA')).subscribe(x => haha.push(x)); | ||
|
||
actions.next({ type: 'LULZ', i: 0 }); | ||
|
||
expect(lulz).to.deep.equal([{ type: 'LULZ', i: 0 }]); | ||
expect(haha).to.deep.equal([]); | ||
|
||
actions.next({ type: 'LARF', i: 1 }); | ||
|
||
expect(lulz).to.deep.equal([{ type: 'LULZ', i: 0 }, { type: 'LARF', i: 1 }]); | ||
expect(haha).to.deep.equal([]); | ||
|
||
actions.next({ type: 'HAHA', i: 0 }); | ||
|
||
expect(lulz).to.deep.equal([{ type: 'LULZ', i: 0 }, { type: 'LARF', i: 1 }]); | ||
expect(haha).to.deep.equal([{ type: 'HAHA', i: 0 }]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters