Skip to content

Commit 92f7c13

Browse files
committed
replace Array.prototype.find with for loop and update tests
1 parent 6420044 commit 92f7c13

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

Diff for: src/plots/cartesian/axes.js

+27-24
Original file line numberDiff line numberDiff line change
@@ -1503,13 +1503,6 @@ axes.getTickFormat = function(ax) {
15031503
function convertToMs(dtick) {
15041504
return typeof dtick !== 'string' ? dtick : Number(dtick.replace('M', '') * ONEAVGMONTH);
15051505
}
1506-
function isProperStop(dtick, range, convert) {
1507-
var convertFn = convert || function(x) { return x;};
1508-
var leftDtick = range[0];
1509-
var rightDtick = range[1];
1510-
return ((!leftDtick && typeof leftDtick !== 'number') || convertFn(leftDtick) <= convertFn(dtick)) &&
1511-
((!rightDtick && typeof rightDtick !== 'number') || convertFn(rightDtick) >= convertFn(dtick));
1512-
}
15131506
function getRangeWidth(range, convert) {
15141507
var convertFn = convert || function(x) { return x;};
15151508
var left = range[0] || 0;
@@ -1534,31 +1527,41 @@ axes.getTickFormat = function(ax) {
15341527
return typeof left === 'number' ? 1 : -1;
15351528
}
15361529
}
1530+
function isProperStop(dtick, range, convert) {
1531+
var convertFn = convert || function(x) { return x;};
1532+
var leftDtick = range[0];
1533+
var rightDtick = range[1];
1534+
return ((!leftDtick && typeof leftDtick !== 'number') || convertFn(leftDtick) <= convertFn(dtick)) &&
1535+
((!rightDtick && typeof rightDtick !== 'number') || convertFn(rightDtick) >= convertFn(dtick));
1536+
}
1537+
function isProperLogStop(dtick, range){
1538+
var isLeftDtickNull = range[0] === null;
1539+
var isRightDtickNull = range[1] === null;
1540+
var isDtickInRangeLeft = compareLogTicks(dtick, range[0]) >= 0;
1541+
var isDtickInRangeRight = compareLogTicks(dtick, range[1]) <= 0;
1542+
return (isLeftDtickNull || isDtickInRangeLeft) && (isRightDtickNull || isDtickInRangeRight);
1543+
}
15371544

15381545
var tickstop;
15391546
if(ax.tickformatstops && ax.tickformatstops.length > 0) {
15401547
switch(ax.type) {
1541-
case 'date': {
1542-
tickstop = ax.tickformatstops.find(function(stop) {
1543-
return isProperStop(ax.dtick, stop.dtickrange, convertToMs)
1544-
});
1545-
break;
1546-
}
1548+
case 'date':
15471549
case 'linear': {
1548-
tickstop = ax.tickformatstops.find(function(stop) {
1549-
return isProperStop(ax.dtick, stop.dtickrange, convertToMs)
1550-
});
1550+
for(var i = 0; i < ax.tickformatstops.length; i++) {
1551+
if (isProperStop(ax.dtick, ax.tickformatstops[i].dtickrange, convertToMs)){
1552+
tickstop = ax.tickformatstops[i];
1553+
break;
1554+
}
1555+
}
15511556
break;
15521557
}
15531558
case 'log': {
1554-
tickstop = ax.tickformatstops.find(function(stop) {
1555-
var left = stop.dtickrange[0], right = stop.dtickrange[1];
1556-
var isLeftDtickNull = left === null;
1557-
var isRightDtickNull = right === null;
1558-
var isDtickInRangeLeft = compareLogTicks(ax.dtick, left) >= 0;
1559-
var isDtickInRangeRight = compareLogTicks(ax.dtick, right) <= 0;
1560-
return (isLeftDtickNull || isDtickInRangeLeft) && (isRightDtickNull || isDtickInRangeRight);
1561-
});
1559+
for(var i = 0; i < ax.tickformatstops.length; i++) {
1560+
if (isProperLogStop(ax.dtick, ax.tickformatstops[i].dtickrange)) {
1561+
tickstop = ax.tickformatstops[i];
1562+
break;
1563+
}
1564+
}
15621565
break;
15631566
}
15641567
default:

Diff for: test/jasmine/tests/tickformatstops_test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('Test Axes.getTickformat', function() {
5050
type: 'linear',
5151
tickformatstops: lineartickformatstops,
5252
dtick: 1
53-
})).toEqual(lineartickformatstops[1].value);
53+
})).toEqual(lineartickformatstops[0].value);
5454

5555
expect(Axes.getTickFormat({
5656
type: 'linear',
@@ -117,7 +117,7 @@ describe('Test Axes.getTickformat', function() {
117117
type: 'date',
118118
tickformatstops: datetickformatstops,
119119
dtick: 1000
120-
})).toEqual(datetickformatstops[1].value); // second
120+
})).toEqual(datetickformatstops[0].value); // millisecond
121121

122122
expect(Axes.getTickFormat({
123123
type: 'date',
@@ -135,7 +135,7 @@ describe('Test Axes.getTickformat', function() {
135135
type: 'date',
136136
tickformatstops: datetickformatstops,
137137
dtick: 'M1'
138-
})).toEqual(datetickformatstops[6].value); // month
138+
})).toEqual(datetickformatstops[5].value); // week
139139

140140
expect(Axes.getTickFormat({
141141
type: 'date',

0 commit comments

Comments
 (0)