Skip to content

Commit 2becc65

Browse files
committed
refactor: replace deprecated String.substr with String.slice
Fixes #6078 String.prototype.substr is deprecated and should be replaced with String.prototype.slice or String.prototype.substring. This commit replaces all occurrences of .substr() with .slice() across the codebase. The slice method is preferred as it handles negative indices more intuitively and matches the existing usage patterns.
1 parent f07f1c7 commit 2becc65

34 files changed

+86
-86
lines changed

src/components/calendars/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ function worldCalFmt(fmt, x, calendar) {
143143
// format the cDate according to the translated directive
144144
else replacementPart = cDate.formatDate(directiveObj[modifier]);
145145

146-
fmt = fmt.substr(0, i) + replacementPart + fmt.substr(i + directiveLen);
146+
fmt = fmt.slice(0, i) + replacementPart + fmt.slice(i + directiveLen);
147147
i += replacementPart.length;
148148
}
149149
}

src/components/color/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ color.clean = function(container) {
117117
key = keys[i];
118118
val = container[key];
119119

120-
if(key.substr(key.length - 5) === 'color') {
120+
if(key.slice(-5) === 'color') {
121121
// only sanitize keys that end in "color" or "colorscale"
122122

123123
if(Array.isArray(val)) {
124124
for(j = 0; j < val.length; j++) val[j] = cleanOne(val[j]);
125125
} else container[key] = cleanOne(val);
126-
} else if(key.substr(key.length - 10) === 'colorscale' && Array.isArray(val)) {
126+
} else if(key.slice(-10) === 'colorscale' && Array.isArray(val)) {
127127
// colorscales have the format [[0, color1], [frac, color2], ... [1, colorN]]
128128

129129
for(j = 0; j < val.length; j++) {
@@ -144,7 +144,7 @@ function cleanOne(val) {
144144
if(isNumeric(val) || typeof val !== 'string') return val;
145145

146146
var valTrim = val.trim();
147-
if(valTrim.substr(0, 3) !== 'rgb') return val;
147+
if(valTrim.slice(0, 3) !== 'rgb') return val;
148148

149149
var match = valTrim.match(/^rgba?\s*\(([^()]*)\)$/);
150150
if(!match) return val;

src/components/colorbar/draw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ function drawColorBar(g, opts, gd) {
338338
// wrong class (in case earlier the colorbar was drawn on
339339
// a different side, I think?)
340340
var otherClass = titleClass.charAt(0) === 'h' ?
341-
titleClass.substr(1) :
341+
titleClass.slice(1) :
342342
'h' + titleClass;
343343
g.selectAll('.' + otherClass + ',.' + otherClass + '-math-group').remove();
344344

src/components/rangeslider/defaults.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ module.exports = function handleDefaults(layoutIn, layoutOut, axName) {
4545
if(subplots) {
4646
var yIds = subplots.cartesian
4747
.filter(function(subplotId) {
48-
return subplotId.substr(0, subplotId.indexOf('y')) === axisIds.name2id(axName);
48+
return subplotId.slice(0, subplotId.indexOf('y')) === axisIds.name2id(axName);
4949
})
5050
.map(function(subplotId) {
51-
return subplotId.substr(subplotId.indexOf('y'), subplotId.length);
51+
return subplotId.slice(subplotId.indexOf('y'), subplotId.length);
5252
});
5353
var yNames = Lib.simpleMap(yIds, axisIds.id2name);
5454
for(var i = 0; i < yNames.length; i++) {

src/components/shapes/calc_autorange.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function shapeBounds(ax, shape, paramsToUse) {
115115
drawnParam = paramsToUse[segment.charAt(0)].drawn;
116116
if(drawnParam === undefined) continue;
117117

118-
params = segments[i].substr(1).match(constants.paramRE);
118+
params = segments[i].slice(1).match(constants.paramRE);
119119
if(!params || params.length < drawnParam) continue;
120120

121121
val = convertVal(params[drawnParam]);

src/components/shapes/draw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ function movePath(pathIn, moveX, moveY) {
620620
var yParams = constants.paramIsY[segmentType];
621621
var nParams = constants.numParams[segmentType];
622622

623-
var paramString = segment.substr(1).replace(constants.paramRE, function(param) {
623+
var paramString = segment.slice(1).replace(constants.paramRE, function(param) {
624624
if(paramNumber >= nParams) return param;
625625

626626
if(xParams[paramNumber]) param = moveX(param);

src/components/shapes/helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ exports.extractPathCoords = function(path, paramsToUse, isRaw) {
4141
var relevantParamIdx = paramsToUse[segment.charAt(0)].drawn;
4242
if(relevantParamIdx === undefined) return;
4343

44-
var params = segment.substr(1).match(constants.paramRE);
44+
var params = segment.slice(1).match(constants.paramRE);
4545
if(!params || params.length < relevantParamIdx) return;
4646

4747
var str = params[relevantParamIdx];
@@ -261,7 +261,7 @@ function convertPath(options, x2p, y2p) {
261261
var yParams = constants.paramIsY[segmentType];
262262
var nParams = constants.numParams[segmentType];
263263

264-
var paramString = segment.substr(1).replace(constants.paramRE, function(param) {
264+
var paramString = segment.slice(1).replace(constants.paramRE, function(param) {
265265
if(xParams[paramNumber]) {
266266
if(xSizemode === 'pixel') param = x2p(xAnchor) + Number(param);
267267
else param = x2p(param);

src/lib/coerce.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ exports.valObjectMeta = {
7676
var k = String(values[i]);
7777

7878
if((k.charAt(0) === '/' && k.charAt(k.length - 1) === '/')) {
79-
var regex = new RegExp(k.substr(1, k.length - 2));
79+
var regex = new RegExp(k.slice(1, -1));
8080
if(regex.test(v)) return true;
8181
} else if(v === values[i]) return true;
8282
}

src/lib/dates.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ exports.dateTime2ms = function(s, calendar) {
166166
// 'G' as a prefix to force the built-in gregorian calendar.
167167
var s0 = s.charAt(0);
168168
if(isWorld && (s0 === 'G' || s0 === 'g')) {
169-
s = s.substr(1);
169+
s = s.slice(1);
170170
calendar = '';
171171
}
172172

173-
var isChinese = isWorld && calendar.substr(0, 7) === 'chinese';
173+
var isChinese = isWorld && calendar.slice(0, 7) === 'chinese';
174174

175175
var match = s.match(isChinese ? DATETIME_REGEXP_CN : DATETIME_REGEXP);
176176
if(!match) return BADNUM;
@@ -234,7 +234,7 @@ exports.isDateTime = function(s, calendar) {
234234

235235
// pad a number with zeroes, to given # of digits before the decimal point
236236
function lpad(val, digits) {
237-
return String(val + Math.pow(10, digits)).substr(1);
237+
return String(val + Math.pow(10, digits)).slice(1);
238238
}
239239

240240
/**
@@ -272,7 +272,7 @@ exports.ms2DateTime = function(ms, r, calendar) {
272272
// other things for a few calendars, so we can't trust it. Just pad
273273
// it manually (after the '-' if there is one)
274274
if(dateStr.charAt(0) === '-') {
275-
while(dateStr.length < 11) dateStr = '-0' + dateStr.substr(1);
275+
while(dateStr.length < 11) dateStr = '-0' + dateStr.slice(1);
276276
} else {
277277
while(dateStr.length < 10) dateStr = '0' + dateStr;
278278
}
@@ -388,7 +388,7 @@ function modDateFormat(fmt, x, formatter, calendar) {
388388
var digits = Math.min(+(match.charAt(1)) || 6, 6);
389389
var fracSecs = ((x / 1000 % 1) + 2)
390390
.toFixed(digits)
391-
.substr(2).replace(/0+$/, '') || '0';
391+
.slice(2).replace(/0+$/, '') || '0';
392392
return fracSecs;
393393
});
394394

@@ -441,7 +441,7 @@ function formatTime(x, tr) {
441441
*/
442442
var sec = Math.min(mod(x / ONESEC, 60), MAXSECONDS[tr]);
443443

444-
var secStr = (100 + sec).toFixed(tr).substr(1);
444+
var secStr = (100 + sec).toFixed(tr).slice(1);
445445
if(tr > 0) {
446446
secStr = secStr.replace(/0+$/, '').replace(/[\.]$/, '');
447447
}

src/lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ lib.syncOrAsync = function (sequence, arg, finalStep) {
462462
* http://stackoverflow.com/questions/6680825/return-string-without-trailing-slash
463463
*/
464464
lib.stripTrailingSlash = function (str) {
465-
if (str.substr(-1) === '/') return str.substr(0, str.length - 1);
465+
if (str.slice(-1) === '/') return str.slice(0, -1);
466466
return str;
467467
};
468468

@@ -737,7 +737,7 @@ function minExtend(obj1, obj2, opt) {
737737
lib.minExtend = minExtend;
738738

739739
lib.titleCase = function (s) {
740-
return s.charAt(0).toUpperCase() + s.substr(1);
740+
return s.charAt(0).toUpperCase() + s.slice(1);
741741
};
742742

743743
lib.containsAny = function (s, fragments) {

0 commit comments

Comments
 (0)