Skip to content

Commit 4ed3123

Browse files
fauzan171trekhleb
authored andcommitted
refactor: replace deprecated String.prototype.substr() with substring()
String.prototype.substr() is deprecated and may be removed in future JavaScript engine versions (Annex B of the ECMAScript specification). Replaced all occurrences in the codebase with String.prototype.substring(): - src/algorithms/string/rabin-karp/rabinKarp.js - src/algorithms/cryptography/polynomial-hash/__test__/SimplePolynomialHash.test.js - src/algorithms/cryptography/polynomial-hash/__test__/PolynomialHash.test.js The behavior is identical since all substr(start, length) calls are converted to substring(start, start + length) with the same arguments. All existing tests pass.
1 parent 95ead35 commit 4ed3123

3 files changed

Lines changed: 5 additions & 5 deletions

File tree

src/algorithms/cryptography/polynomial-hash/__test__/PolynomialHash.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ describe('PolynomialHash', () => {
2121

2222
// Check hashing for different word lengths.
2323
frameSizes.forEach((frameSize) => {
24-
let previousWord = text.substr(0, frameSize);
24+
let previousWord = text.substring(0, frameSize);
2525
let previousHash = polynomialHash.hash(previousWord);
2626

2727
// Shift frame through the whole text.
2828
for (let frameShift = 1; frameShift < (text.length - frameSize); frameShift += 1) {
29-
const currentWord = text.substr(frameShift, frameSize);
29+
const currentWord = text.substring(frameShift, frameShift + frameSize);
3030
const currentHash = polynomialHash.hash(currentWord);
3131
const currentRollingHash = polynomialHash.roll(previousHash, previousWord, currentWord);
3232

src/algorithms/cryptography/polynomial-hash/__test__/SimplePolynomialHash.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ describe('PolynomialHash', () => {
1818

1919
// Check hashing for different word lengths.
2020
frameSizes.forEach((frameSize) => {
21-
let previousWord = text.substr(0, frameSize);
21+
let previousWord = text.substring(0, frameSize);
2222
let previousHash = polynomialHash.hash(previousWord);
2323

2424
// Shift frame through the whole text.
2525
for (let frameShift = 1; frameShift < (text.length - frameSize); frameShift += 1) {
26-
const currentWord = text.substr(frameShift, frameSize);
26+
const currentWord = text.substring(frameShift, frameShift + frameSize);
2727
const currentHash = polynomialHash.hash(currentWord);
2828
const currentRollingHash = polynomialHash.roll(previousHash, previousWord, currentWord);
2929

src/algorithms/string/rabin-karp/rabinKarp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default function rabinKarp(text, word) {
3232
// In case of hash collision the strings may not be equal.
3333
if (
3434
wordHash === currentFrameHash
35-
&& text.substr(charIndex, word.length) === word
35+
&& text.substring(charIndex, charIndex + word.length) === word
3636
) {
3737
return charIndex;
3838
}

0 commit comments

Comments
 (0)