Skip to content

Commit

Permalink
fix(pluck): param type
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasylonen committed Jun 19, 2019
1 parent b24d3ea commit 9697b69
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
6 changes: 5 additions & 1 deletion spec-dtslint/operators/pluck-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ it('should not accept empty parameter', () => {
const a = of({ name: 'abc' }).pipe(pluck()); // $ExpectError
});

it('should accept string only', () => {
it('should not accept a number when plucking an object', () => {
const a = of({ name: 'abc' }).pipe(pluck(1)); // $ExpectError
});

it('should support arrays', () => {
const a = of(['abc']).pipe(pluck(0)) // ExpectType Observable<string>
})
10 changes: 10 additions & 0 deletions spec/operators/pluck-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ describe('pluck operator', () => {
expectObservable(result).toBe(expected, {x: '1', y: '2', z: '3'});
});

it('should work for one array', () => {
const a = cold('--x--|', {x: ['abc']});
const asubs = '^ !';
const expected = '--y--|';

const r = a.pipe(pluck(0));
expectObservable(r).toBe(expected, {y: 'abc'});
expectSubscriptions(a.subscriptions).toBe(asubs);
});

it('should work for one object', () => {
const a = cold('--x--|', {x: {prop: 42}});
const asubs = '^ !';
Expand Down
12 changes: 6 additions & 6 deletions src/internal/operators/pluck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends
/* tslint:enable:max-line-length */

/**
* Maps each source value (an object) to its specified nested property.
* Maps each source value to its specified nested property.
*
* <span class="informal">Like {@link map}, but meant only for picking one of
* the nested properties of every emitted object.</span>
* the nested properties of every emitted value.</span>
*
* ![](pluck.png)
*
* Given a list of strings describing a path to an object property, retrieves
* Given a list of strings or numbers describing a path to a property, retrieves
* the value of a specified nested property from all values in the source
* Observable. If a property can't be resolved, it will return `undefined` for
* that value.
Expand All @@ -39,20 +39,20 @@ export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends
* @see {@link map}
*
* @param {...string} properties The nested properties to pluck from each source
* value (an object).
* value.
* @return {Observable} A new Observable of property values from the source values.
* @method pluck
* @owner Observable
*/
export function pluck<T, R>(...properties: string[]): OperatorFunction<T, R> {
export function pluck<T, R>(...properties: Array<string | number>): OperatorFunction<T, R> {
const length = properties.length;
if (length === 0) {
throw new Error('list of properties cannot be empty.');
}
return (source: Observable<T>) => map(plucker(properties, length))(source as any);
}

function plucker(props: string[], length: number): (x: string) => any {
function plucker(props: Array<string | number>, length: number): (x: string) => any {
const mapper = (x: string) => {
let currentProp = x;
for (let i = 0; i < length; i++) {
Expand Down

0 comments on commit 9697b69

Please sign in to comment.