Skip to content

Commit 2662401

Browse files
authored
ref: Replace string.substr with string.slice (#6760)
`substr` is actually deprecated (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr). For our cases, `slice()` should work just as well - see this very informative answer: https://stackoverflow.com/a/31910656
1 parent 93b8ec5 commit 2662401

File tree

5 files changed

+15
-15
lines changed

5 files changed

+15
-15
lines changed

packages/node/src/module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ export function getModule(filename: string | undefined): string | undefined {
2727
let n = path.lastIndexOf('/node_modules/');
2828
if (n > -1) {
2929
// /node_modules/ is 14 chars
30-
return `${path.substr(n + 14).replace(/\//g, '.')}:${file}`;
30+
return `${path.slice(n + 14).replace(/\//g, '.')}:${file}`;
3131
}
3232
// Let's see if it's a part of the main module
3333
// To be a part of main module, it has to share the same base
3434
n = `${path}/`.lastIndexOf(base, 0);
3535

3636
if (n === 0) {
37-
let moduleName = path.substr(base.length).replace(/\//g, '.');
37+
let moduleName = path.slice(base.length).replace(/\//g, '.');
3838
if (moduleName) {
3939
moduleName += ':';
4040
}

packages/utils/src/path.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ function trim(arr: string[]): string[] {
9595
/** JSDoc */
9696
export function relative(from: string, to: string): string {
9797
/* eslint-disable no-param-reassign */
98-
from = resolve(from).substr(1);
99-
to = resolve(to).substr(1);
98+
from = resolve(from).slice(1);
99+
to = resolve(to).slice(1);
100100
/* eslint-enable no-param-reassign */
101101

102102
const fromParts = trim(from.split('/'));
@@ -126,7 +126,7 @@ export function relative(from: string, to: string): string {
126126
/** JSDoc */
127127
export function normalizePath(path: string): string {
128128
const isPathAbsolute = isAbsolute(path);
129-
const trailingSlash = path.substr(-1) === '/';
129+
const trailingSlash = path.slice(-1) === '/';
130130

131131
// Normalize the path
132132
let normalizedPath = normalizeArray(
@@ -169,7 +169,7 @@ export function dirname(path: string): string {
169169

170170
if (dir) {
171171
// It has a dirname, strip trailing slash
172-
dir = dir.substr(0, dir.length - 1);
172+
dir = dir.slice(0, dir.length - 1);
173173
}
174174

175175
return root + dir;
@@ -178,8 +178,8 @@ export function dirname(path: string): string {
178178
/** JSDoc */
179179
export function basename(path: string, ext?: string): string {
180180
let f = splitPath(path)[2];
181-
if (ext && f.substr(ext.length * -1) === ext) {
182-
f = f.substr(0, f.length - ext.length);
181+
if (ext && f.slice(ext.length * -1) === ext) {
182+
f = f.slice(0, f.length - ext.length);
183183
}
184184
return f;
185185
}

packages/utils/src/stacktrace.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ function node(getModule?: GetModuleFn): StackLineParserFn {
142142
}
143143

144144
if (methodStart > 0) {
145-
object = functionName.substr(0, methodStart);
146-
method = functionName.substr(methodStart + 1);
145+
object = functionName.slice(0, methodStart);
146+
method = functionName.slice(methodStart + 1);
147147
const objectEnd = object.indexOf('.Module');
148148
if (objectEnd > 0) {
149-
functionName = functionName.substr(objectEnd + 1);
150-
object = object.substr(0, objectEnd);
149+
functionName = functionName.slice(objectEnd + 1);
150+
object = object.slice(0, objectEnd);
151151
}
152152
}
153153
typeName = undefined;
@@ -168,7 +168,7 @@ function node(getModule?: GetModuleFn): StackLineParserFn {
168168
functionName = typeName ? `${typeName}.${methodName}` : methodName;
169169
}
170170

171-
const filename = lineMatch[2]?.startsWith('file://') ? lineMatch[2].substr(7) : lineMatch[2];
171+
const filename = lineMatch[2]?.startsWith('file://') ? lineMatch[2].slice(7) : lineMatch[2];
172172
const isNative = lineMatch[5] === 'native';
173173
const isInternal =
174174
isNative || (filename && !filename.startsWith('/') && !filename.startsWith('.') && filename.indexOf(':\\') !== 1);

packages/utils/src/string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function truncate(str: string, max: number = 0): string {
1111
if (typeof str !== 'string' || max === 0) {
1212
return str;
1313
}
14-
return str.length <= max ? str : `${str.substr(0, max)}...`;
14+
return str.length <= max ? str : `${str.slice(0, max)}...`;
1515
}
1616

1717
/**

packages/wasm/src/registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function registerModule(module: WebAssembly.Module, url: string): void {
5050
code_id: buildId,
5151
code_file: url,
5252
debug_file: debugFile ? new URL(debugFile, url).href : null,
53-
debug_id: `${buildId.padEnd(32, '0').substr(0, 32)}0`,
53+
debug_id: `${buildId.padEnd(32, '0').slice(0, 32)}0`,
5454
});
5555
}
5656
}

0 commit comments

Comments
 (0)