Skip to content

Commit 7a18ec7

Browse files
committed
Merge pull request #47 from plotly/len-1-bins
length-1 bins count as ascending in Plotly.Lib.findBin
2 parents 459c928 + 321400f commit 7a18ec7

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/lib/search.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ exports.findBin = function(val, bins, linelow) {
3434
c = 0,
3535
n,
3636
test;
37-
if(bins[bins.length - 1] > bins[0]) {
37+
if(bins[bins.length - 1] >= bins[0]) {
3838
test = linelow ? lessThan : lessOrEqual;
3939
} else {
4040
test = linelow ? greaterOrEqual : greaterThan;

test/jasmine/tests/search_test.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var Plotly = require('@src/plotly');
2+
3+
describe('Test search.js:', function() {
4+
'use strict';
5+
6+
describe('findBin', function() {
7+
it('should work on ascending arrays', function() {
8+
expect(Plotly.Lib.findBin(-10000, [0, 1, 3])).toBe(-1);
9+
expect(Plotly.Lib.findBin(0.5, [0, 1, 3])).toBe(0);
10+
expect(Plotly.Lib.findBin(2, [0, 1, 3])).toBe(1);
11+
expect(Plotly.Lib.findBin(10000, [0, 1, 3])).toBe(2);
12+
// default: linelow falsey, so the line is in the higher bin
13+
expect(Plotly.Lib.findBin(1, [0, 1, 3])).toBe(1);
14+
// linelow truthy, so the line is in the lower bin
15+
expect(Plotly.Lib.findBin(1, [0, 1, 3], true)).toBe(0);
16+
});
17+
18+
it('should work on decending arrays', function() {
19+
expect(Plotly.Lib.findBin(-10000, [3, 1, 0])).toBe(2);
20+
expect(Plotly.Lib.findBin(0.5, [3, 1, 0])).toBe(1);
21+
expect(Plotly.Lib.findBin(2, [3, 1, 0])).toBe(0);
22+
expect(Plotly.Lib.findBin(10000, [3, 1, 0])).toBe(-1);
23+
24+
expect(Plotly.Lib.findBin(1, [3, 1, 0])).toBe(0);
25+
expect(Plotly.Lib.findBin(1, [3, 1, 0], true)).toBe(1);
26+
});
27+
28+
it('should treat a length-1 array as ascending', function() {
29+
expect(Plotly.Lib.findBin(-1, [0])).toBe(-1);
30+
expect(Plotly.Lib.findBin(1, [0])).toBe(0);
31+
32+
expect(Plotly.Lib.findBin(0, [0])).toBe(0);
33+
expect(Plotly.Lib.findBin(0, [0], true)).toBe(-1);
34+
});
35+
// TODO: didn't test bins as objects {start, stop, size}
36+
});
37+
});

0 commit comments

Comments
 (0)