Skip to content

Commit e3da5df

Browse files
fix: replace deprecated String.prototype.substr() (#4667)
.substr() is deprecated so we replace it with .slice() which works similarily but isn't deprecated Signed-off-by: Tobias Speicher <[email protected]>
1 parent 44108f7 commit e3da5df

File tree

17 files changed

+28
-28
lines changed

17 files changed

+28
-28
lines changed

lib/commands/cache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class Cache extends BaseCommand {
177177
async verify () {
178178
const cache = path.join(this.npm.cache, '_cacache')
179179
const prefix = cache.indexOf(process.env.HOME) === 0
180-
? `~${cache.substr(process.env.HOME.length)}`
180+
? `~${cache.slice(process.env.HOME.length)}`
181181
: cache
182182
const stats = await cacache.verify(cache)
183183
this.npm.output(`Cache verified and compressed (${prefix})`)

lib/commands/completion.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ class Completion extends BaseCommand {
9797
const word = words[w]
9898
const line = COMP_LINE
9999
const point = +COMP_POINT
100-
const partialLine = line.substr(0, point)
100+
const partialLine = line.slice(0, point)
101101
const partialWords = words.slice(0, w)
102102

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

110-
const partialWord = unescape(partialWordRaw.substr(0, i))
110+
const partialWord = unescape(partialWordRaw.slice(0, i))
111111
partialWords.push(partialWord)
112112

113113
const opts = {

lib/commands/doctor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class Doctor extends BaseCommand {
145145
return ''
146146
} catch (er) {
147147
if (/^E\d{3}$/.test(er.code || '')) {
148-
throw er.code.substr(1) + ' ' + er.message
148+
throw er.code.slice(1) + ' ' + er.message
149149
} else {
150150
throw er.message
151151
}
@@ -211,7 +211,7 @@ class Doctor extends BaseCommand {
211211
const gid = process.getgid()
212212
const files = new Set([root])
213213
for (const f of files) {
214-
tracker.silly('checkFilesPermission', f.substr(root.length + 1))
214+
tracker.silly('checkFilesPermission', f.slice(root.length + 1))
215215
const st = await lstat(f).catch(er => {
216216
// if it can't be missing, or if it can and the error wasn't that it was missing
217217
if (!missingOk || er.code !== 'ENOENT') {

lib/commands/help-search.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ class HelpSearch extends BaseCommand {
171171
const finder = line.toLowerCase().split(arg.toLowerCase())
172172
let p = 0
173173
for (const f of finder) {
174-
hilitLine.push(line.substr(p, f.length))
175-
const word = line.substr(p + f.length, arg.length)
174+
hilitLine.push(line.slice(p, p + f.length))
175+
const word = line.slice(p + f.length, p + f.length + arg.length)
176176
const hilit = color.bgBlack(color.red(word))
177177
hilitLine.push(hilit)
178178
p += f.length + arg.length

lib/search/format-package-stream.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function addColorMarker (str, arg, i) {
111111

112112
if (arg.charAt(0) === '/') {
113113
return str.replace(
114-
new RegExp(arg.substr(1, arg.length - 2), 'gi'),
114+
new RegExp(arg.slice(1, -1), 'gi'),
115115
bit => markStart + bit + markEnd
116116
)
117117
}
@@ -121,9 +121,9 @@ function addColorMarker (str, arg, i) {
121121
var p = 0
122122

123123
return pieces.map(function (piece) {
124-
piece = str.substr(p, piece.length)
124+
piece = str.slice(p, p + piece.length)
125125
var mark = markStart +
126-
str.substr(p + piece.length, arg.length) +
126+
str.slice(p + piece.length, p + piece.length + arg.length) +
127127
markEnd
128128
p += piece.length + arg.length
129129
return piece + mark

lib/search/package-filter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function filterWords (data, include, exclude, opts) {
3636
function match (words, pattern) {
3737
if (pattern.charAt(0) === '/') {
3838
pattern = pattern.replace(/\/$/, '')
39-
pattern = new RegExp(pattern.substr(1, pattern.length - 1))
39+
pattern = new RegExp(pattern.slice(1))
4040
return words.match(pattern)
4141
}
4242
return words.indexOf(pattern) !== -1

lib/utils/config/definition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ const wrapAll = s => {
233233
return (
234234
'* ' +
235235
block
236-
.substr(1)
236+
.slice(1)
237237
.trim()
238238
.split('\n* ')
239239
.map(li => {

lib/utils/npm-usage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const wrap = (arr) => {
5353
out[l] = c
5454
}
5555
}
56-
return out.join('\n ').substr(2)
56+
return out.join('\n ').slice(2)
5757
}
5858

5959
const usages = async (npm) => {

lib/utils/tar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ const logTar = (tarball, opts = {}) => {
4848
{
4949
name: 'integrity:',
5050
value:
51-
tarball.integrity.toString().substr(0, 20) +
51+
tarball.integrity.toString().slice(0, 20) +
5252
'[...]' +
53-
tarball.integrity.toString().substr(80),
53+
tarball.integrity.toString().slice(80),
5454
},
5555
tarball.bundled.length && { name: 'bundled deps:', value: tarball.bundled.length },
5656
tarball.bundled.length && {

workspaces/arborist/lib/retire-path.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const pathSafeHash = s =>
77
.update(s)
88
.digest('base64')
99
.replace(/[^a-zA-Z0-9]+/g, '')
10-
.substr(0, 8)
10+
.slice(0, 8)
1111

1212
const retirePath = from => {
1313
const d = dirname(from)

workspaces/arborist/lib/shrinkwrap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ class Shrinkwrap {
807807
const pathFixed = !resolved ? null
808808
: !/^file:/.test(resolved) ? resolved
809809
// resolve onto the metadata path
810-
: `file:${resolve(this.path, resolved.substr(5))}`
810+
: `file:${resolve(this.path, resolved.slice(5))}`
811811

812812
// if we have one, only set the other if it matches
813813
// otherwise it could be for a completely different thing.
@@ -1056,7 +1056,7 @@ class Shrinkwrap {
10561056
// turn absolute file: paths into relative paths from the node
10571057
// this especially shows up with workspace edges when the root
10581058
// node is also a workspace in the set.
1059-
const p = resolve(node.realpath, spec.substr('file:'.length))
1059+
const p = resolve(node.realpath, spec.slice('file:'.length))
10601060
set[k] = `file:${relpath(node.realpath, p)}`
10611061
} else {
10621062
set[k] = spec

workspaces/arborist/lib/version-from-tgz.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = (name, tgz) => {
1616
// basename checking. Note that registries can
1717
// be mounted below the root url, so /a/b/-/x/y/foo/-/foo-1.2.3.tgz
1818
// is a potential option.
19-
const tfsplit = u.path.substr(1).split('/-/')
19+
const tfsplit = u.path.slice(1).split('/-/')
2020
if (tfsplit.length > 1) {
2121
const afterTF = tfsplit.pop()
2222
if (afterTF === base) {

workspaces/arborist/scripts/benchmark.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ Options:
6363
for (let i = 2; i < process.argv.length; i++) {
6464
const arg = process.argv[i]
6565
if (/^--previous=/.test(arg)) {
66-
options.previous = arg.substr('--previous='.length)
66+
options.previous = arg.slice('--previous='.length)
6767
} else if (/^--warn-range=[0-9]+/.test(arg)) {
68-
options.warnRange = +arg.substr('--warn-range='.length)
68+
options.warnRange = +arg.slice('--warn-range='.length)
6969
} else if (/^--cache=/.test(arg)) {
70-
options.cache = resolve(arg.substr('--cache='.length))
70+
options.cache = resolve(arg.slice('--cache='.length))
7171
} else if (/^--save=/.test(arg)) {
72-
const save = arg.substr('--save='.length)
72+
const save = arg.slice('--save='.length)
7373
if (/[/\\]|^\.\.?$/.test(save)) {
7474
throw new Error('save cannot have slashes or be . or ..')
7575
}

workspaces/arborist/test/arborist/load-actual.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const {
1414

1515
// strip the fixtures path off of the trees in snapshots
1616
const pp = path => path &&
17-
normalizePath(path).substr(normalizePath(fixtures).length + 1)
17+
normalizePath(path).slice(normalizePath(fixtures).length + 1)
1818
const defixture = obj => {
1919
if (obj instanceof Set) {
2020
return new Set([...obj].map(defixture))

workspaces/libnpmdiff/lib/should-print-patch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const shouldPrintPatch = (path, opts = {}) => {
1414
filename.startsWith('.')
1515
? filename
1616
: extname(filename)
17-
).substr(1)
17+
).slice(1)
1818

1919
return !binaryExtensions.includes(extension)
2020
}

workspaces/libnpmhook/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ cmd.add = (name, endpoint, secret, opts = {}) => {
1313
}
1414
if (name[0] === '~') {
1515
type = 'owner'
16-
name = name.substr(1)
16+
name = name.slice(1)
1717
}
1818
return fetch.json('/-/npm/v1/hooks/hook', {
1919
...opts,

workspaces/libnpmpublish/lib/unpublish.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const unpublish = async (spec, opts) => {
8282
...opts,
8383
query: { write: true },
8484
})
85-
const tarballUrl = new URL(dist.tarball).pathname.substr(1)
85+
const tarballUrl = new URL(dist.tarball).pathname.slice(1)
8686
await npmFetch(`${tarballUrl}/-rev/${_rev}`, {
8787
...opts,
8888
method: 'DELETE',

0 commit comments

Comments
 (0)