Skip to content

Commit 494fa54

Browse files
aduh95targos
andcommittedJun 1, 2024
test: replace .substr with .slice
`String.prototype.substr` is deprecated, and using it will raise an error when using ESLint 9+. Co-authored-by: =?UTF-8?q?Micha=C3=ABl=20Zasso?= <targos@protonmail.com> PR-URL: #53070 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Tierney Cyren <hello@bnb.im> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 4a2c6ff commit 494fa54

13 files changed

+22
-22
lines changed
 

‎test/es-module/test-esm-exports.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ const { requireFromInside, importFromInside } = fromInside;
231231
});
232232

233233
function assertStartsWith(actual, expected) {
234-
const start = actual.toString().substr(0, expected.length);
234+
const start = actual.toString().slice(0, expected.length);
235235
strictEqual(start, expected);
236236
}
237237

‎test/es-module/test-esm-imports.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ requireFixture('#cjs').then(mustCall((actual) => {
124124
}));
125125

126126
function assertStartsWith(actual, expected) {
127-
const start = actual.toString().substr(0, expected.length);
127+
const start = actual.toString().slice(0, expected.length);
128128
strictEqual(start, expected);
129129
}
130130

‎test/parallel/test-crypto-authenticated.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ for (const test of TEST_CASES) {
212212
cipher.update('01234567', 'hex');
213213
cipher.final();
214214
const tag = cipher.getAuthTag();
215-
assert.strictEqual(tag.toString('hex'), fullTag.substr(0, 2 * e));
215+
assert.strictEqual(tag.toString('hex'), fullTag.slice(0, 2 * e));
216216
}
217217
}
218218

‎test/parallel/test-crypto-hmac.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ for (let i = 0, l = rfc4231.length; i < l; i++) {
272272
.update(rfc4231[i].data)
273273
.digest('hex');
274274
if (rfc4231[i].truncate) {
275-
actual = actual.substr(0, 32); // first 128 bits == 32 hex chars
276-
strRes = strRes.substr(0, 32);
275+
actual = actual.slice(0, 32); // first 128 bits == 32 hex chars
276+
strRes = strRes.slice(0, 32);
277277
}
278278
const expected = rfc4231[i].hmac[hash];
279279
assert.strictEqual(

‎test/parallel/test-crypto-randomuuid.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ for (let n = 0; n < 130; n++) {
3434

3535
// Check that version 4 identifier was populated.
3636
assert.strictEqual(
37-
Buffer.from(uuid.substr(14, 2), 'hex')[0] & 0x40, 0x40);
37+
Buffer.from(uuid.slice(14, 16), 'hex')[0] & 0x40, 0x40);
3838

3939
// Check that clock_seq_hi_and_reserved was populated with reserved bits.
4040
assert.strictEqual(
41-
Buffer.from(uuid.substr(19, 2), 'hex')[0] & 0b1100_0000, 0b1000_0000);
41+
Buffer.from(uuid.slice(19, 21), 'hex')[0] & 0b1100_0000, 0b1000_0000);
4242
}
4343

4444
// Test non-buffered UUID's

