diff --git a/lib/back.js b/lib/back.js index 75eb94132..f3040bf4a 100644 --- a/lib/back.js +++ b/lib/back.js @@ -154,15 +154,11 @@ const record = { const context = load(fixture, options) if (!context.isLoaded) { - recorder.record( - _.assign( - { - dont_print: true, - output_objects: true, - }, - options && options.recorder - ) - ) + recorder.record({ + dont_print: true, + output_objects: true, + ...options.recorder, + }) context.isRecording = true } diff --git a/lib/common.js b/lib/common.js index 0fec60dfa..d49a85d10 100644 --- a/lib/common.js +++ b/lib/common.js @@ -214,13 +214,9 @@ const headersFieldNamesToLowerCase = function(headers) { return lowerCaseHeaders } -const headersFieldsArrayToLowerCase = function(headers) { - return _.uniq( - _.map(headers, function(fieldName) { - return fieldName.toLowerCase() - }) - ) -} +const headersFieldsArrayToLowerCase = headers => [ + ...new Set(headers.map(fieldName => fieldName.toLowerCase())), +] /** * Converts the various accepted formats of headers into a flat array representing "raw headers". @@ -421,10 +417,12 @@ function percentEncode(str) { } function matchStringOrRegexp(target, pattern) { - const str = - (!_.isUndefined(target) && target.toString && target.toString()) || '' + const targetStr = + target === undefined || target === null ? '' : String(target) - return pattern instanceof RegExp ? pattern.test(str) : str === String(pattern) + return pattern instanceof RegExp + ? pattern.test(targetStr) + : targetStr === String(pattern) } /** @@ -444,8 +442,8 @@ function formatQueryValue(key, value, stringFormattingFn) { case _.isBoolean(value): value = value.toString() break - case _.isNull(value): // fall-through - case _.isUndefined(value): + case value === null: + case value === undefined: value = '' break case _.isString(value): @@ -455,7 +453,7 @@ function formatQueryValue(key, value, stringFormattingFn) { break case value instanceof RegExp: break - case _.isArray(value): { + case Array.isArray(value): { value = value.map(function(val, idx) { return formatQueryValue(idx, val, stringFormattingFn)[1] }) diff --git a/lib/intercept.js b/lib/intercept.js index f0549fa4d..f7f0a0f52 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -332,43 +332,24 @@ function restoreOverriddenClientRequest() { function isActive() { // If ClientRequest has been overwritten by Nock then originalClientRequest is not undefined. // This means that Nock has been activated. - return !_.isUndefined(originalClientRequest) + return originalClientRequest !== undefined } function interceptorScopes() { - return _.reduce( - allInterceptors, - function(result, interceptors) { - for (const interceptor in interceptors.scopes) { - result = result.concat(interceptors.scopes[interceptor].__nock_scope) - } - - return result - }, - [] - ) + const nestedInterceptors = Object.values(allInterceptors).map(i => i.scopes) + return [].concat(...nestedInterceptors).map(i => i.scope) } function isDone() { - return _.every(interceptorScopes(), function(scope) { - return scope.isDone() - }) + return interceptorScopes().every(scope => scope.isDone()) } function pendingMocks() { - return _.flatten( - _.map(interceptorScopes(), function(scope) { - return scope.pendingMocks() - }) - ) + return [].concat(...interceptorScopes().map(scope => scope.pendingMocks())) } function activeMocks() { - return _.flatten( - _.map(interceptorScopes(), function(scope) { - return scope.activeMocks() - }) - ) + return [].concat(...interceptorScopes().map(scope => scope.activeMocks())) } function activate() { diff --git a/lib/interceptor.js b/lib/interceptor.js index a5e0d1b61..e0a8711f0 100644 --- a/lib/interceptor.js +++ b/lib/interceptor.js @@ -205,7 +205,7 @@ module.exports = class Interceptor { return true } - if (!_.isUndefined(reqHeader) && !_.isUndefined(header)) { + if (reqHeader !== undefined && header !== undefined) { if (_.isFunction(reqHeader)) { return reqHeader(header) } else if (common.matchStringOrRegexp(header, reqHeader)) { diff --git a/lib/request_overrider.js b/lib/request_overrider.js index 230bb5248..0d131a7b3 100644 --- a/lib/request_overrider.js +++ b/lib/request_overrider.js @@ -73,9 +73,7 @@ class InterceptedRequestHandler { // affecting the user so we use a clone of the object. ...options, // We use lower-case header field names throughout Nock. - headers: options.headers - ? common.headersFieldNamesToLowerCase(options.headers) - : undefined, + headers: common.headersFieldNamesToLowerCase(options.headers || {}), } this.interceptors = interceptors @@ -94,13 +92,11 @@ class InterceptedRequestHandler { const { options } = this - if (options.headers) { - _.forOwn(options.headers, (val, key) => { - setHeader(req, key, val) - }) + for (const [name, val] of Object.entries(options.headers)) { + setHeader(req, name, val) } - if (options.auth && (!options.headers || !options.headers.authorization)) { + if (options.auth && !options.headers.authorization) { setHeader( req, 'Authorization', diff --git a/lib/scope.js b/lib/scope.js index a4701a14a..3b0df85a1 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -268,7 +268,7 @@ function load(path) { function getStatusFromDefinition(nockDef) { // Backward compatibility for when `status` was encoded as string in `reply`. - if (!_.isUndefined(nockDef.reply)) { + if (nockDef.reply !== undefined) { const parsedReply = parseInt(nockDef.reply, 10) if (isNaN(parsedReply)) { throw Error('`reply`, when present, must be a numeric string') @@ -286,7 +286,7 @@ function getScopeFromDefinition(nockDef) { if (nockDef.port !== undefined) { // Include `port` into scope if it doesn't exist. const options = url.parse(nockDef.scope) - if (_.isNull(options.port)) { + if (options.port === null) { return `${nockDef.scope}:${nockDef.port}` } else { if (parseInt(options.port) !== parseInt(nockDef.port)) { diff --git a/tests/test_common.js b/tests/test_common.js index 8c50a6b03..58405a95d 100644 --- a/tests/test_common.js +++ b/tests/test_common.js @@ -157,16 +157,16 @@ test('headersFieldNamesToLowerCase throws on conflicting keys', t => { test('headersFieldsArrayToLowerCase works on arrays', function(t) { t.deepEqual( - // Sort for comparison beause order doesn't matter. + // Sort for comparison because order doesn't matter. common.headersFieldsArrayToLowerCase(['HoSt', 'Content-typE']).sort(), ['content-type', 'host'] ) t.end() }) -test('headersFieldsArrayToLowerCase deduplicates arrays', function(t) { +test('headersFieldsArrayToLowerCase de-duplicates arrays', function(t) { t.deepEqual( - // Sort for comparison beause order doesn't matter. + // Sort for comparison because order doesn't matter. common .headersFieldsArrayToLowerCase([ 'hosT',