Skip to content

Commit 450e1f2

Browse files
authored
Avoid prototype property iteration in trimUndefinedProperties (#602)
1 parent 64017d9 commit 450e1f2

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

doc/helpers.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This is a comprehensive guide of all helpers available for the Twitter API on bo
1717
* [Usage without instantiation](#usage-without-instantiation)
1818
* [Extract errors in an array](#extract-errors-in-an-array)
1919
* [Change image size of a profile picture](#change-image-size-of-a-profile-picture)
20+
* [Remove undefined properties from objects](#remove-undefined-properties-from-objects)
2021

2122
<!-- vscode-markdown-toc-config
2223
numbering=false
@@ -215,3 +216,17 @@ const user = await client.currentUser();
215216
// Original profile img link
216217
const originalProfileImage = TwitterApi.getProfileImageInSize(user.profile_image_url_https, 'original');
217218
```
219+
220+
## Remove undefined properties from objects
221+
222+
Use `trimUndefinedProperties` to remove `undefined` values from an object's own enumerable properties.
223+
This helper iterates over keys returned by `Object.keys`, leaving properties defined on the prototype chain untouched.
224+
225+
```ts
226+
const proto = { inherited: 'value' };
227+
const obj = Object.create(proto);
228+
obj.keep = 1;
229+
obj.toRemove = undefined;
230+
trimUndefinedProperties(obj);
231+
// obj is now { keep: 1 } and still inherits `inherited`
232+
```

src/helpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ export function arrayWrap<T>(value: T | T[]) : T[] {
2626

2727
export function trimUndefinedProperties(object: any) {
2828
// Delete undefined parameters
29-
for (const parameter in object) {
30-
if (object[parameter] === undefined)
29+
for (const parameter of Object.keys(object)) {
30+
if (object[parameter] === undefined) {
3131
delete object[parameter];
32+
}
3233
}
3334
}
3435

test/helpers.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { trimUndefinedProperties } from '../src/helpers';
4+
5+
describe('trimUndefinedProperties', () => {
6+
it('does not access or delete properties from the prototype chain', () => {
7+
let accessed = false;
8+
const proto = {} as any;
9+
Object.defineProperty(proto, 'inherited', {
10+
enumerable: true,
11+
get() {
12+
accessed = true;
13+
return undefined;
14+
},
15+
});
16+
17+
const obj = Object.create(proto);
18+
obj.keep = 1;
19+
obj.toRemove = undefined;
20+
21+
trimUndefinedProperties(obj);
22+
23+
expect(accessed).to.equal(false);
24+
expect(obj).to.not.have.own.property('toRemove');
25+
expect(obj.keep).to.equal(1);
26+
expect('inherited' in obj).to.equal(true);
27+
expect(Object.prototype.hasOwnProperty.call(obj, 'inherited')).to.equal(false);
28+
});
29+
});

0 commit comments

Comments
 (0)