diff --git a/lib/back.js b/lib/back.js index acf9d3489..a0e4467d7 100644 --- a/lib/back.js +++ b/lib/back.js @@ -61,6 +61,7 @@ function Back(fixtureName, options, nockedFn) { ) } + // TODO-12.x: Replace with `typeof fixtureName === 'string'`. if (!_.isString(fixtureName)) { throw new Error('Parameter fixtureName must be a string') } @@ -70,7 +71,7 @@ function Back(fixtureName, options, nockedFn) { } else if (arguments.length === 2) { // If 2nd parameter is a function then `options` has been omitted // otherwise `options` haven't been omitted but `nockedFn` was. - if (_.isFunction(options)) { + if (options instanceof Function) { nockedFn = options options = {} } @@ -88,7 +89,7 @@ function Back(fixtureName, options, nockedFn) { debug('context:', context) // If nockedFn is a function then invoke it, otherwise return a promise resolving to nockDone. - if (_.isFunction(nockedFn)) { + if (nockedFn instanceof Function) { nockedFn.call(context, nockDone) } else { return Promise.resolve({ nockDone, context }) @@ -169,7 +170,7 @@ const record = { if (context.isRecording) { let outputs = recorder.outputs() - if (typeof options.afterRecord === 'function') { + if (options.afterRecord instanceof Function) { outputs = options.afterRecord(outputs) } diff --git a/lib/common.js b/lib/common.js index 3a7c6150c..995a06239 100644 --- a/lib/common.js +++ b/lib/common.js @@ -170,6 +170,7 @@ function stringifyRequest(options, body) { function isContentEncoded(headers) { const contentEncoding = headers['content-encoding'] + // TODO-12.x: Replace with `typeof contentEncoding === 'string'`. return _.isString(contentEncoding) && contentEncoding !== '' } @@ -314,7 +315,7 @@ const noDuplicatesHeaders = new Set([ */ function addHeaderLine(headers, name, value) { let values // code below expects `values` to be an array of strings - if (typeof value === 'function') { + if (value instanceof Function) { // Function values are evaluated towards the end of the response, before that we use a placeholder // string just to designate that the header exists. Useful when `Content-Type` is set with a function. values = [value.name] @@ -359,6 +360,7 @@ function deleteHeadersField(headers, fieldNameToDelete) { throw Error('headers must be an object') } + // TODO-12.x: Replace with `typeof fieldNameToDelete !== 'string'`. if (!_.isString(fieldNameToDelete)) { throw Error('field name must be a string') } @@ -440,6 +442,7 @@ function formatQueryValue(key, value, stringFormattingFn) { case value === undefined: value = '' break + // TODO-12.x: Replace with `typeof value === 'string'`. case _.isString(value): if (stringFormattingFn) { value = stringFormattingFn(value) @@ -453,7 +456,7 @@ function formatQueryValue(key, value, stringFormattingFn) { }) break } - case _.isObject(value): { + case typeof value === 'object': { value = Object.entries(value).reduce(function(acc, [subKey, subVal]) { const subPair = formatQueryValue(subKey, subVal, stringFormattingFn) acc[subPair[0]] = subPair[1] @@ -473,7 +476,7 @@ function isStream(obj) { obj && typeof obj !== 'string' && !Buffer.isBuffer(obj) && - _.isFunction(obj.setEncoding) + obj.setEncoding instanceof Function ) } @@ -497,7 +500,7 @@ function normalizeClientRequestArgs(input, options, cb) { input = null } - if (typeof options === 'function') { + if (options instanceof Function) { cb = options options = input || {} } else { diff --git a/lib/intercept.js b/lib/intercept.js index 75b936110..cc2cd7dfe 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -52,9 +52,10 @@ let allowNetConnect * nock.enableNetConnect(/(google|amazon)/); */ function enableNetConnect(matcher) { + // TODO-12.x: Replace with `typeof matcher === 'string'`. if (_.isString(matcher)) { allowNetConnect = new RegExp(matcher) - } else if (_.isObject(matcher) && _.isFunction(matcher.test)) { + } else if (matcher instanceof RegExp) { allowNetConnect = matcher } else { allowNetConnect = /.*/ @@ -170,7 +171,7 @@ function interceptorsFor(options) { return [ { options: { allowUnmocked: true }, - matchAddress() { + matchOrigin() { return false }, }, @@ -389,13 +390,12 @@ function activate() { const interceptors = interceptorsFor(options) if (isOn() && interceptors) { - const matches = !!_.find(interceptors, function(interceptor) { - return interceptor.matchAddress(options) - }) - - const allowUnmocked = !!_.find(interceptors, function(interceptor) { - return interceptor.options.allowUnmocked - }) + const matches = interceptors.some(interceptor => + interceptor.matchOrigin(options) + ) + const allowUnmocked = interceptors.some( + interceptor => interceptor.options.allowUnmocked + ) if (!matches && allowUnmocked) { let req diff --git a/lib/intercepted_request_router.js b/lib/intercepted_request_router.js index 7b898fc01..27e6cecee 100644 --- a/lib/intercepted_request_router.js +++ b/lib/intercepted_request_router.js @@ -113,7 +113,7 @@ class InterceptedRequestRouter { } this.requestBodyBuffers.push(buffer) } - if (typeof callback === 'function') { + if (callback instanceof Function) { callback() } } else { @@ -131,17 +131,17 @@ class InterceptedRequestRouter { debug('req.end') const { req } = this - if (typeof chunk === 'function') { + if (chunk instanceof Function) { callback = chunk chunk = null - } else if (typeof encoding === 'function') { + } else if (encoding instanceof Function) { callback = encoding encoding = null } if (!req.aborted && !this.playbackStarted) { req.write(chunk, encoding, () => { - if (typeof callback === 'function') { + if (callback instanceof Function) { callback() } this.startPlayback() diff --git a/lib/interceptor.js b/lib/interceptor.js index 0e173defc..a1a865287 100644 --- a/lib/interceptor.js +++ b/lib/interceptor.js @@ -110,7 +110,7 @@ module.exports = class Interceptor { reply(statusCode, body, rawHeaders) { // support the format of only passing in a callback - if (_.isFunction(statusCode)) { + if (statusCode instanceof Function) { if (arguments.length > 1) { // It's not very Javascript-y to throw an error for extra args to a function, but because // of legacy behavior, this error was added to reduce confusion for those migrating. @@ -126,7 +126,7 @@ module.exports = class Interceptor { } this.statusCode = statusCode || 200 - if (_.isFunction(body)) { + if (body instanceof Function) { this.replyFunction = body body = null } @@ -213,7 +213,7 @@ module.exports = class Interceptor { } if (reqHeader !== undefined && header !== undefined) { - if (_.isFunction(reqHeader)) { + if (reqHeader instanceof Function) { return reqHeader(header) } else if (common.matchStringOrRegexp(header, reqHeader)) { return true @@ -248,7 +248,7 @@ module.exports = class Interceptor { const requestMatchesFilter = ({ name, value: predicate }) => { const headerValue = req.getHeader(name) - if (typeof predicate === 'function') { + if (predicate instanceof Function) { return predicate(headerValue) } else { return common.matchStringOrRegexp(headerValue, predicate) @@ -315,7 +315,7 @@ module.exports = class Interceptor { matchKey = common.normalizeOrigin(proto, options.host, options.port) } - if (typeof this.uri === 'function') { + if (this.uri instanceof Function) { matches = common.matchStringOrRegexp(matchKey, this.basePath) && // This is a false positive, as `uri` is not bound to `this`. @@ -352,9 +352,9 @@ module.exports = class Interceptor { * Return true when the interceptor's method, protocol, host, port, and path * match the provided options. */ - matchAddress(options) { - const isRegex = _.isRegExp(this.path) - const isRegexBasePath = _.isRegExp(this.scope.basePath) + matchOrigin(options) { + const isRegex = this.path instanceof RegExp + const isRegexBasePath = this.scope.basePath instanceof RegExp const method = (options.method || 'GET').toUpperCase() let { path } = options @@ -395,7 +395,7 @@ module.exports = class Interceptor { debug('Interceptor queries: %j', this.queries) debug(' Request queries: %j', reqQueries) - if (_.isFunction(this.queries)) { + if (this.queries instanceof Function) { return this.queries(reqQueries) } @@ -456,7 +456,7 @@ module.exports = class Interceptor { return this } - if (_.isFunction(queries)) { + if (queries instanceof Function) { this.queries = queries return this } @@ -553,7 +553,7 @@ module.exports = class Interceptor { if (_.isNumber(opts)) { headDelay = opts bodyDelay = 0 - } else if (_.isObject(opts)) { + } else if (typeof opts === 'object') { headDelay = opts.head || 0 bodyDelay = opts.body || 0 } else { diff --git a/lib/match_body.js b/lib/match_body.js index 04a28bcff..84753d008 100644 --- a/lib/match_body.js +++ b/lib/match_body.js @@ -26,7 +26,7 @@ module.exports = function matchBody(options, spec, body) { // try to transform body to json let json - if (typeof spec === 'object' || typeof spec === 'function') { + if (typeof spec === 'object' || spec instanceof Function) { try { json = JSON.parse(body) } catch (err) { @@ -39,7 +39,7 @@ module.exports = function matchBody(options, spec, body) { } } - if (typeof spec === 'function') { + if (spec instanceof Function) { return spec.call(options, body) } @@ -56,12 +56,7 @@ module.exports = function matchBody(options, spec, body) { // Because the nature of URL encoding, all the values in the body have been cast to strings. // dataEqual does strict checking so we we have to cast the non-regexp values in the spec too. if (isUrlencoded) { - spec = mapValuesDeep(spec, function(val) { - if (_.isRegExp(val)) { - return val - } - return `${val}` - }) + spec = mapValuesDeep(spec, val => (val instanceof RegExp ? val : `${val}`)) } return common.dataEqual(spec, body) @@ -73,14 +68,10 @@ module.exports = function matchBody(options, spec, body) { */ function mapValuesDeep(obj, cb) { if (Array.isArray(obj)) { - return obj.map(function(v) { - return mapValuesDeep(v, cb) - }) + return obj.map(v => mapValuesDeep(v, cb)) } if (_.isPlainObject(obj)) { - return _.mapValues(obj, function(v) { - return mapValuesDeep(v, cb) - }) + return _.mapValues(obj, v => mapValuesDeep(v, cb)) } return cb(obj) } diff --git a/lib/playback_interceptor.js b/lib/playback_interceptor.js index c0c67fe5d..b1df24c8f 100644 --- a/lib/playback_interceptor.js +++ b/lib/playback_interceptor.js @@ -3,7 +3,6 @@ const util = require('util') const zlib = require('zlib') const debug = require('debug')('nock.playback_interceptor') -const _ = require('lodash') const common = require('./common') const DelayedBody = require('./delayed_body') @@ -100,7 +99,7 @@ function playbackInterceptor({ interceptor.markConsumed() let error - if (_.isObject(interceptor.errorMessage)) { + if (typeof interceptor.errorMessage === 'object') { error = interceptor.errorMessage } else { error = new Error(interceptor.errorMessage) @@ -271,7 +270,7 @@ function playbackInterceptor({ // Evaluate functional headers. common.forEachHeader(response.rawHeaders, (value, fieldName, i) => { - if (typeof value === 'function') { + if (value instanceof Function) { response.rawHeaders[i + 1] = value(req, response, responseBody) } }) diff --git a/lib/recorder.js b/lib/recorder.js index f5cf28e2d..ec961a982 100644 --- a/lib/recorder.js +++ b/lib/recorder.js @@ -344,10 +344,10 @@ function record(recOptions) { const oldEnd = req.end req.end = function(chunk, encoding, callback) { debug('req.end') - if (typeof chunk === 'function') { + if (chunk instanceof Function) { callback = chunk chunk = null - } else if (typeof encoding === 'function') { + } else if (encoding instanceof Function) { callback = encoding encoding = null }