Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Replace a few more uses of lodash #1916

Merged
merged 4 commits into from Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/common.js
Expand Up @@ -436,8 +436,8 @@ function matchStringOrRegexp(target, pattern) {
function formatQueryValue(key, value, stringFormattingFn) {
// TODO: Probably refactor code to replace `switch(true)` with `if`/`else`.
switch (true) {
case _.isNumber(value): // fall-through
case _.isBoolean(value):
case typeof value === 'number': // fall-through
case typeof value === 'boolean':
value = value.toString()
break
case value === null:
Expand Down
12 changes: 9 additions & 3 deletions lib/interceptor.js
Expand Up @@ -102,7 +102,10 @@ module.exports = class Interceptor {
replyWithError(errorMessage) {
this.errorMessage = errorMessage

_.defaults(this.options, this.scope.scopeOptions)
this.options = {
...this.scope.scopeOptions,
...this.options,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these guaranteed to be shallow objects?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure, though I think this is the same behavior as _.defaults.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, your change is parity.
But I'm questioning if that is correct. Some of the options are objects, eg reqheaders. So I think it's a bug that it doesn't do deep cloning.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's possible. I don't know what the use cases are for merging these options. I'm not sure if it's possible that reqheaders could end up in both (or if merging them would be the correct thing to do). Would you like me to add a todo comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, yeah it's a weird undocumented case. I think I'd vote for keeping with your current change since it keeps parity and come back to it again if anyone ever opens an issue.


this.scope.add(this._key, this)
return this.scope
Expand Down Expand Up @@ -132,7 +135,10 @@ module.exports = class Interceptor {
}
}

_.defaults(this.options, this.scope.scopeOptions)
this.options = {
...this.scope.scopeOptions,
...this.options,
}

this.rawHeaders = common.headersInputToRawArray(rawHeaders)

Expand Down Expand Up @@ -557,7 +563,7 @@ module.exports = class Interceptor {
delay(opts) {
let headDelay
let bodyDelay
if (_.isNumber(opts)) {
if (typeof opts === 'number') {
headDelay = opts
bodyDelay = 0
} else if (typeof opts === 'object') {
Expand Down
10 changes: 5 additions & 5 deletions lib/scope.js
Expand Up @@ -7,7 +7,6 @@ const { addInterceptor, isOn } = require('./intercept')
const common = require('./common')
const assert = require('assert')
const url = require('url')
const _ = require('lodash')
const debug = require('debug')('nock.scope')
const { EventEmitter } = require('events')
const util = require('util')
Expand Down Expand Up @@ -184,7 +183,7 @@ class Scope extends EventEmitter {
}
return candidate.replace(filteringArguments[0], filteringArguments[1])
}
} else if (_.isFunction(arguments[0])) {
} else if (typeof arguments[0] === 'function') {
return arguments[0]
}
}
Expand Down Expand Up @@ -356,9 +355,10 @@ function define(nockDefs) {
} else if (nockDef.responseIsBinary) {
response = Buffer.from(nockDef.response, 'hex')
} else {
response = _.isString(nockDef.response)
? tryJsonParse(nockDef.response)
: nockDef.response
response =
typeof nockDef.response === 'string'
? tryJsonParse(nockDef.response)
: nockDef.response
}

const scope = new Scope(nscope, options)
Expand Down