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

Use String.includes() when possible #675

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function bin (args, silent, cb) {
if (!silent) output(b)
process.nextTick(cb.bind(this, null, b))

if (npm.config.get('global') && PATH.indexOf(b) === -1) {
if (npm.config.get('global') && !PATH.includes(b)) {
npm.config.get('logstream').write('(not in PATH env variable)\n')
}
}
4 changes: 2 additions & 2 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ function rebuildBundles (pkg, folder, cb) {
// not a .folder, like .bin or .hooks
return !file.match(/^[._-]/) &&
// not some old 0.x style bundle
file.indexOf('@') === -1 &&
!file.includes('@') &&
// either not a dep, or explicitly bundled
(deps.indexOf(file) === -1 || bundles.indexOf(file) !== -1)
(!deps.includes(file) || bundles.includes(file))
}).map(function (file) {
file = path.resolve(folder, 'node_modules', file)
return function (cb) {
Expand Down
2 changes: 1 addition & 1 deletion lib/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function completion (args, cb) {

console.error(opts)

if (partialWords.slice(0, -1).indexOf('--') === -1) {
if (!partialWords.slice(0, -1).includes('--')) {
if (word.charAt(0) === '-') return configCompl(opts, cb)
if (words[w - 1] &&
words[w - 1].charAt(0) === '-' &&
Expand Down
4 changes: 2 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function set (key, val, cb) {
return unknown('', cb)
}
if (val === undefined) {
if (key.indexOf('=') !== -1) {
if (key.includes('=')) {
var k = key.split('=')
key = k.shift()
val = k.join('=')
Expand Down Expand Up @@ -176,7 +176,7 @@ function sort (a, b) {
}

function publicVar (k) {
return !(k.charAt(0) === '_' || k.indexOf(':_') !== -1)
return !(k.charAt(0) === '_' || k.includes(':_'))
}

function getKeys (data) {
Expand Down
10 changes: 5 additions & 5 deletions lib/config/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,11 @@ function parseField (f, k) {

// type can be an array or single thing.
var typeList = [].concat(types[k])
var isPath = typeList.indexOf(path) !== -1
var isBool = typeList.indexOf(Boolean) !== -1
var isString = typeList.indexOf(String) !== -1
var isUmask = typeList.indexOf(Umask) !== -1
var isNumber = typeList.indexOf(Number) !== -1
var isPath = typeList.includes(path)
var isBool = typeList.includes(Boolean)
var isString = typeList.includes(String)
var isUmask = typeList.includes(Umask)
var isNumber = typeList.includes(Number)

f = ('' + f).trim()

Expand Down
4 changes: 2 additions & 2 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ nopt.invalidHandler = function (k, val, type) {
log.warn('invalid config', k + '=' + JSON.stringify(val))

if (Array.isArray(type)) {
if (type.indexOf(url) !== -1) type = url
else if (type.indexOf(path) !== -1) type = path
if (type.includes(url)) type = url
else if (type.includes(path)) type = path
}

switch (type) {
Expand Down
7 changes: 3 additions & 4 deletions lib/help-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function searchFiles (args, files, cb) {
// skip if no matches at all
var match
for (var a = 0, l = args.length; a < l && !match; a++) {
match = data.toLowerCase().indexOf(args[a].toLowerCase()) !== -1
match = data.toLowerCase().includes(args[a].toLowerCase())
}
if (!match) return

Expand All @@ -69,8 +69,7 @@ function searchFiles (args, files, cb) {
match = false
if (nextLine) {
for (a = 0, ll = args.length; a < ll && !match; a++) {
match = nextLine.toLowerCase()
.indexOf(args[a].toLowerCase()) !== -1
match = nextLine.toLowerCase().includes(args[a].toLowerCase())
}
if (match) {
// skip over the next line, and the line after it.
Expand All @@ -81,7 +80,7 @@ function searchFiles (args, files, cb) {

match = false
for (a = 0, ll = args.length; a < ll && !match; a++) {
match = line.toLowerCase().indexOf(args[a].toLowerCase()) !== -1
match = line.toLowerCase().includes(args[a].toLowerCase())
}
if (match) {
// skip over the next line
Expand Down
2 changes: 1 addition & 1 deletion lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function help (args, cb) {
}

// npm apihelp <section>: Prefer section 3 over section 1
var apihelp = argv.length && argv[0].indexOf('api') !== -1
var apihelp = argv.length && argv[0].includes('api')
var pref = apihelp ? [3, 1, 5, 7] : [1, 3, 5, 7]
if (argnum) {
pref = [ argnum ].concat(pref.filter(function (n) {
Expand Down
2 changes: 1 addition & 1 deletion lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ install.completion = function (opts, cb) {
null,
{
fullPath: fullPath,
isPackage: contents.indexOf('package.json') !== -1
isPackage: contents.includes('package.json')
}
)
})
Expand Down
2 changes: 1 addition & 1 deletion lib/install/diff-trees.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ var sortActions = module.exports.sortActions = function (differences) {

// safety net, anything excluded above gets tacked on the end
differences.forEach((_) => {
if (sorted.indexOf(_) === -1) sorted.push(_)
if (!sorted.includes(_)) sorted.push(_)
})

return sorted
Expand Down
2 changes: 1 addition & 1 deletion lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function linkInstall (pkgs, cb) {

// if it's a folder, a random not-installed thing, or not a scoped package,
// then link or install it first
if (pkg[0] !== '@' && (pkg.indexOf('/') !== -1 || pkg.indexOf('\\') !== -1)) {
if (pkg[0] !== '@' && (pkg.includes('/') || pkg.includes('\\'))) {
return fs.lstat(path.resolve(pkg), function (er, st) {
if (er || !st.isDirectory()) {
npm.commands.install(t, pkg, n)
Expand Down
4 changes: 2 additions & 2 deletions lib/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function ls (args, silent, cb) {
}

function inList (list, value) {
return list.indexOf(value) !== -1
return list.includes(value)
}

var lsFromTree = ls.fromTree = function (dir, physicalTree, args, silent, cb) {
Expand Down Expand Up @@ -125,7 +125,7 @@ function pruneNestedExtraneous (data, visited) {
for (var i in data.dependencies) {
if (data.dependencies[i].extraneous) {
data.dependencies[i].dependencies = {}
} else if (visited.indexOf(data.dependencies[i]) === -1) {
} else if (!visited.includes(data.dependencies[i])) {
pruneNestedExtraneous(data.dependencies[i], visited)
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@

var littleGuys = [ 'isntall', 'verison' ]
var fullList = cmdList.concat(aliasNames).filter(function (c) {
return plumbing.indexOf(c) === -1
return !plumbing.includes(c)
})
var abbrevs = abbrev(fullList)

// we have our reasons
fullList = npm.fullList = fullList.filter(function (c) {
return littleGuys.indexOf(c) === -1
return !littleGuys.includes(c)
})

var registryRefer
Expand Down Expand Up @@ -162,7 +162,7 @@

return commandCache[a]
},
enumerable: fullList.indexOf(c) !== -1,
enumerable: fullList.includes(c),
configurable: true })

// make css-case commands callable via camelCase as well
Expand All @@ -186,7 +186,7 @@
return '-' + m.toLowerCase()
})
}
if (plumbing.indexOf(c) !== -1) return c
if (plumbing.includes(c)) return c
var a = abbrevs[c]
while (aliases[a]) {
a = aliases[a]
Expand Down
2 changes: 1 addition & 1 deletion lib/outdated.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ function shouldUpdate (args, tree, dep, has, req, depth, pkgpath, opts, cb, type
cb)
}

if (args.length && args.indexOf(dep) === -1) return skip()
if (args.length && !args.includes(dep)) return skip()

if (tree.isLink && req == null) return skip()

Expand Down
4 changes: 2 additions & 2 deletions lib/owner.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ owner.completion = function (opts, cb) {
return npmFetch.json(byUser, opts).then(d => {
return d[theUser].filter(
// kludge for server adminery.
p => un === 'isaacs' || d[un].indexOf(p) === -1
p => un === 'isaacs' || !d[un].includes(p)
)
})
}
Expand All @@ -59,7 +59,7 @@ owner.completion = function (opts, cb) {
return npmFetch.json(byUser, opts).then(d => {
var mine = d[un] || []
var theirs = d[theUser] || []
return mine.filter(p => theirs.indexOf(p) === -1)
return mine.filter(p => !theirs.includes(p))
})
} else {
// just list all users who aren't me.
Expand Down
2 changes: 1 addition & 1 deletion lib/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function set (args) {
'npm profile set password\n' +
'Do not include your current or new passwords on the command line.'))
}
if (writableProfileKeys.indexOf(prop) === -1) {
if (!writableProfileKeys.includes(prop)) {
return Promise.reject(Error(`"${prop}" is not a property we can set. Valid properties are: ` + writableProfileKeys.join(', ')))
}
return BB.try(() => {
Expand Down
2 changes: 1 addition & 1 deletion lib/prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Pruner.prototype.loadAllDepsIntoIdealTree = function (cb) {
return moduleName(child)
}
function matchesArg (name) {
return self.args.length === 0 || self.args.indexOf(name) !== -1
return self.args.length === 0 || self.args.includes(name)
}
function nameObj (name) {
return {name: name}
Expand Down
4 changes: 2 additions & 2 deletions lib/run-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ runScript.completion = function (opts, cb) {
if (er) d = {}
var scripts = Object.keys(d.scripts || {})
console.error('local scripts', scripts)
if (scripts.indexOf(argv[2]) !== -1) return cb()
if (scripts.includes(argv[2])) return cb()
// ok, try to find out which package it was, then
var pref = npm.config.get('global') ? npm.config.get('prefix')
: npm.localPrefix
Expand Down Expand Up @@ -85,7 +85,7 @@ function list (cb) {
var scripts = []
var runScripts = []
allScripts.forEach(function (script) {
if (cmdList.indexOf(script) !== -1) scripts.push(script)
if (cmdList.includes(script)) scripts.push(script)
else runScripts.push(script)
})

Expand Down
2 changes: 1 addition & 1 deletion lib/search/package-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ function match (words, pattern) {
pattern = new RegExp(pattern.substr(1, pattern.length - 1))
return words.match(pattern)
}
return words.indexOf(pattern) !== -1
return words.includes(pattern)
}
2 changes: 1 addition & 1 deletion lib/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function update_ (args) {
const isTransitive = !(ww.dep.requiredBy || []).some((p) => p.isTop)
const key = where + ':' + String(isTransitive)
if (!toInstall[key]) toInstall[key] = {where: where, opts: {saveOnlyLock: isTransitive}, what: []}
if (toInstall[key].what.indexOf(ww.what) === -1) toInstall[key].what.push(ww.what)
if (!toInstall[key].what.includes(ww.what)) toInstall[key].what.push(ww.what)
})
return Bluebird.each(Object.keys(toInstall), (key) => {
const deps = toInstall[key]
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/error-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function errorMessage (er) {
// npm ERR! code E401
// npm ERR! Unable to authenticate, need: Basic
const auth = (er.headers && er.headers['www-authenticate'] && er.headers['www-authenticate'].map((au) => au.split(/,\s*/))[0]) || []
if (auth.indexOf('Bearer') !== -1) {
if (auth.includes('Bearer')) {
short.push(['', 'Unable to authenticate, your authentication token seems to be invalid.'])
detail.push([
'',
Expand All @@ -166,7 +166,7 @@ function errorMessage (er) {
' npm login'
].join('\n')
])
} else if (auth.indexOf('Basic') !== -1) {
} else if (auth.includes('Basic')) {
short.push(['', 'Incorrect or missing password.'])
detail.push([
'',
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/open-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function open (url, errMsg, cb, browser = npm.config.get('brows
output(alternateMsg)
}

const skipBrowser = process.argv.indexOf('--no-browser') > -1
const skipBrowser = process.argv.includes('--no-browser')

if (skipBrowser) {
printAlternateMsg()
Expand Down
2 changes: 1 addition & 1 deletion lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function parseLastGitTag (cb) {
var options = { env: process.env }
git.whichAndExec(['describe', '--abbrev=0'], options, function (er, stdout) {
if (er) {
if (er.message.indexOf('No names found') !== -1) return cb(new Error('No tags found'))
if (er.message.includes('No names found')) return cb(new Error('No tags found'))
return cb(er)
}

Expand Down
8 changes: 4 additions & 4 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ view.completion = function (opts, cb) {
if (!d) return f
pref = pref || []
Object.keys(d).forEach(function (k) {
if (k.charAt(0) === '_' || k.indexOf('.') !== -1) return
if (k.charAt(0) === '_' || k.includes('.')) return
const p = pref.concat(k).join('.')
f.push(p)
if (Array.isArray(d[k])) {
Expand Down Expand Up @@ -162,15 +162,15 @@ function fetchAndRead (nv, args, silent, opts, cb) {
if (!args.length) args = ['']

// remove readme unless we asked for it
if (args.indexOf('readme') === -1) {
if (!args.includes('readme')) {
delete data.readme
}

Object.keys(versions).forEach(function (v) {
if (semver.satisfies(v, version, true)) {
args.forEach(function (args) {
// remove readme unless we asked for it
if (args.indexOf('readme') !== -1) {
if (args.includes('readme')) {
delete versions[v].readme
}
results.push(showFields(data, versions[v], args))
Expand Down Expand Up @@ -447,7 +447,7 @@ function printData (data, name, opts, cb) {
}
if (!opts.json) {
if (f && includeFields) f += ' = '
if (d.indexOf('\n') !== -1) d = ' \n' + d
if (d.includes('\n')) d = ' \n' + d
msg += (includeVersions ? name + '@' + v + ' ' : '') +
(includeFields ? f : '') + d + '\n'
}
Expand Down
2 changes: 1 addition & 1 deletion test/tap/00-verify-bundle-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var bundled = manifest.bundleDependencies
test('all deps are bundled deps or dev deps', function (t) {
deps.forEach(function (name) {
t.assert(
bundled.indexOf(name) !== -1,
bundled.includes(name),
name + ' is in bundledDependencies'
)
})
Expand Down
6 changes: 3 additions & 3 deletions test/tap/config-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ test('get lines', function (t) {
line: i
}
}
} else if (exceptions.indexOf(f) === -1 && f.indexOf('.cache') === -1) {
} else if (!exceptions.includes(f) && !f.includes('.cache')) {
t.fail('non-string-literal config used in ' + f + ':' + i)
}
})
Expand Down Expand Up @@ -105,8 +105,8 @@ test('check configs', function (t) {
if (CONFS[c1].file.indexOf(lib) === 0) {
t.ok(DOC[c1], 'should be documented ' + c1 + ' ' +
CONFS[c1].file + ':' + CONFS[c1].line)
t.ok(types.indexOf(c1) !== -1, 'should be defined in npmconf ' + c1)
t.ok(defaults.indexOf(c1) !== -1, 'should have default in npmconf ' + c1)
t.ok(types.includes(c1), 'should be defined in npmconf ' + c1)
t.ok(defaults.includes(c1), 'should have default in npmconf ' + c1)
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/tap/gist-short-shortcut-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test('gist-short-shortcut-package', function (t) {
'child_process': {
'execFile': function (cmd, args, options, cb) {
process.nextTick(function () {
if (args.indexOf('clone') === -1) return cb(null, '', '')
if (!args.includes('clone')) return cb(null, '', '')
var cloneUrl = cloneUrls.shift()
if (cloneUrl) {
t.is(args[args.length - 2], cloneUrl[0], cloneUrl[1])
Expand Down
2 changes: 1 addition & 1 deletion test/tap/gist-short-shortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test('gist-shortcut', function (t) {
'child_process': {
'execFile': function (cmd, args, options, cb) {
process.nextTick(function () {
if (args.indexOf('clone') === -1) return cb(null, '', '')
if (!args.includes('clone')) return cb(null, '', '')
var cloneUrl = cloneUrls.shift()
if (cloneUrl) {
t.is(args[args.length - 2], cloneUrl[0], cloneUrl[1])
Expand Down
2 changes: 1 addition & 1 deletion test/tap/gist-shortcut-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('gist-shortcut-package', function (t) {
'child_process': {
'execFile': function (cmd, args, options, cb) {
process.nextTick(function () {
if (args.indexOf('clone') === -1) return cb(null, '', '')
if (!args.includes('clone')) return cb(null, '', '')
var cloneUrl = cloneUrls.shift()
if (cloneUrl) {
t.is(args[args.length - 2], cloneUrl[0], cloneUrl[1])
Expand Down
Loading