Skip to content

Commit

Permalink
fix: replace deprecated String.prototype.substr() (#4667)
Browse files Browse the repository at this point in the history
.substr() is deprecated so we replace it with .slice() which works similarily but isn't deprecated

Signed-off-by: Tobias Speicher <[email protected]>
  • Loading branch information
CommanderRoot authored Apr 4, 2022
1 parent 44108f7 commit e3da5df
Show file tree
Hide file tree
Showing 17 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/commands/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class Cache extends BaseCommand {
async verify () {
const cache = path.join(this.npm.cache, '_cacache')
const prefix = cache.indexOf(process.env.HOME) === 0
? `~${cache.substr(process.env.HOME.length)}`
? `~${cache.slice(process.env.HOME.length)}`
: cache
const stats = await cacache.verify(cache)
this.npm.output(`Cache verified and compressed (${prefix})`)
Expand Down
6 changes: 3 additions & 3 deletions lib/commands/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ class Completion extends BaseCommand {
const word = words[w]
const line = COMP_LINE
const point = +COMP_POINT
const partialLine = line.substr(0, point)
const partialLine = line.slice(0, point)
const partialWords = words.slice(0, w)

// figure out where in that last word the point is.
const partialWordRaw = args[w]
let i = partialWordRaw.length
while (partialWordRaw.substr(0, i) !== partialLine.substr(-1 * i) && i > 0) {
while (partialWordRaw.slice(0, i) !== partialLine.slice(-1 * i) && i > 0) {
i--
}

const partialWord = unescape(partialWordRaw.substr(0, i))
const partialWord = unescape(partialWordRaw.slice(0, i))
partialWords.push(partialWord)

const opts = {
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/doctor.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Doctor extends BaseCommand {
return ''
} catch (er) {
if (/^E\d{3}$/.test(er.code || '')) {
throw er.code.substr(1) + ' ' + er.message
throw er.code.slice(1) + ' ' + er.message
} else {
throw er.message
}
Expand Down Expand Up @@ -211,7 +211,7 @@ class Doctor extends BaseCommand {
const gid = process.getgid()
const files = new Set([root])
for (const f of files) {
tracker.silly('checkFilesPermission', f.substr(root.length + 1))
tracker.silly('checkFilesPermission', f.slice(root.length + 1))
const st = await lstat(f).catch(er => {
// if it can't be missing, or if it can and the error wasn't that it was missing
if (!missingOk || er.code !== 'ENOENT') {
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/help-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ class HelpSearch extends BaseCommand {
const finder = line.toLowerCase().split(arg.toLowerCase())
let p = 0
for (const f of finder) {
hilitLine.push(line.substr(p, f.length))
const word = line.substr(p + f.length, arg.length)
hilitLine.push(line.slice(p, p + f.length))
const word = line.slice(p + f.length, p + f.length + arg.length)
const hilit = color.bgBlack(color.red(word))
hilitLine.push(hilit)
p += f.length + arg.length
Expand Down
6 changes: 3 additions & 3 deletions lib/search/format-package-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function addColorMarker (str, arg, i) {

if (arg.charAt(0) === '/') {
return str.replace(
new RegExp(arg.substr(1, arg.length - 2), 'gi'),
new RegExp(arg.slice(1, -1), 'gi'),
bit => markStart + bit + markEnd
)
}
Expand All @@ -121,9 +121,9 @@ function addColorMarker (str, arg, i) {
var p = 0

return pieces.map(function (piece) {
piece = str.substr(p, piece.length)
piece = str.slice(p, p + piece.length)
var mark = markStart +
str.substr(p + piece.length, arg.length) +
str.slice(p + piece.length, p + piece.length + arg.length) +
markEnd
p += piece.length + arg.length
return piece + mark
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 @@ -36,7 +36,7 @@ function filterWords (data, include, exclude, opts) {
function match (words, pattern) {
if (pattern.charAt(0) === '/') {
pattern = pattern.replace(/\/$/, '')
pattern = new RegExp(pattern.substr(1, pattern.length - 1))
pattern = new RegExp(pattern.slice(1))
return words.match(pattern)
}
return words.indexOf(pattern) !== -1
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/config/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ const wrapAll = s => {
return (
'* ' +
block
.substr(1)
.slice(1)
.trim()
.split('\n* ')
.map(li => {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/npm-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const wrap = (arr) => {
out[l] = c
}
}
return out.join('\n ').substr(2)
return out.join('\n ').slice(2)
}

const usages = async (npm) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/tar.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ const logTar = (tarball, opts = {}) => {
{
name: 'integrity:',
value:
tarball.integrity.toString().substr(0, 20) +
tarball.integrity.toString().slice(0, 20) +
'[...]' +
tarball.integrity.toString().substr(80),
tarball.integrity.toString().slice(80),
},
tarball.bundled.length && { name: 'bundled deps:', value: tarball.bundled.length },
tarball.bundled.length && {
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/lib/retire-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const pathSafeHash = s =>
.update(s)
.digest('base64')
.replace(/[^a-zA-Z0-9]+/g, '')
.substr(0, 8)
.slice(0, 8)

const retirePath = from => {
const d = dirname(from)
Expand Down
4 changes: 2 additions & 2 deletions workspaces/arborist/lib/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ class Shrinkwrap {
const pathFixed = !resolved ? null
: !/^file:/.test(resolved) ? resolved
// resolve onto the metadata path
: `file:${resolve(this.path, resolved.substr(5))}`
: `file:${resolve(this.path, resolved.slice(5))}`

// if we have one, only set the other if it matches
// otherwise it could be for a completely different thing.
Expand Down Expand Up @@ -1056,7 +1056,7 @@ class Shrinkwrap {
// turn absolute file: paths into relative paths from the node
// this especially shows up with workspace edges when the root
// node is also a workspace in the set.
const p = resolve(node.realpath, spec.substr('file:'.length))
const p = resolve(node.realpath, spec.slice('file:'.length))
set[k] = `file:${relpath(node.realpath, p)}`
} else {
set[k] = spec
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/lib/version-from-tgz.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = (name, tgz) => {
// basename checking. Note that registries can
// be mounted below the root url, so /a/b/-/x/y/foo/-/foo-1.2.3.tgz
// is a potential option.
const tfsplit = u.path.substr(1).split('/-/')
const tfsplit = u.path.slice(1).split('/-/')
if (tfsplit.length > 1) {
const afterTF = tfsplit.pop()
if (afterTF === base) {
Expand Down
8 changes: 4 additions & 4 deletions workspaces/arborist/scripts/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ Options:
for (let i = 2; i < process.argv.length; i++) {
const arg = process.argv[i]
if (/^--previous=/.test(arg)) {
options.previous = arg.substr('--previous='.length)
options.previous = arg.slice('--previous='.length)
} else if (/^--warn-range=[0-9]+/.test(arg)) {
options.warnRange = +arg.substr('--warn-range='.length)
options.warnRange = +arg.slice('--warn-range='.length)
} else if (/^--cache=/.test(arg)) {
options.cache = resolve(arg.substr('--cache='.length))
options.cache = resolve(arg.slice('--cache='.length))
} else if (/^--save=/.test(arg)) {
const save = arg.substr('--save='.length)
const save = arg.slice('--save='.length)
if (/[/\\]|^\.\.?$/.test(save)) {
throw new Error('save cannot have slashes or be . or ..')
}
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/test/arborist/load-actual.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {

// strip the fixtures path off of the trees in snapshots
const pp = path => path &&
normalizePath(path).substr(normalizePath(fixtures).length + 1)
normalizePath(path).slice(normalizePath(fixtures).length + 1)
const defixture = obj => {
if (obj instanceof Set) {
return new Set([...obj].map(defixture))
Expand Down
2 changes: 1 addition & 1 deletion workspaces/libnpmdiff/lib/should-print-patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const shouldPrintPatch = (path, opts = {}) => {
filename.startsWith('.')
? filename
: extname(filename)
).substr(1)
).slice(1)

return !binaryExtensions.includes(extension)
}
Expand Down
2 changes: 1 addition & 1 deletion workspaces/libnpmhook/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cmd.add = (name, endpoint, secret, opts = {}) => {
}
if (name[0] === '~') {
type = 'owner'
name = name.substr(1)
name = name.slice(1)
}
return fetch.json('/-/npm/v1/hooks/hook', {
...opts,
Expand Down
2 changes: 1 addition & 1 deletion workspaces/libnpmpublish/lib/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const unpublish = async (spec, opts) => {
...opts,
query: { write: true },
})
const tarballUrl = new URL(dist.tarball).pathname.substr(1)
const tarballUrl = new URL(dist.tarball).pathname.slice(1)
await npmFetch(`${tarballUrl}/-rev/${_rev}`, {
...opts,
method: 'DELETE',
Expand Down

0 comments on commit e3da5df

Please sign in to comment.