Skip to content

Commit 2521775

Browse files
committed
- Refactoring: Use more for-of
- Refactoring: Avoid further `let`, bringing declaration closer to scope
1 parent 729530f commit 2521775

File tree

2 files changed

+64
-73
lines changed

2 files changed

+64
-73
lines changed

src/jquery.form.js

+54-59
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@
147147
}
148148

149149
/* eslint consistent-this: ["error", "$form"] */
150-
let url;
151150
const $form = this;
152151

153152
if (typeof options === 'function') {
@@ -171,7 +170,8 @@
171170
const method = options.method || options.type || this.attr2('method');
172171
const action = options.url || this.attr2('action');
173172

174-
url = typeof action === 'string' ? $.trim(action) : '';
173+
let url = typeof action === 'string' ? $.trim(action) : '';
174+
175175
url = url || window.location.href || '';
176176
if (url) {
177177
// clean url (don't include hash vaue)
@@ -208,16 +208,15 @@
208208
return this;
209209
}
210210

211-
let traditional = options.traditional;
212-
213-
if (typeof traditional === 'undefined') {
214-
traditional = $.ajaxSettings.traditional;
215-
}
211+
const {
212+
traditional = $.ajaxSettings.traditional
213+
} = options;
216214

217215
const elements = [];
218-
let qx;
219216
const arr = this.formToArray(options.semantic, elements, options.filtering);
220217

218+
let qx;
219+
221220
if (options.data) {
222221
const optionsData = $.isFunction(options.data) ? options.data(arr) : options.data;
223222

@@ -291,8 +290,8 @@
291290
options.success = function(dta, status, xhr) { // jQuery 1.4+ passes xhr as 3rd arg
292291
const context = options.context || this; // jQuery 1.4+ supports scope context
293292

294-
for (let i = 0, max = callbacks.length; i < max; i++) {
295-
callbacks[i].apply(context, [dta, status, xhr || $form, $form]);
293+
for (const callback of callbacks) {
294+
callback.apply(context, [dta, status, xhr || $form, $form]);
296295
}
297296
};
298297

@@ -368,14 +367,12 @@
368367
// utility fn for deep serialization
369368
function deepSerialize(extraData) {
370369
const serialized = $.param(extraData, options.traditional).split('&');
371-
const len = serialized.length;
372370
const result = [];
373-
let i, part;
374371

375-
for (i = 0; i < len; i++) {
372+
for (const ser of serialized) {
376373
// #252; undo param space replacement
377-
serialized[i] = serialized[i].replace(/\+/g, ' ');
378-
part = serialized[i].split('=');
374+
const part = ser.replace(/\+/g, ' ').split('=');
375+
379376
// #278; use array instead of object storage, favoring array serializations
380377
result.push([decodeURIComponent(part[0]), decodeURIComponent(part[1])]);
381378
}
@@ -387,16 +384,16 @@
387384
function fileUploadXhr(array) {
388385
const formdata = new FormData();
389386

390-
for (let i = 0; i < array.length; i++) {
391-
formdata.append(array[i].name, array[i].value);
387+
for (const {name, value} of array) {
388+
formdata.append(name, value);
392389
}
393390

394391
if (options.extraData) {
395392
const serializedData = deepSerialize(options.extraData);
396393

397-
for (let i = 0; i < serializedData.length; i++) {
398-
if (serializedData[i]) {
399-
formdata.append(serializedData[i][0], serializedData[i][1]);
394+
for (const serData of serializedData) {
395+
if (serData) {
396+
formdata.append(serData[0], serData[1]);
400397
}
401398
}
402399
}
@@ -458,12 +455,12 @@
458455
// private function for handling file uploads (hat tip to YAHOO!)
459456
function fileUploadIframe(array) {
460457
const form = $form[0];
461-
let $io, el, id, timedOut, timeoutHandle;
462458

463459
if (array) {
464460
// ensure that every serialized input is still enabled
465-
for (let i = 0; i < elements.length; i++) {
466-
el = $(elements[i]);
461+
for (const element of elements) {
462+
const el = $(element);
463+
467464
if (hasProp) {
468465
el.prop('disabled', false);
469466
} else {
@@ -475,15 +472,16 @@
475472
const settings = $.extend(true, {}, $.ajaxSettings, options);
476473

477474
settings.context = settings.context || settings;
478-
id = 'jqFormIO' + new Date().getTime();
475+
let id = 'jqFormIO' + new Date().getTime();
479476
const ownerDocument = form.ownerDocument;
480477
const $body = $form.closest('body');
481478

482-
let name;
479+
let $io;
483480

484481
if (settings.iframeTarget) {
485482
$io = $(settings.iframeTarget, ownerDocument);
486-
name = $io.attr2('name');
483+
const name = $io.attr2('name');
484+
487485
if (name) {
488486
id = name;
489487
} else {
@@ -569,7 +567,8 @@
569567
const sub = form.clk;
570568

571569
if (sub) {
572-
name = sub.name;
570+
const {name} = sub;
571+
573572
if (name && !sub.disabled) {
574573
settings.extraData = settings.extraData || {};
575574
settings.extraData[name] = sub.value;
@@ -627,6 +626,8 @@
627626
settings.extraData[csrfParam] = csrfToken;
628627
}
629628

629+
let timedOut, timeoutHandle;
630+
630631
// take a breath so that pending repaints get some cpu time before the upload starts
631632
function doSubmit() {
632633
// make sure form attrs are set
@@ -746,11 +747,6 @@
746747
setTimeout(doSubmit, timeout); // this lets dom updates render
747748
}
748749

749-
let callbackProcessed, doc, domCheckCount = 50;
750-
751-
// eslint-disable-next-line prefer-const
752-
let httpData;
753-
754750
const toXml = $.parseXML || function(str, docum) { // use parseXML if available (jQuery 1.5+)
755751
if (window.ActiveXObject) {
756752
docum = new ActiveXObject('Microsoft.XMLDOM');
@@ -764,12 +760,17 @@
764760
return docum && docum.documentElement && docum.documentElement.nodeName !== 'parsererror' ? docum : null;
765761
};
766762

763+
// eslint-disable-next-line prefer-const
764+
let httpData;
765+
let callbackProcessed, domCheckCount = 50;
766+
767767
function cb(e) {
768768
if (xhr.aborted || callbackProcessed) {
769769
return;
770770
}
771771

772-
doc = getDoc(io);
772+
const doc = getDoc(io);
773+
773774
if (!doc) {
774775
log('cannot access response document');
775776
e = SERVER_ABORT;
@@ -801,9 +802,7 @@
801802
io.removeEventListener('load', cb, false);
802803
}
803804

804-
let errMsg, status = 'success';
805-
806-
let dta;
805+
let dta, errMsg, status = 'success';
807806

808807
try {
809808
if (timedOut) {
@@ -1072,7 +1071,7 @@
10721071
}
10731072

10741073
function captureSubmittingElement(e) {
1075-
let target = e.target;
1074+
let {target} = e;
10761075
const $el = $(target);
10771076

10781077
if (!$el.is('[type=submit],[type=image]')) {
@@ -1143,7 +1142,6 @@
11431142
const form = this[0];
11441143
const formId = this.attr('id');
11451144
let els = semantic || typeof form.elements === 'undefined' ? form.getElementsByTagName('*') : form.elements;
1146-
let els2;
11471145

11481146
if (els) {
11491147
els = $.makeArray(els); // convert to standard array
@@ -1152,7 +1150,8 @@
11521150
// #386; account for inputs outside the form which use the 'form' attribute
11531151
// FinesseRus: in non-IE browsers outside fields are already included in form.elements.
11541152
if (formId && (semantic || /(Edge|Trident)\//.test(navigator.userAgent))) {
1155-
els2 = $(':input[form="' + formId + '"]').get(); // hat tip @thet
1153+
const els2 = $(':input[form="' + formId + '"]').get(); // hat tip @thet
1154+
11561155
if (els2.length) {
11571156
els = (els || []).concat(els2);
11581157
}
@@ -1166,11 +1165,9 @@
11661165
els = $.map(els, filtering);
11671166
}
11681167

1169-
let el, i, j, jmax, max, name;
1168+
for (const el of els) {
1169+
const {name} = el;
11701170

1171-
for (i = 0, max = els.length; i < max; i++) {
1172-
el = els[i];
1173-
name = el.name;
11741171
if (!name || el.disabled) {
11751172
// eslint-disable-next-line no-continue
11761173
continue;
@@ -1188,12 +1185,12 @@
11881185

11891186
const val = $.fieldValue(el, true);
11901187

1191-
if (val && val.constructor === Array) {
1188+
if (val && Array.isArray(val)) {
11921189
if (elements) {
11931190
elements.push(el);
11941191
}
1195-
for (j = 0, jmax = val.length; j < jmax; j++) {
1196-
arr.push({name: name, value: val[j]});
1192+
for (const value of val) {
1193+
arr.push({name: name, value: value});
11971194
}
11981195

11991196
} else if (feature.fileapi && el.type === 'file') {
@@ -1204,8 +1201,8 @@
12041201
const files = el.files;
12051202

12061203
if (files.length) {
1207-
for (j = 0; j < files.length; j++) {
1208-
arr.push({name: name, type: el.type, value: files[j]});
1204+
for (const file of files) {
1205+
arr.push({name: name, type: el.type, value: file});
12091206
}
12101207
} else {
12111208
// #180
@@ -1224,7 +1221,7 @@
12241221
// input type=='image' are not found in elements array! handle it here
12251222
const $input = $(form.clk), input = $input[0];
12261223

1227-
name = input.name;
1224+
const {name} = input;
12281225

12291226
if (name && !input.disabled && input.type === 'image') {
12301227
arr.push({name: name, value: $input.val()});
@@ -1261,9 +1258,9 @@
12611258

12621259
const val = $.fieldValue(this, successful);
12631260

1264-
if (val && val.constructor === Array) {
1265-
for (let i = 0, max = val.length; i < max; i++) {
1266-
arr.push({name: name, value: val[i]});
1261+
if (val && Array.isArray(val)) {
1262+
for (const value of val) {
1263+
arr.push({name: name, value: value});
12671264
}
12681265

12691266
} else if (val !== null && typeof val !== 'undefined') {
@@ -1320,12 +1317,12 @@
13201317
const el = this[i];
13211318
const value = $.fieldValue(el, successful);
13221319

1323-
if (value === null || typeof value === 'undefined' || (value.constructor === Array && !value.length)) {
1320+
if (value === null || typeof value === 'undefined' || (Array.isArray(value) && !value.length)) {
13241321
// eslint-disable-next-line no-continue
13251322
continue;
13261323
}
13271324

1328-
if (value.constructor === Array) {
1325+
if (Array.isArray(value)) {
13291326
$.merge(val, value);
13301327
} else {
13311328
val.push(value);
@@ -1370,11 +1367,9 @@
13701367
const op = ops[i];
13711368

13721369
if (op.selected && !op.disabled) {
1373-
let val = op.value;
1374-
1375-
if (!val) { // extra pain for IE...
1376-
val = op.attributes && op.attributes.value && !op.attributes.value.specified ? op.text : op.value;
1377-
}
1370+
const val = op.value ||
1371+
// extra pain for IE...
1372+
(op.attributes && op.attributes.value && !op.attributes.value.specified ? op.text : op.value);
13781373

13791374
if (one) {
13801375
return val;

test/test.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
const arrayCount = function(arr, key) {
77
let count = 0;
88

9-
for (let i = 0; i < arr.length; i++) {
10-
if (arr[i].name === key) {
9+
for (const {name} of arr) {
10+
if (name === key) {
1111
count++;
1212
}
1313
}
@@ -17,9 +17,9 @@ const arrayCount = function(arr, key) {
1717

1818
// helper method
1919
const arrayValue = function(arr, key) {
20-
for (let i = 0; i < arr.length; i++) {
21-
if (arr[i].name === key) {
22-
return arr[i].value;
20+
for (const {name, value} of arr) {
21+
if (name === key) {
22+
return value;
2323
}
2424
}
2525

@@ -664,8 +664,6 @@ describe('form', function() {
664664

665665

666666
it('fieldValue(true)', function() {
667-
let i;
668-
669667
assert.ok($('#fieldTest input').fieldValue(true)[0] === '5', 'input');
670668
assert.ok($('#fieldTest :input').fieldValue(true)[0] === '1', ':input');
671669
assert.ok($('#fieldTest input:hidden').fieldValue(true)[0] === '5', ':hidden');
@@ -677,22 +675,20 @@ describe('form', function() {
677675
let result = $('#fieldTest :checkbox').fieldValue(true);
678676

679677
assert.ok(result.length === expected.length, 'result size check (checkbox): ' + result.length + '=' + expected.length);
680-
for (i = 0; i < result.length; i++) {
678+
for (let i = 0; i < result.length; i++) {
681679
assert.ok(result[i] === expected[i], expected[i]);
682680
}
683681

684682
expected = ['3', '4'];
685683
result = $('#fieldTest [name=B]').fieldValue(true);
686684
assert.ok(result.length === expected.length, 'result size check (select-multiple): ' + result.length + '=' + expected.length);
687-
for (i = 0; i < result.length; i++) {
685+
for (let i = 0; i < result.length; i++) {
688686
assert.ok(result[i] === expected[i], expected[i]);
689687
}
690688
});
691689

692690
// eslint-disable-next-line max-statements
693691
it('fieldValue(false)', function() {
694-
let i;
695-
696692
assert.ok($('#fieldTest input').fieldValue(false)[0] === '5', 'input');
697693
assert.ok($('#fieldTest :input').fieldValue(false)[0] === '1', ':input');
698694
assert.ok($('#fieldTest input:hidden').fieldValue(false)[0] === '5', ':hidden');
@@ -703,21 +699,21 @@ describe('form', function() {
703699
let result = $('#fieldTest :checkbox').fieldValue(false);
704700

705701
assert.ok(result.length === expected.length, 'result size check (checkbox): ' + result.length + '=' + expected.length);
706-
for (i = 0; i < result.length; i++) {
702+
for (let i = 0; i < result.length; i++) {
707703
assert.ok(result[i] === expected[i], expected[i]);
708704
}
709705

710706
expected = ['11', '12', '13'];
711707
result = $('#fieldTest :radio').fieldValue(false);
712708
assert.ok(result.length === expected.length, 'result size check (radio): ' + result.length + '=' + expected.length);
713-
for (i = 0; i < result.length; i++) {
709+
for (let i = 0; i < result.length; i++) {
714710
assert.ok(result[i] === expected[i], expected[i]);
715711
}
716712

717713
expected = ['3', '4'];
718714
result = $('#fieldTest [name=B]').fieldValue(false);
719715
assert.ok(result.length === expected.length, 'result size check (select-multiple): ' + result.length + '=' + expected.length);
720-
for (i = 0; i < result.length; i++) {
716+
for (let i = 0; i < result.length; i++) {
721717
assert.ok(result[i] === expected[i], expected[i]);
722718
}
723719
});

0 commit comments

Comments
 (0)