Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Replace calls to deprecated String.substr() #6146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions tasks/util/browserify_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var path = require('path');
var browserify = require('browserify');
var minify = require('minify-stream');
var derequire = require('derequire');
var substr2slice = require('./substr2slice');
var through = require('through2');

var strictD3 = require('./strict_d3');
Expand Down Expand Up @@ -78,7 +79,7 @@ module.exports = function _bundle(pathToIndex, pathToBundle, opts, cb) {
};

bundleStream
.pipe(applyDerequire())
.pipe(modify())
.pipe(minify(minifyOpts))
.pipe(fs.createWriteStream(pathToMinBundle))
.on('finish', function() {
Expand All @@ -89,7 +90,7 @@ module.exports = function _bundle(pathToIndex, pathToBundle, opts, cb) {

if(pathToBundle) {
bundleStream
.pipe(applyDerequire())
.pipe(modify())
.pipe(fs.createWriteStream(pathToBundle))
.on('finish', function() {
logger(pathToBundle);
Expand All @@ -103,13 +104,19 @@ function logger(pathToOutput) {
console.log(log);
}

function applyDerequire() {
var buf = '';
function modify() {
var str = '';
return through(function(chunk, enc, next) {
buf += chunk.toString();
str += chunk.toString();
next();
}, function(done) {
this.push(derequire(buf));
this.push(
derequire( // require >> _dereq_
substr2slice( // substr >> slice
str
)
)
);
done();
});
}
65 changes: 65 additions & 0 deletions tasks/util/substr2slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';

// Replace .substr(a, ?b) with .slice(a, (a)+b)
//
// String.prototype.substr() is deprecated!
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr

module.exports = function substr2slice(str) {
var i0 = 0;
while(i0 !== -1) {
// assuming there is no white space after substr
i0 = str.indexOf('.substr(', i0);
if(i0 === -1) return str;

var args = [];
var text = '';
var k = 0;

// step into the function
var i = i0 + 7;
var p = 1; // open parentheses
while(p > 0) {
i++;

var c = str.charAt(i);
if(!c) break;
if(p === 1 && (
c === ',' ||
c === ')'
)) {
args[k++] = text;
text = '';
} else {
text += c;
}

if(c === '(') p++;
if(c === ')') p--;
}

// console.log(str.slice(i0, i + 1));
// console.log(args);

var startStr = args[0];
var lengthStr = args[1];
var out = '.slice(' + startStr;

if(lengthStr !== undefined) {
out += ',';
if(+startStr !== 0) {
out += '(' + startStr + ') + ';
}
out += lengthStr;
}

out += ')';

// console.log(out)
// console.log('__');

str = str.slice(0, i0) + out + str.slice(i + 1);
}

return str;
};
18 changes: 18 additions & 0 deletions tasks/util/watchified_bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ var fs = require('fs');
var browserify = require('browserify');
var watchify = require('watchify');
var prettySize = require('prettysize');
var through = require('through2');

var substr2slice = require('../../tasks/util/substr2slice');
var constants = require('./constants');
var common = require('./common');

Expand Down Expand Up @@ -50,6 +52,7 @@ module.exports = function makeWatchifiedBundle(strict, onFirstBundleCallback) {
firstBundle = false;
}
})
.pipe(modify())
.pipe(
fs.createWriteStream(constants.pathToPlotlyBuild)
);
Expand Down Expand Up @@ -81,3 +84,18 @@ function formatBundleMsg(b, bundleName) {
console.log(msgParts.join(' '));
});
}

function modify() {
var str = '';
return through(function(chunk, enc, next) {
str += chunk.toString();
next();
}, function(done) {
this.push(
substr2slice( // substr >> slice
str
)
);
done();
});
}