Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df3c1e6

Browse files
committed
[Refactor] use Object.prototype.hasOwnProperty.call
1 parent fb66cb7 commit df3c1e6

File tree

5 files changed

+53
-58
lines changed

5 files changed

+53
-58
lines changed

lib/form_data.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ FormData.prototype._trackLength = function(header, value, options) {
103103
FormData.LINE_BREAK.length;
104104

105105
// empty or either doesn't have path or not an http response or not a stream
106-
if (!value || ( !value.path && !(value.readable && value.hasOwnProperty('httpVersion')) && !(value instanceof Stream))) {
106+
if (!value || ( !value.path && !(value.readable && Object.prototype.hasOwnProperty.call(value, 'httpVersion')) && !(value instanceof Stream))) {
107107
return;
108108
}
109109

@@ -114,8 +114,7 @@ FormData.prototype._trackLength = function(header, value, options) {
114114
};
115115

116116
FormData.prototype._lengthRetriever = function(value, callback) {
117-
118-
if (value.hasOwnProperty('fd')) {
117+
if (Object.prototype.hasOwnProperty.call(value, 'fd')) {
119118

120119
// take read range into a account
121120
// `end` = Infinity –> read file till the end
@@ -150,11 +149,11 @@ FormData.prototype._lengthRetriever = function(value, callback) {
150149
}
151150

152151
// or http response
153-
} else if (value.hasOwnProperty('httpVersion')) {
152+
} else if (Object.prototype.hasOwnProperty.call(value, 'httpVersion')) {
154153
callback(null, +value.headers['content-length']);
155154

156155
// or request stream http://github.com/mikeal/request
157-
} else if (value.hasOwnProperty('httpModule')) {
156+
} else if (Object.prototype.hasOwnProperty.call(value, 'httpModule')) {
158157
// wait till response come back
159158
value.on('response', function(response) {
160159
value.pause();
@@ -194,22 +193,23 @@ FormData.prototype._multiPartHeader = function(field, value, options) {
194193

195194
var header;
196195
for (var prop in headers) {
197-
if (!headers.hasOwnProperty(prop)) continue;
198-
header = headers[prop];
196+
if (Object.prototype.hasOwnProperty.call(headers, prop)) {
197+
header = headers[prop];
199198

200-
// skip nullish headers.
201-
if (header == null) {
202-
continue;
203-
}
199+
// skip nullish headers.
200+
if (header == null) {
201+
continue;
202+
}
204203

205-
// convert all headers to arrays.
206-
if (!Array.isArray(header)) {
207-
header = [header];
208-
}
204+
// convert all headers to arrays.
205+
if (!Array.isArray(header)) {
206+
header = [header];
207+
}
209208

210-
// add non-empty headers.
211-
if (header.length) {
212-
contents += prop + ': ' + header.join('; ') + FormData.LINE_BREAK;
209+
// add non-empty headers.
210+
if (header.length) {
211+
contents += prop + ': ' + header.join('; ') + FormData.LINE_BREAK;
212+
}
213213
}
214214
}
215215

@@ -230,7 +230,7 @@ FormData.prototype._getContentDisposition = function(value, options) {
230230
// formidable and the browser add a name property
231231
// fs- and request- streams have path property
232232
filename = path.basename(options.filename || value.name || value.path);
233-
} else if (value.readable && value.hasOwnProperty('httpVersion')) {
233+
} else if (value.readable && Object.prototype.hasOwnProperty.call(value, 'httpVersion')) {
234234
// or try http response
235235
filename = path.basename(value.client._httpMessage.path || '');
236236
}
@@ -258,7 +258,7 @@ FormData.prototype._getContentType = function(value, options) {
258258
}
259259

260260
// or if it's http-reponse
261-
if (!contentType && value.readable && value.hasOwnProperty('httpVersion')) {
261+
if (!contentType && value.readable && Object.prototype.hasOwnProperty.call(value, 'httpVersion')) {
262262
contentType = value.headers['content-type'];
263263
}
264264

@@ -299,7 +299,7 @@ FormData.prototype.getHeaders = function(userHeaders) {
299299
};
300300

301301
for (header in userHeaders) {
302-
if (userHeaders.hasOwnProperty(header)) {
302+
if (Object.prototype.hasOwnProperty.call(userHeaders, header)) {
303303
formHeaders[header.toLowerCase()] = userHeaders[header];
304304
}
305305
}

test/common.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ common.testFields = function (FIELDS, callback) {
4040
var incomingForm = new IncomingForm({uploadDir: common.dir.tmp});
4141

4242
incomingForm.parse(req);
43-
43+
4444
common.actions.checkForm(incomingForm, FIELDS, function (fieldsChecked) {
4545
// keep track of number of the processed fields
4646
callback(fieldsPassed - fieldsChecked);
@@ -55,18 +55,17 @@ common.testFields = function (FIELDS, callback) {
5555
common.actions = {};
5656

5757
// generic form field population
58-
common.actions.populateFields = function(form, fields)
59-
{
58+
common.actions.populateFields = function (form, fields) {
6059
var field;
6160
for (var name in fields) {
62-
if (!fields.hasOwnProperty(name)) { continue; }
63-
64-
field = fields[name];
65-
// important to append ReadStreams within the same tick
66-
if ((typeof field.value == 'function')) {
67-
field.value = field.value();
61+
if (Object.prototype.hasOwnProperty.call(fields, name)) {
62+
field = fields[name];
63+
// important to append ReadStreams within the same tick
64+
if ((typeof field.value == 'function')) {
65+
field.value = field.value();
66+
}
67+
form.append(name, field.value);
6868
}
69-
form.append(name, field.value);
7069
}
7170
};
7271

test/integration/test-custom-content-type.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,15 @@ server.listen(common.port, function() {
6767

6868
var form = new FormData();
6969

70-
var field;
7170
for (var name in FIELDS) {
72-
if (!FIELDS.hasOwnProperty(name)) { continue; }
73-
74-
field = FIELDS[name];
75-
// important to append ReadStreams within the same tick
76-
if ((typeof field.value == 'function')) {
77-
field.value = field.value();
71+
if (Object.prototype.hasOwnProperty.call(FIELDS, name)) {
72+
var field = FIELDS[name];
73+
// important to append ReadStreams within the same tick
74+
if ((typeof field.value == 'function')) {
75+
field.value = field.value();
76+
}
77+
form.append(name, field.value, field.options);
7878
}
79-
form.append(name, field.value, field.options);
8079
}
8180

8281
// custom params object passed to submit

test/integration/test-pipe.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,15 @@ server.listen(common.port, function() {
4949

5050
var form = new FormData();
5151

52-
var field;
5352
for (var name in FIELDS) {
54-
if (!FIELDS.hasOwnProperty(name)) { continue; }
55-
56-
field = FIELDS[name];
57-
// important to append ReadStreams within the same tick
58-
if ((typeof field.value == 'function')) {
59-
field.value = field.value();
53+
if (Object.prototype.hasOwnProperty.call(FIELDS, name)) {
54+
var field = FIELDS[name];
55+
// important to append ReadStreams within the same tick
56+
if ((typeof field.value == 'function')) {
57+
field.value = field.value();
58+
}
59+
form.append(name, field.value);
6060
}
61-
form.append(name, field.value);
6261
}
6362

6463
var req = http.request({

test/integration/test-ranged-filestream.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,18 @@ server.listen(common.port, function() {
8080

8181
// add test subjects to the form
8282
for (name in testSubjects) {
83-
if (!testSubjects.hasOwnProperty(name)) {
84-
continue;
85-
}
86-
87-
options = {encoding: 'utf8'};
83+
if (Object.prototype.hasOwnProperty.call(testSubjects, name)) {
84+
options = {encoding: 'utf8'};
8885

89-
if (testSubjects[name].start) { options.start = testSubjects[name].start; }
90-
if (testSubjects[name].end) { options.end = testSubjects[name].end; }
86+
if (testSubjects[name].start) { options.start = testSubjects[name].start; }
87+
if (testSubjects[name].end) { options.end = testSubjects[name].end; }
9188

92-
form.append(name, testSubjects[name].fsStream = fs.createReadStream(common.dir.fixture + '/' + testSubjects[name].file, options));
89+
form.append(name, testSubjects[name].fsStream = fs.createReadStream(common.dir.fixture + '/' + testSubjects[name].file, options));
9390

94-
// calculate data size
95-
testSubjects[name].readSize = 0;
96-
testSubjects[name].fsStream.on('data', readSizeAccumulator.bind(testSubjects[name]));
91+
// calculate data size
92+
testSubjects[name].readSize = 0;
93+
testSubjects[name].fsStream.on('data', readSizeAccumulator.bind(testSubjects[name]));
94+
}
9795
}
9896

9997
form.submit('http://localhost:' + common.port + '/', function(err, res) {

0 commit comments

Comments
 (0)