Skip to content

Commit 688fd6d

Browse files
committed
Remove references to undefined.
Although unlikely, it’s possible to reassign `undefined` externally so that it affects internal behaviour of any functions that reference it. This doesn’t affect the minified code, since `undefined` is replaced with `void 0` by UglifyJs. Although it’s not possible to defend against all possible ways of modifying core JavaScript functionality, it seems simple enough to avoid it in a few limited cases, and this change barely affects the minified and gzipped sizes. Fixes d3#2077.
1 parent 9364923 commit 688fd6d

7 files changed

Lines changed: 35 additions & 17 deletions

File tree

d3.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,53 @@
4242
d3.min = function(array, f) {
4343
var i = -1, n = array.length, a, b;
4444
if (arguments.length === 1) {
45-
while (++i < n && !((a = array[i]) != null && a <= a)) a = undefined;
45+
while (++i < n) if ((b = array[i]) != null && b >= b) {
46+
a = b;
47+
break;
48+
}
4649
while (++i < n) if ((b = array[i]) != null && a > b) a = b;
4750
} else {
48-
while (++i < n && !((a = f.call(array, array[i], i)) != null && a <= a)) a = undefined;
51+
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) {
52+
a = b;
53+
break;
54+
}
4955
while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b;
5056
}
5157
return a;
5258
};
5359
d3.max = function(array, f) {
5460
var i = -1, n = array.length, a, b;
5561
if (arguments.length === 1) {
56-
while (++i < n && !((a = array[i]) != null && a <= a)) a = undefined;
62+
while (++i < n) if ((b = array[i]) != null && b >= b) {
63+
a = b;
64+
break;
65+
}
5766
while (++i < n) if ((b = array[i]) != null && b > a) a = b;
5867
} else {
59-
while (++i < n && !((a = f.call(array, array[i], i)) != null && a <= a)) a = undefined;
68+
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) {
69+
a = b;
70+
break;
71+
}
6072
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b;
6173
}
6274
return a;
6375
};
6476
d3.extent = function(array, f) {
6577
var i = -1, n = array.length, a, b, c;
6678
if (arguments.length === 1) {
67-
while (++i < n && !((a = c = array[i]) != null && a <= a)) a = c = undefined;
79+
while (++i < n) if ((b = array[i]) != null && b >= b) {
80+
a = c = b;
81+
break;
82+
}
6883
while (++i < n) if ((b = array[i]) != null) {
6984
if (a > b) a = b;
7085
if (c < b) c = b;
7186
}
7287
} else {
73-
while (++i < n && !((a = c = f.call(array, array[i], i)) != null && a <= a)) a = undefined;
88+
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) {
89+
a = c = b;
90+
break;
91+
}
7492
while (++i < n) if ((b = f.call(array, array[i], i)) != null) {
7593
if (a > b) a = b;
7694
if (c < b) c = b;
@@ -100,7 +118,7 @@
100118
} else {
101119
while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) s += a; else --j;
102120
}
103-
return j ? s / j : undefined;
121+
if (j) return s / j;
104122
};
105123
d3.quantile = function(values, p) {
106124
var H = (values.length - 1) * p + 1, h = Math.floor(H), v = +values[h - 1], e = H - h;
@@ -113,7 +131,7 @@
113131
} else {
114132
while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) numbers.push(a);
115133
}
116-
return numbers.length ? d3.quantile(numbers.sort(d3_ascending), .5) : undefined;
134+
if (numbers.length) return d3.quantile(numbers.sort(d3_ascending), .5);
117135
};
118136
function d3_bisector(compare) {
119137
return {

d3.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/arrays/extent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ d3.extent = function(array, f) {
55
b,
66
c;
77
if (arguments.length === 1) {
8-
while (++i < n && !((a = c = array[i]) != null && a <= a)) a = c = undefined;
8+
while (++i < n) if ((b = array[i]) != null && b >= b) { a = c = b; break; }
99
while (++i < n) if ((b = array[i]) != null) {
1010
if (a > b) a = b;
1111
if (c < b) c = b;
1212
}
1313
} else {
14-
while (++i < n && !((a = c = f.call(array, array[i], i)) != null && a <= a)) a = undefined;
14+
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) { a = c = b; break; }
1515
while (++i < n) if ((b = f.call(array, array[i], i)) != null) {
1616
if (a > b) a = b;
1717
if (c < b) c = b;

src/arrays/max.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ d3.max = function(array, f) {
44
a,
55
b;
66
if (arguments.length === 1) {
7-
while (++i < n && !((a = array[i]) != null && a <= a)) a = undefined;
7+
while (++i < n) if ((b = array[i]) != null && b >= b) { a = b; break; }
88
while (++i < n) if ((b = array[i]) != null && b > a) a = b;
99
} else {
10-
while (++i < n && !((a = f.call(array, array[i], i)) != null && a <= a)) a = undefined;
10+
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) { a = b; break; }
1111
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b;
1212
}
1313
return a;

src/arrays/mean.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ d3.mean = function(array, f) {
1111
} else {
1212
while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) s += a; else --j;
1313
}
14-
return j ? s / j : undefined;
14+
if (j) return s / j;
1515
};

src/arrays/median.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ d3.median = function(array, f) {
1212
} else {
1313
while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) numbers.push(a);
1414
}
15-
return numbers.length ? d3.quantile(numbers.sort(d3_ascending), .5) : undefined;
15+
if (numbers.length) return d3.quantile(numbers.sort(d3_ascending), .5);
1616
};

src/arrays/min.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ d3.min = function(array, f) {
44
a,
55
b;
66
if (arguments.length === 1) {
7-
while (++i < n && !((a = array[i]) != null && a <= a)) a = undefined;
7+
while (++i < n) if ((b = array[i]) != null && b >= b) { a = b; break; }
88
while (++i < n) if ((b = array[i]) != null && a > b) a = b;
99
} else {
10-
while (++i < n && !((a = f.call(array, array[i], i)) != null && a <= a)) a = undefined;
10+
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) { a = b; break; }
1111
while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b;
1212
}
1313
return a;

0 commit comments

Comments
 (0)