Skip to content

Commit ca3fa4d

Browse files
committed
lint + add test for open[i] === close[i] cases
1 parent 961fed4 commit ca3fa4d

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

src/traces/ohlc/helpers.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,21 @@ exports.makeTransform = function(traceIn, state, direction) {
9595
};
9696

9797
exports.getFilterFn = function(direction) {
98+
return new _getFilterFn(direction);
99+
};
98100

99-
var fn;
101+
function _getFilterFn(direction) {
100102
var isPrevThisDirection = null;
101-
var oprev = null;
102-
var cprev = null;
103+
var cPrev = null;
104+
var fn;
103105

104106
switch(direction) {
105107
case 'increasing':
106-
var fn = function(o, c) {
108+
fn = function(o, c) {
107109
if(o === c) {
108-
if(c > cprev) {
110+
if(c > cPrev) {
109111
return true; // increasing
110-
} else if(c < cprev) {
112+
} else if(c < cPrev) {
111113
return false; // decreasing
112114
} else {
113115
if(isPrevThisDirection === true) {
@@ -124,11 +126,11 @@ exports.getFilterFn = function(direction) {
124126
break;
125127

126128
case 'decreasing':
127-
var fn = function(o, c) {
129+
fn = function(o, c) {
128130
if(o === c) {
129-
if(c > cprev) {
131+
if(c > cPrev) {
130132
return false; // increasing
131-
} else if(c < cprev) {
133+
} else if(c < cPrev) {
132134
return true; // decreasing
133135
} else {
134136
if(isPrevThisDirection === true) {
@@ -142,18 +144,17 @@ exports.getFilterFn = function(direction) {
142144
}
143145
return o > c;
144146
};
145-
break
147+
break;
146148
}
147-
149+
148150
return function(o, c) {
149151
var out = fn(o, c);
150152
isPrevThisDirection = !!out;
151-
oprev = o;
152-
cprev = c;
153+
cPrev = c;
153154
return out;
154155
};
156+
}
155157

156-
};
157158
exports.addRangeSlider = function(data, layout) {
158159
var hasOneVisibleTrace = false;
159160

test/jasmine/tests/finance_test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,45 @@ describe('finance charts calc transforms:', function() {
704704
expect(out[1].x).toEqual([]);
705705
expect(out[3].x).toEqual([]);
706706
});
707+
708+
it('should handle cases where \'open\' and \'close\' entries are equal', function() {
709+
var out = _calc([{
710+
type: 'ohlc',
711+
open: [0, 1, 0, 2, 1, 1, 2, 2],
712+
high: [3, 3, 3, 3, 3, 3, 3, 3],
713+
low: [-1, -1, -1, -1, -1, -1, -1, -1],
714+
close: [0, 2, 0, 1, 1, 1, 2, 2],
715+
tickwidth: 0
716+
}, {
717+
type: 'candlestick',
718+
open: [0, 2, 0, 1],
719+
high: [3, 3, 3, 3],
720+
low: [-1, -1, -1, -1],
721+
close: [0, 1, 0, 2]
722+
}]);
723+
724+
expect(out[0].x).toEqual([
725+
0, 0, 0, 0, 0, 0, null,
726+
1, 1, 1, 1, 1, 1, null,
727+
6, 6, 6, 6, 6, 6, null,
728+
7, 7, 7, 7, 7, 7, null
729+
]);
730+
expect(out[1].x).toEqual([
731+
2, 2, 2, 2, 2, 2, null,
732+
3, 3, 3, 3, 3, 3, null,
733+
4, 4, 4, 4, 4, 4, null,
734+
5, 5, 5, 5, 5, 5, null
735+
]);
736+
737+
expect(out[2].x).toEqual([
738+
0, 0, 0, 0, 0, 0,
739+
3, 3, 3, 3, 3, 3
740+
]);
741+
expect(out[3].x).toEqual([
742+
1, 1, 1, 1, 1, 1,
743+
2, 2, 2, 2, 2, 2
744+
]);
745+
});
707746
});
708747

709748
describe('finance charts updates:', function() {

0 commit comments

Comments
 (0)