‎test/parallel/test-fs-realpath.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ let root = '/';
4242
let assertEqualPath = assert.strictEqual;
4343
if (common.isWindows) {
4444
// Something like "C:\\"
45-
root = process.cwd().substr(0, 3);
45+
root = process.cwd().slice(0, 3);
4646
assertEqualPath = function(path_left, path_right, message) {
4747
assert
4848
.strictEqual(path_left.toLowerCase(), path_right.toLowerCase(), message);
@@ -367,7 +367,7 @@ function test_deep_symlink_mix(realpath, realpathSync, callback) {
367367
function test_non_symlinks(realpath, realpathSync, callback) {
368368
console.log('test_non_symlinks');
369369
const entrydir = path.dirname(tmpAbsDir);
370-
const entry = `${tmpAbsDir.substr(entrydir.length + 1)}/cycles/root.js`;
370+
const entry = `${tmpAbsDir.slice(entrydir.length + 1)}/cycles/root.js`;
371371
const expected = `${tmpAbsDir}/cycles/root.js`;
372372
const origcwd = process.cwd();
373373
process.chdir(entrydir);

‎test/parallel/test-http-content-length.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const server = http.createServer(function(req, res) {
2626
res.removeHeader('Date');
2727
res.setHeader('Keep-Alive', 'timeout=1');
2828

29-
switch (req.url.substr(1)) {
29+
switch (req.url.slice(1)) {
3030
case 'multiple-writes':
3131
delete req.headers.host;
3232
assert.deepStrictEqual(req.headers, expectedHeadersMultipleWrites);

‎test/parallel/test-http-upgrade-server.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function test_upgrade_with_listener() {
100100
assert.strictEqual(typeof data, 'string');
101101

102102
if (state === 1) {
103-
assert.strictEqual(data.substr(0, 12), 'HTTP/1.1 101');
103+
assert.strictEqual(data.slice(0, 12), 'HTTP/1.1 101');
104104
assert.strictEqual(request_upgradeHead.toString('utf8'), 'WjN}|M(6');
105105
conn.write('test', 'utf8');
106106
} else if (state === 2) {
@@ -133,7 +133,7 @@ function test_upgrade_no_listener() {
133133

134134
conn.once('data', (data) => {
135135
assert.strictEqual(typeof data, 'string');
136-
assert.strictEqual(data.substr(0, 12), 'HTTP/1.1 200');
136+
assert.strictEqual(data.slice(0, 12), 'HTTP/1.1 200');
137137
conn.end();
138138
});
139139

@@ -153,7 +153,7 @@ function test_standard_http() {
153153

154154
conn.once('data', function(data) {
155155
assert.strictEqual(typeof data, 'string');
156-
assert.strictEqual(data.substr(0, 12), 'HTTP/1.1 200');
156+
assert.strictEqual(data.slice(0, 12), 'HTTP/1.1 200');
157157
conn.end();
158158
});
159159

‎test/parallel/test-path-dirname.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common');
33
const assert = require('assert');
44
const path = require('path');
55

6-
assert.strictEqual(path.dirname(__filename).substr(-13),
6+
assert.strictEqual(path.dirname(__filename).slice(-13),
77
common.isWindows ? 'test\\parallel' : 'test/parallel');
88

99
assert.strictEqual(path.posix.dirname('/a/b/'), '/a');

‎test/parallel/test-repl-tab-complete.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ testMe.complete('obj.', common.mustCall(function(error, data) {
584584

585585
data[0].forEach((key) => {
586586
if (!key || key === 'ele.biu') return;
587-
assert.notStrictEqual(ele[key.substr(4)], undefined);
587+
assert.notStrictEqual(ele[key.slice(4)], undefined);
588588
});
589589
}));
590590
});

‎test/parallel/test-repl.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async function runReplTests(socket, prompt, tests) {
6464

6565
// Cut away the initial prompt
6666
while (lineBuffer.startsWith(prompt))
67-
lineBuffer = lineBuffer.substr(prompt.length);
67+
lineBuffer = lineBuffer.slice(prompt.length);
6868

6969
// Allow to match partial text if no newline was received, because
7070
// sending newlines from the REPL itself would be redundant
@@ -76,13 +76,13 @@ async function runReplTests(socket, prompt, tests) {
7676

7777
// Split off the current line.
7878
const newlineOffset = lineBuffer.indexOf('\n');
79-
let actualLine = lineBuffer.substr(0, newlineOffset);
80-
lineBuffer = lineBuffer.substr(newlineOffset + 1);
79+
let actualLine = lineBuffer.slice(0, newlineOffset);
80+
lineBuffer = lineBuffer.slice(newlineOffset + 1);
8181

8282
// This might have been skipped in the loop above because the buffer
8383
// already contained a \n to begin with and the entire loop was skipped.
8484
while (actualLine.startsWith(prompt))
85-
actualLine = actualLine.substr(prompt.length);
85+
actualLine = actualLine.slice(prompt.length);
8686

8787
console.error('in:', JSON.stringify(actualLine));
8888

‎test/parallel/test-webcrypto-digest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ async function testDigest(size, name) {
156156
Object.keys(kDigestedData).forEach((alg) => {
157157
const upCase = alg.toUpperCase();
158158
const downCase = alg.toLowerCase();
159-
const mixedCase = upCase.substr(0, 1) + downCase.substr(1);
159+
const mixedCase = upCase.slice(0, 1) + downCase.slice(1);
160160

161161
variations.push(testDigest(size, upCase));
162162
variations.push(testDigest(size, downCase));

‎test/parallel/test-zlib-truncated.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ const errMessage = /unexpected end of file/;
5252

5353
// Sync truncated input test, finishFlush = Z_SYNC_FLUSH
5454
const result = toUTF8(zlib[methods.decompSync](truncated, syncFlushOpt));
55-
assert.strictEqual(result, inputString.substr(0, result.length));
55+
assert.strictEqual(result, inputString.slice(0, result.length));
5656

5757
// Async truncated input test, finishFlush = Z_SYNC_FLUSH
5858
zlib[methods.decomp](truncated, syncFlushOpt, function(err, decompressed) {
5959
assert.ifError(err);
6060
const result = toUTF8(decompressed);
61-
assert.strictEqual(result, inputString.substr(0, result.length));
61+
assert.strictEqual(result, inputString.slice(0, result.length));
6262
});
6363
});
6464
});

0 commit comments

Comments
 (0)
Please sign in to comment.