Skip to content

Commit 87b3f04

Browse files
refactor: convert more loops to for-of
1 parent 5a899a7 commit 87b3f04

File tree

5 files changed

+46
-51
lines changed

5 files changed

+46
-51
lines changed

lib/model.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4515,9 +4515,8 @@ function _assign(model, vals, mod, assignmentOpts) {
45154515
_val = utils.getValue(foreignField, val);
45164516
if (Array.isArray(_val)) {
45174517
_val = utils.array.flatten(_val);
4518-
const _valLength = _val.length;
4519-
for (let j = 0; j < _valLength; ++j) {
4520-
let __val = _val[j];
4518+
4519+
for (let __val of _val) {
45214520
if (__val instanceof Document) {
45224521
__val = __val._id;
45234522
}

lib/query.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ function Query(conditions, options, model, collection) {
7070
// this is the case where we have a CustomQuery, we need to check if we got
7171
// options passed in, and if we did, merge them in
7272
const keys = Object.keys(options);
73-
for (let i = 0; i < keys.length; ++i) {
74-
const k = keys[i];
75-
this._mongooseOptions[k] = options[k];
73+
for (const key of keys) {
74+
this._mongooseOptions[key] = options[key];
7675
}
7776

7877
if (collection) {
@@ -4604,14 +4603,14 @@ Query.prototype.populate = function() {
46044603
const readConcern = this.options.readConcern;
46054604
const readPref = this.options.readPreference;
46064605

4607-
for (let i = 0; i < res.length; ++i) {
4608-
if (readConcern != null && get(res[i], 'options.readConcern') == null) {
4609-
res[i].options = res[i].options || {};
4610-
res[i].options.readConcern = readConcern;
4606+
for (const populateOptions of res) {
4607+
if (readConcern != null && get(populateOptions, 'options.readConcern') == null) {
4608+
populateOptions.options = populateOptions.options || {};
4609+
populateOptions.options.readConcern = readConcern;
46114610
}
4612-
if (readPref != null && get(res[i], 'options.readPreference') == null) {
4613-
res[i].options = res[i].options || {};
4614-
res[i].options.readPreference = readPref;
4611+
if (readPref != null && get(populateOptions, 'options.readPreference') == null) {
4612+
populateOptions.options = populateOptions.options || {};
4613+
populateOptions.options.readPreference = readPref;
46154614
}
46164615
}
46174616
}
@@ -4620,10 +4619,10 @@ Query.prototype.populate = function() {
46204619

46214620
if (opts.lean != null) {
46224621
const lean = opts.lean;
4623-
for (let i = 0; i < res.length; ++i) {
4624-
if (get(res[i], 'options.lean') == null) {
4625-
res[i].options = res[i].options || {};
4626-
res[i].options.lean = lean;
4622+
for (const populateOptions of res) {
4623+
if (get(populateOptions, 'options.lean') == null) {
4624+
populateOptions.options = populateOptions.options || {};
4625+
populateOptions.options.lean = lean;
46274626
}
46284627
}
46294628
}
@@ -4634,12 +4633,13 @@ Query.prototype.populate = function() {
46344633

46354634
const pop = opts.populate;
46364635

4637-
for (let i = 0; i < res.length; ++i) {
4638-
const path = res[i].path;
4639-
if (pop[path] && pop[path].populate && res[i].populate) {
4640-
res[i].populate = pop[path].populate.concat(res[i].populate);
4636+
for (const populateOptions of res) {
4637+
const path = populateOptions.path;
4638+
if (pop[path] && pop[path].populate && populateOptions.populate) {
4639+
populateOptions.populate = pop[path].populate.concat(populateOptions.populate);
46414640
}
4642-
pop[res[i].path] = res[i];
4641+
4642+
pop[populateOptions.path] = populateOptions;
46434643
}
46444644

46454645
return this;
@@ -5326,12 +5326,7 @@ Query.prototype.selectedExclusively = function selectedExclusively() {
53265326
}
53275327

53285328
const keys = Object.keys(this._fields);
5329-
if (keys.length === 0) {
5330-
return false;
5331-
}
5332-
5333-
for (let i = 0; i < keys.length; ++i) {
5334-
const key = keys[i];
5329+
for (const key of keys) {
53355330
if (key === '_id') {
53365331
continue;
53375332
}

lib/queryhelpers.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ exports.applyPaths = function applyPaths(fields, schema) {
174174
// check for parent exclusions
175175
const pieces = path.split('.');
176176
let cur = '';
177-
for (let i = 0; i < pieces.length; ++i) {
178-
cur += cur.length ? '.' + pieces[i] : pieces[i];
177+
for (const piece of pieces) {
178+
cur += cur.length ? '.' + piece : piece;
179179
if (excluded.indexOf(cur) !== -1) {
180180
return;
181181
}
@@ -186,8 +186,8 @@ exports.applyPaths = function applyPaths(fields, schema) {
186186
// project out everything else under the parent path
187187
if (!exclude && get(type, 'options.$skipDiscriminatorCheck', false)) {
188188
let cur = '';
189-
for (let i = 0; i < pieces.length; ++i) {
190-
cur += (cur.length === 0 ? '' : '.') + pieces[i];
189+
for (const piece of pieces) {
190+
cur += (cur.length === 0 ? '' : '.') + piece;
191191
const projection = get(fields, cur, false);
192192
if (projection && typeof projection !== 'object') {
193193
return;
@@ -203,8 +203,8 @@ exports.applyPaths = function applyPaths(fields, schema) {
203203

204204
switch (exclude) {
205205
case true:
206-
for (let i = 0; i < excluded.length; ++i) {
207-
fields[excluded[i]] = 0;
206+
for (const fieldName of excluded) {
207+
fields[fieldName] = 0;
208208
}
209209
break;
210210
case false:
@@ -214,8 +214,8 @@ exports.applyPaths = function applyPaths(fields, schema) {
214214
schema.paths['_id'].options.select === false) {
215215
fields._id = 0;
216216
}
217-
for (let i = 0; i < selected.length; ++i) {
218-
fields[selected[i]] = 1;
217+
for (const fieldName of selected) {
218+
fields[fieldName] = 1;
219219
}
220220
break;
221221
case undefined:
@@ -231,8 +231,8 @@ exports.applyPaths = function applyPaths(fields, schema) {
231231

232232
// user didn't specify fields, implies returning all fields.
233233
// only need to apply excluded fields and delete any plus paths
234-
for (let i = 0; i < excluded.length; ++i) {
235-
fields[excluded[i]] = 0;
234+
for (const fieldName of excluded) {
235+
fields[fieldName] = 0;
236236
}
237237
break;
238238
}

lib/schema.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,7 @@ Schema.prototype.add = function add(obj, prefix) {
460460
prefix = prefix || '';
461461
const keys = Object.keys(obj);
462462

463-
for (let i = 0; i < keys.length; ++i) {
464-
const key = keys[i];
463+
for (const key of keys) {
465464
const fullPath = prefix + key;
466465

467466
if (obj[key] == null) {
@@ -1486,10 +1485,9 @@ Schema.prototype.plugin = function(fn, opts) {
14861485
'got "' + (typeof fn) + '"');
14871486
}
14881487

1489-
if (opts &&
1490-
opts.deduplicate) {
1491-
for (let i = 0; i < this.plugins.length; ++i) {
1492-
if (this.plugins[i].fn === fn) {
1488+
if (opts && opts.deduplicate) {
1489+
for (const plugin of this.plugins) {
1490+
if (plugin.fn === fn) {
14931491
return this;
14941492
}
14951493
}
@@ -1908,10 +1906,13 @@ Schema.prototype.remove = function(path) {
19081906
function _deletePath(schema, name) {
19091907
const pieces = name.split('.');
19101908
const last = pieces.pop();
1909+
19111910
let branch = schema.tree;
1912-
for (let i = 0; i < pieces.length; ++i) {
1913-
branch = branch[pieces[i]];
1911+
1912+
for (const piece of pieces) {
1913+
branch = branch[piece];
19141914
}
1915+
19151916
delete branch[last];
19161917
}
19171918

lib/schematype.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -972,17 +972,17 @@ SchemaType.prototype.getDefault = function(scope, init) {
972972
SchemaType.prototype._applySetters = function(value, scope, init, priorVal) {
973973
let v = value;
974974
const setters = this.setters;
975-
let len = setters.length;
976975
const caster = this.caster;
977976

978-
while (len--) {
979-
v = setters[len].call(scope, v, this);
977+
for (const setter of setters) {
978+
v = setter.call(scope, v, this);
980979
}
981980

982981
if (Array.isArray(v) && caster && caster.setters) {
983982
const newVal = [];
984-
for (let i = 0; i < v.length; i++) {
985-
newVal.push(caster.applySetters(v[i], scope, init, priorVal));
983+
984+
for (const value of v) {
985+
newVal.push(caster.applySetters(value, scope, init, priorVal));
986986
}
987987
v = newVal;
988988
}

0 commit comments

Comments
 (0)