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

Skip to content

Commit 0e54e6c

Browse files
committed
replace .substr(a,b) with .substring(a,a+b)
1 parent 4e3d62a commit 0e54e6c

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
"use-draftlogs": "node tasks/use_draftlogs.js",
3333
"empty-draftlogs": "node tasks/empty_draftlogs.js",
3434
"empty-dist": "node tasks/empty_dist.js",
35-
"build": "npm run empty-dist && npm run preprocess && npm run find-strings && npm run bundle && npm run extra-bundles basic && npm run extra-bundles cartesian && npm run extra-bundles geo && npm run extra-bundles gl2d && npm run extra-bundles gl3d && npm run extra-bundles mapbox && npm run extra-bundles finance && npm run locales && npm run schema dist && npm run stats",
35+
"build": "npm run empty-dist && npm run preprocess && npm run find-strings && npm run bundle && npm run extra-bundles basic && npm run extra-bundles cartesian && npm run extra-bundles geo && npm run extra-bundles gl2d && npm run extra-bundles gl3d && npm run extra-bundles mapbox && npm run extra-bundles finance && npm run locales && npm run schema dist && npm run replace-substr && npm run stats",
36+
"replace-substr": "node ./tasks/replace_substr.js",
3637
"regl-codegen": "node devtools/regl_codegen/server.js",
37-
"cibuild": "npm run empty-dist && npm run preprocess && node tasks/cibundle.js",
38+
"cibuild": "npm run empty-dist && npm run preprocess && node tasks/cibundle.js && npm run replace-substr -- build",
3839
"watch": "node tasks/watch.js",
3940
"lint": "eslint --version && eslint .",
4041
"lint-fix": "eslint . --fix || true",

tasks/replace_substr.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'use strict';
2+
3+
// Replace .substr(a, ?b) with .substring(a, (a)+b)
4+
//
5+
// String.prototype.substr() is deprecated!
6+
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
7+
8+
var fs = require('fs');
9+
var allFolders = ['dist/'];
10+
if(process.argv.indexOf('build') !== -1) allFolders.push('build/');
11+
12+
for(var k = 0; k < allFolders.length; k++) {
13+
var folder = allFolders[k];
14+
var allFilenames = fs.readdirSync(folder);
15+
16+
for(var i = 0; i < allFilenames.length; i++) {
17+
var filename = allFilenames[i];
18+
var len = filename.length;
19+
if(filename.substring(len - 3) === '.js') {
20+
var f = folder + filename;
21+
var str = fs.readFileSync(f, {encoding: 'utf8', flag: 'r+'});
22+
fs.writeFileSync(f, replaceSubstr(str), 'utf8');
23+
console.log('Overwritten:' + f);
24+
}
25+
}
26+
}
27+
28+
function replaceSubstr(str) {
29+
var i0;
30+
while(i0 !== -1) {
31+
i0 = str.indexOf('.substr(');
32+
if(i0 === -1) return str;
33+
34+
var args = [];
35+
var text = '';
36+
var k = 0;
37+
38+
// step into the function
39+
var i = i0 + 7;
40+
var p = 1; // open parentheses
41+
while(p > 0) {
42+
i++;
43+
44+
var c = str.charAt(i);
45+
if(!c) break;
46+
if(p === 1 && (
47+
c === ',' ||
48+
c === ')'
49+
)) {
50+
args[k++] = text;
51+
text = '';
52+
} else {
53+
text += c;
54+
}
55+
56+
if(c === '(') p++;
57+
if(c === ')') p--;
58+
}
59+
60+
// console.log(str.substring(i0, i + 1));
61+
// console.log(args);
62+
63+
var startStr = args[0];
64+
var lengthStr = args[1];
65+
var out = '.substring(' + startStr;
66+
67+
if(lengthStr !== undefined) {
68+
out += ',';
69+
if(+startStr !== 0) {
70+
out += '(' + startStr + ')+';
71+
}
72+
out += lengthStr;
73+
}
74+
75+
out += ')';
76+
77+
// console.log(out)
78+
// console.log('__');
79+
80+
str = str.substring(0, i0) + out + str.substring(i + 1);
81+
}
82+
83+
return str;
84+
}

0 commit comments

Comments
 (0)