Skip to content

Commit

Permalink
refactor: replace some lodash usage with native (#1685)
Browse files Browse the repository at this point in the history
For: #1285

Not logic changes, just making use of newer JS.
  • Loading branch information
mastermatt authored and gr2m committed Sep 5, 2019
1 parent 9aba710 commit 4ff3c66
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 61 deletions.
14 changes: 5 additions & 9 deletions lib/back.js
Expand Up @@ -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
}
Expand Down
24 changes: 11 additions & 13 deletions lib/common.js
Expand Up @@ -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".
Expand Down Expand Up @@ -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)
}

/**
Expand All @@ -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):
Expand All @@ -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]
})
Expand Down
31 changes: 6 additions & 25 deletions lib/intercept.js
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion lib/interceptor.js
Expand Up @@ -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)) {
Expand Down
12 changes: 4 additions & 8 deletions lib/request_overrider.js
Expand Up @@ -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
Expand All @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions lib/scope.js
Expand Up @@ -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')
Expand All @@ -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)) {
Expand Down
6 changes: 3 additions & 3 deletions tests/test_common.js
Expand Up @@ -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',
Expand Down

0 comments on commit 4ff3c66

Please sign in to comment.