mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
refactor: replace String.prototype.substr()
with String.prototype.slice()
(#2993)
This commit is contained in:
parent
6bd9332860
commit
c78061eef3
@ -87,7 +87,7 @@ function clean(length: number): Uint8Array {
|
||||
|
||||
function pad(num: number, bytes: number, base = 8): string {
|
||||
const numString = num.toString(base);
|
||||
return "000000000000".substr(numString.length + 12 - bytes) + numString;
|
||||
return "000000000000".slice(numString.length + 12 - bytes) + numString;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -188,8 +188,8 @@ export class Tar {
|
||||
while (i >= 0) {
|
||||
i = fileName.lastIndexOf("/", i);
|
||||
if (i <= 155) {
|
||||
fileNamePrefix = fileName.substr(0, i);
|
||||
fileName = fileName.substr(i + 1);
|
||||
fileNamePrefix = fileName.slice(0, i);
|
||||
fileName = fileName.slice(i + 1);
|
||||
break;
|
||||
}
|
||||
i--;
|
||||
|
@ -93,7 +93,7 @@ Deno.test({
|
||||
"ad28": "bar",
|
||||
"100f": "dino",
|
||||
},
|
||||
(it) => it.substr(0, 2),
|
||||
(it) => it.slice(0, 2),
|
||||
],
|
||||
{
|
||||
"ad": "bar",
|
||||
@ -114,7 +114,7 @@ Deno.test({
|
||||
"bcd": 33,
|
||||
"d": 11,
|
||||
},
|
||||
(key) => key.substr(1),
|
||||
(key) => key.slice(1),
|
||||
],
|
||||
{
|
||||
"b": 22,
|
||||
|
@ -85,7 +85,7 @@ Deno.test({
|
||||
"FoodFile": "/home/deno/food.txt",
|
||||
"CalendarFile": "/home/deno/weekend.cal",
|
||||
},
|
||||
(path) => path.substr(path.lastIndexOf(".")),
|
||||
(path) => path.slice(path.lastIndexOf(".")),
|
||||
],
|
||||
{
|
||||
"FoodFile": ".txt",
|
||||
|
@ -654,11 +654,11 @@ class Printf {
|
||||
let round = false;
|
||||
if (fractional.length > precision) {
|
||||
fractional = "1" + fractional; // prepend a 1 in case of leading 0
|
||||
let tmp = parseInt(fractional.substr(0, precision + 2)) / 10;
|
||||
let tmp = parseInt(fractional.slice(0, precision + 2)) / 10;
|
||||
tmp = Math.round(tmp);
|
||||
fractional = Math.floor(tmp).toString();
|
||||
round = fractional[0] === "2";
|
||||
fractional = fractional.substr(1); // remove extra 1
|
||||
fractional = fractional.slice(1); // remove extra 1
|
||||
} else {
|
||||
while (fractional.length < precision) {
|
||||
fractional += "0";
|
||||
@ -741,7 +741,7 @@ class Printf {
|
||||
while (m.length < splIdx) {
|
||||
m += "0";
|
||||
}
|
||||
return m.substr(0, splIdx) + "." + m.substr(splIdx);
|
||||
return m.slice(0, splIdx) + "." + m.slice(splIdx);
|
||||
}
|
||||
}
|
||||
// avoiding sign makes padding easier
|
||||
@ -829,7 +829,7 @@ class Printf {
|
||||
*/
|
||||
fmtString(s: string): string {
|
||||
if (this.flags.precision !== -1) {
|
||||
s = s.substr(0, this.flags.precision);
|
||||
s = s.slice(0, this.flags.precision);
|
||||
}
|
||||
return this.pad(s);
|
||||
}
|
||||
@ -883,7 +883,7 @@ class Printf {
|
||||
return this.pad(Deno.inspect(val, options));
|
||||
} else {
|
||||
const p = this.flags.precision;
|
||||
return p === -1 ? val.toString() : val.toString().substr(0, p);
|
||||
return p === -1 ? val.toString() : val.toString().slice(0, p);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -109,7 +109,7 @@ function parseMediaType(
|
||||
|
||||
for (const [key, val] of kvps) {
|
||||
const value = val && val[0] === `"` && val[val.length - 1] === `"`
|
||||
? val.substr(1, val.length - 2)
|
||||
? val.slice(1, val.length - 1)
|
||||
: val;
|
||||
|
||||
if (key === "q" && value) {
|
||||
|
@ -344,7 +344,7 @@ function calculateServerName(options, req) {
|
||||
// Leading '[', but no ']'. Need to do something...
|
||||
servername = hostHeader;
|
||||
} else {
|
||||
servername = hostHeader.substr(1, index - 1);
|
||||
servername = hostHeader.slice(1, index);
|
||||
}
|
||||
} else {
|
||||
servername = hostHeader.split(":", 1)[0];
|
||||
|
@ -284,8 +284,8 @@ for (let i = 0, l = rfc4231.length; i < l; i++) {
|
||||
.update(rfc4231[i].data)
|
||||
.digest('hex');
|
||||
if (rfc4231[i].truncate) {
|
||||
actual = actual.substr(0, 32); // first 128 bits == 32 hex chars
|
||||
strRes = strRes.substr(0, 32);
|
||||
actual = actual.slice(0, 32); // first 128 bits == 32 hex chars
|
||||
strRes = strRes.slice(0, 32);
|
||||
}
|
||||
const expected = rfc4231[i].hmac[hash];
|
||||
assert.strictEqual(
|
||||
|
@ -10,7 +10,7 @@ const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const path = require('path');
|
||||
|
||||
assert.strictEqual(path.dirname(__filename).substr(-13),
|
||||
assert.strictEqual(path.dirname(__filename).slice(-13),
|
||||
common.isWindows ? 'test\\parallel' : 'test/parallel');
|
||||
|
||||
assert.strictEqual(path.posix.dirname('/a/b/'), '/a');
|
||||
|
@ -59,13 +59,13 @@ const errMessage = /unexpected end of file/;
|
||||
|
||||
// Sync truncated input test, finishFlush = Z_SYNC_FLUSH
|
||||
const result = toUTF8(zlib[methods.decompSync](truncated, syncFlushOpt));
|
||||
assert.strictEqual(result, inputString.substr(0, result.length));
|
||||
assert.strictEqual(result, inputString.slice(0, result.length));
|
||||
|
||||
// Async truncated input test, finishFlush = Z_SYNC_FLUSH
|
||||
zlib[methods.decomp](truncated, syncFlushOpt, function(err, decompressed) {
|
||||
assert.ifError(err);
|
||||
const result = toUTF8(decompressed);
|
||||
assert.strictEqual(result, inputString.substr(0, result.length));
|
||||
assert.strictEqual(result, inputString.slice(0, result.length));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -436,7 +436,7 @@ export class Url {
|
||||
srcPath.unshift("");
|
||||
}
|
||||
|
||||
if (hasTrailingSlash && srcPath.join("/").substr(-1) !== "/") {
|
||||
if (hasTrailingSlash && srcPath.join("/").slice(-1) !== "/") {
|
||||
srcPath.push("");
|
||||
}
|
||||
|
||||
|
@ -156,7 +156,7 @@ Deno.test("Can use fast-check to property test indexOf function", async (t) => {
|
||||
const pattern = b;
|
||||
const index = indexOf(text, pattern);
|
||||
return index === -1 ||
|
||||
text.substr(index, pattern.length) === pattern;
|
||||
text.slice(index, index + pattern.length) === pattern;
|
||||
},
|
||||
),
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user