Skip to content

Commit 24e5e32

Browse files
committed
Refactor: substr() is deprecated, using slice() instead
- `substr()` is not part of the core JS since ~2018 - No wonder why no one noticed this :) - Though its supported by modern browsers, its deprecated - Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr Why `slice()` and not `substring()`? > Beacuse I like pizza ;) jk The reason, that I'm not using the most obvious alternative substring() is; it swaps the args passed, when; `startIndex > endIndex`, which I think is not a property of good fn and also does not reflects in it's name, and could lead to more bugs in most of the cases also, it doesn't support negative args, which I think reduces flexibility to work with it. - Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring#differences_between_substring_and_slice - `slice`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice - `substring`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
1 parent 243a148 commit 24e5e32

File tree

13 files changed

+20
-23
lines changed

13 files changed

+20
-23
lines changed

packages/assets/path-support.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function getAndroidResourceIdentifier(asset: PackagerAsset): string {
8282

8383
function getBasePath(asset: PackagerAsset): string {
8484
const basePath = asset.httpServerLocation;
85-
return basePath.startsWith('/') ? basePath.substr(1) : basePath;
85+
return basePath.startsWith('/') ? basePath.slice(1) : basePath;
8686
}
8787

8888
module.exports = {

packages/hermes-inspector-msggen/src/Converters.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
*/
1010

1111
export function toCppNamespace(domain: string): string {
12-
return domain.substr(0, 1).toLowerCase() + domain.substr(1);
12+
return domain.slice(0, 1).toLowerCase() + domain.slice(1);
1313
}
1414

1515
export function toCppType(type: string): string {
16-
return type.substr(0, 1).toUpperCase() + type.substr(1);
16+
return type.slice(0, 1).toUpperCase() + type.slice(1);
1717
}
1818

1919
export type JsTypeString =

packages/hermes-inspector-msggen/src/Property.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ function toDomainAndId(
114114
domain = curDomain;
115115
id = absOrRelRef;
116116
} else {
117-
domain = absOrRelRef.substr(0, i);
118-
id = absOrRelRef.substr(i + 1);
117+
domain = absOrRelRef.slice(0, i);
118+
id = absOrRelRef.slice(i + 1);
119119
}
120120

121121
return [domain, id];

packages/polyfills/console.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ const inspect = (function () {
252252
return ' ' + line;
253253
})
254254
.join('\n')
255-
.substr(2);
255+
.slice(2);
256256
} else {
257257
str =
258258
'\n' +
@@ -274,7 +274,7 @@ const inspect = (function () {
274274
}
275275
name = JSON.stringify('' + key);
276276
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
277-
name = name.substr(1, name.length - 2);
277+
name = name.slice(1, name.length - 2);
278278
name = ctx.stylize(name, 'name');
279279
} else {
280280
name = name

packages/react-native/Libraries/DOM/Nodes/ReadOnlyCharacterData.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ export default class ReadOnlyCharacterData extends ReadOnlyNode {
6767
);
6868
}
6969
let adjustedCount = count < 0 || count > data.length ? data.length : count;
70-
return data.substr(offset, adjustedCount);
70+
return data.slice(offset, offset + adjustedCount);
7171
}
7272
}

packages/react-native/Libraries/Inspector/NetworkOverlay.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function getStringByValue(value: any): string {
6464
}
6565
if (typeof value === 'string' && value.length > 500) {
6666
return String(value)
67-
.substr(0, 500)
67+
.slice(0, 500)
6868
.concat('\n***TRUNCATED TO 500 CHARACTERS***');
6969
}
7070
return value;

packages/react-native/Libraries/LogBox/UI/AnsiHighlight.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default function Ansi({
7373
return content.replace(/\| $/, ' ');
7474
} else if (key === 2 && commonWhitespaceLength < Infinity) {
7575
// Remove common whitespace at the beginning of the line
76-
return content.substr(commonWhitespaceLength);
76+
return content.slice(commonWhitespaceLength);
7777
} else {
7878
return content;
7979
}

packages/react-native/Libraries/LogBox/UI/LogBoxMessage.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,22 @@ function LogBoxMessage(props: Props): React.Node {
138138
const key = String(index);
139139

140140
if (substitution.offset > prevOffset) {
141-
const prevPart = content.substr(
142-
prevOffset,
143-
substitution.offset - prevOffset,
144-
);
141+
const prevPart = content.slice(prevOffset, substitution.offset);
145142

146143
createUnderLength(key, prevPart);
147144
}
148145

149-
const substitutionPart = content.substr(
146+
const substitutionPart = content.slice(
150147
substitution.offset,
151-
substitution.length,
148+
substitution.offset + substitution.length,
152149
);
153150

154151
createUnderLength(key + '.5', substitutionPart, substitutionStyle);
155152
return substitution.offset + substitution.length;
156153
}, 0);
157154

158155
if (lastOffset < content.length) {
159-
const lastPart = content.substr(lastOffset);
156+
const lastPart = content.slice(lastOffset);
160157
createUnderLength('-1', lastPart);
161158
}
162159

packages/react-native/Libraries/Utilities/PolyfillFunctions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function polyfillObjectProperty<T>(
3232
): void {
3333
const descriptor = Object.getOwnPropertyDescriptor<$FlowFixMe>(object, name);
3434
if (__DEV__ && descriptor) {
35-
const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
35+
const backupName = `original${name[0].toUpperCase()}${name.slice(1)}`;
3636
Object.defineProperty(object, backupName, descriptor);
3737
}
3838

packages/react-native/ReactAndroid/src/androidTest/js/TextInputTestModule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class TokenizedTextExample extends React.Component {
4747
if (token[0].length === 0) {
4848
index = 1;
4949
}
50-
parts.push(_text.substr(0, index));
50+
parts.push(_text.slice(0, index));
5151
parts.push(token[0]);
5252
index = index + token[0].length;
5353
_text = _text.slice(index);

0 commit comments

Comments
 (0)