Skip to content

Commit

Permalink
fix(common): Fix implementation of 'pick' -- use hasOwnProperty
Browse files Browse the repository at this point in the history
Closes #53
  • Loading branch information
christopherthielen committed May 6, 2017
1 parent 0b1e9ed commit 09848a4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,9 @@ export function ancestors(first: StateObject, second: StateObject) {

/**
* Return a copy of the object only containing the whitelisted properties.
* @example
* ```
*
* #### Example:
* ```
* var foo = { a: 1, b: 2, c: 3 };
* var ab = pick(foo, ['a', 'b']); // { a: 1, b: 2 }
* ```
Expand All @@ -246,8 +245,7 @@ export function ancestors(first: StateObject, second: StateObject) {
*/
export function pick(obj: Obj, propNames: string[]): Obj {
let copy = {};
// propNames.forEach(prop => { if (obj.hasOwnProperty(prop)) copy[prop] = obj[prop] });
propNames.forEach(prop => { if (isDefined(obj[prop])) copy[prop] = obj[prop] });
propNames.forEach(prop => { if (obj.hasOwnProperty(prop)) copy[prop] = obj[prop] });
return copy;
}

Expand Down
15 changes: 15 additions & 0 deletions test/transitionSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,21 @@ describe('transition', function () {
done();
});

it('should not inherit previous params when new params are passed', async(done) => {
router.stateRegistry.register({
name: 'foo',
url: '?fooParam',
});

await $state.go('foo', { fooParam: 'abc' });
expect(router.globals.params).toEqualValues({ fooParam: 'abc' });

await $state.go('foo', { fooParam: undefined });
expect(router.globals.params).toEqualValues({ fooParam: undefined });

done();
});

it('should not inherit params whose type has inherit: false', async(done) => {
router.urlService.config.type('inherit', {
inherit: true, encode: x => x, decode: x => x, is: () => true, equals: equals, pattern: /.*/, raw: false,
Expand Down

0 comments on commit 09848a4

Please sign in to comment.