Skip to content

Commit e1f3c50

Browse files
[fixed] rename to support inherited properties
Previously, `rename` would fail on `UIEvent`, because `event.hasOwnProperty('x') === false`. Switching to the `in` operator allows `rename` to work as expected without polluting the resulting object with undefined values. I'm not sure why I was checking if `value` had a given key in the first place. I probably did it without thinking too hard about it; perhaps to avoid trying to get a value from `undefined`. Since I don't recall why that check is there, I'm going to leave it until I have a good reason to remove it. In most cases, it shouldn't matter. What the correct behavior is for garbage value like `undefined` isn't something I have mental capacity to think about right now.
1 parent caedb27 commit e1f3c50

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

packages/core/src/operators/__tests__/rename.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@ describe('motionObservable.rename',
6767
}
6868
);
6969

70+
it('should support inherited properties',
71+
() => {
72+
subject.rename({ mapping: { width: 'x', height: 'y' } }).subscribe(listener);
73+
74+
const value = Object.create({ width: 3 });
75+
value.height = 10;
76+
subject.next(value);
77+
78+
expect(value.hasOwnProperty('width')).to.be.false;
79+
expect(listener).to.have.been.calledWith({ x: 3, y: 10 });
80+
}
81+
);
82+
83+
it('should not set values for unknown keys',
84+
() => {
85+
subject.rename({ width: 'x', height: 'y', depth: 'z' }).subscribe(listener);
86+
87+
subject.next({ width: 3, height: 10 });
88+
89+
expect(listener).to.have.been.calledWithExactly({ x: 3, y: 10 });
90+
}
91+
);
92+
7093
// This can be updated to passthrough unmapped names if we ever care.
7194
it('should ignore unmapped names',
7295
() => {

packages/core/src/operators/rename.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function withRename<T, S extends Constructor<MotionMappable<T>>>(supercla
7979

8080
Object.entries(mapping).forEach(
8181
([oldKey, newKey]) => {
82-
if (value.hasOwnProperty(oldKey)) {
82+
if (oldKey in value) {
8383
(result as any)[newKey] = value[oldKey as K];
8484
}
8585
}

0 commit comments

Comments
 (0)