@@ -171,19 +171,21 @@ userSchema.statics.findByEmail = function findByEmail(email, cb) {
171
171
* Queries User collection by username and returns one User document.
172
172
*
173
173
* @param {string } username - Username string
174
- * @param {boolean } [caseInsensitive] - Does a caseInsensitive query, defaults to false
174
+ * @param {Object } [options] - Optional options
175
+ * @param {boolean } options.caseInsensitive - Does a caseInsensitive query, defaults to false
175
176
* @callback [cb] - Optional error-first callback that passes User document
176
177
* @return {Promise<Object> } - Returns Promise fulfilled by User document
177
178
*/
178
- userSchema . statics . findByUsername = function findByUsername ( username , caseInsensitive , cb ) {
179
+ userSchema . statics . findByUsername = function findByUsername ( username , options , cb ) {
179
180
const query = {
180
181
username
181
182
} ;
182
- if ( arguments . length === 3
183
- || ( arguments . length === 2 && typeof caseInsensitive === 'boolean ' && caseInsensitive ) ) {
183
+ if ( ( arguments . length === 3 && options . caseInsensitive )
184
+ || ( arguments . length === 2 && typeof options === 'object ' && options . caseInsensitive ) ) {
184
185
return this . findOne ( query ) . collation ( { locale : 'en' , strength : 2 } ) . exec ( cb ) ;
185
186
}
186
- return this . findOne ( query , cb || caseInsensitive ) ;
187
+ const callback = typeof options === 'function' ? options : cb ;
188
+ return this . findOne ( query , callback ) ;
187
189
} ;
188
190
189
191
/**
@@ -193,23 +195,26 @@ userSchema.statics.findByUsername = function findByUsername(username, caseInsens
193
195
* a username or email.
194
196
*
195
197
* @param {string } value - Email or username
196
- * @param {boolean } caseInsensitive - Does a caseInsensitive query rather than
197
- * default query for username or email, defaults
198
- * to false
198
+ * @param {Object } [options] - Optional options
199
+ * @param {boolean } options.caseInsensitive - Does a caseInsensitive query rather than
200
+ * default query for username or email, defaults
201
+ * to false
199
202
* @callback [cb] - Optional error-first callback that passes User document
200
203
* @return {Promise<Object> } - Returns Promise fulfilled by User document
201
204
*/
202
- userSchema . statics . findByEmailOrUsername = function findByEmailOrUsername ( value , caseInsensitive , cb ) {
205
+ userSchema . statics . findByEmailOrUsername = function findByEmailOrUsername ( value , options , cb ) {
206
+ // do the case insensitive stuff
203
207
const isEmail = value . indexOf ( '@' ) > - 1 ;
204
- if ( arguments . length === 3
205
- || ( arguments . length === 2 && typeof caseInsensitive === 'boolean ' && caseInsensitive ) ) {
208
+ if ( ( arguments . length === 3 && options . caseInsensitive )
209
+ || ( arguments . length === 2 && typeof options === 'object ' && options . caseInsensitive ) ) {
206
210
const query = isEmail ? { email : value } : { username : value } ;
207
211
return this . findOne ( query ) . collation ( { locale : 'en' , strength : 2 } ) . exec ( cb ) ;
208
212
}
213
+ const callback = typeof options === 'function' ? options : cb ;
209
214
if ( isEmail ) {
210
- return this . findByEmail ( value , cb || caseInsensitive ) ;
215
+ return this . findByEmail ( value , callback ) ;
211
216
}
212
- return this . findByUsername ( value , cb || caseInsensitive ) ;
217
+ return this . findByUsername ( value , callback ) ;
213
218
} ;
214
219
215
220
/**
0 commit comments