提交 ae67f126 编写于 作者: L lang

Add List#indexOfRawIndex

上级 96f5e100
......@@ -502,6 +502,32 @@ define(function (require) {
return -1;
};
/**
* Retreive the index with given raw data index
* @param {number} idx
* @param {number} name
* @return {number}
*/
listProto.indexOfRawIndex = function (rawIndex) {
// Indices are ascending
var indices = this.indices;
var left = 0;
var right = indices.length - 1;
while (left <= right) {
var mid = (left + right) / 2 | 0;
if (indices[mid] < rawIndex) {
left = mid + 1;
}
else if (indices[mid] > rawIndex) {
right = mid - 1;
}
else {
return mid;
}
}
return -1;
};
/**
* Retreive the index of nearest value
* @param {string} dim
......
......@@ -74,6 +74,32 @@ describe('List', function () {
expect(list.getItemModel(1).option).toEqual([20, 25]);
});
testCase('indexOfRawIndex', function (List) {
var list = new List(['x']);
list.initData([]);
expect(list.indexOfRawIndex(1)).toEqual(-1);
list.initData([0]);
expect(list.indexOfRawIndex(0)).toEqual(0);
expect(list.indexOfRawIndex(1)).toEqual(-1);
list.initData([0, 1, 2, 3]);
expect(list.indexOfRawIndex(1)).toEqual(1);
expect(list.indexOfRawIndex(2)).toEqual(2);
expect(list.indexOfRawIndex(5)).toEqual(-1);
list.initData([0, 1, 2, 3, 4]);
expect(list.indexOfRawIndex(2)).toEqual(2);
expect(list.indexOfRawIndex(3)).toEqual(3);
expect(list.indexOfRawIndex(5)).toEqual(-1);
list.filterSelf(function (idx) {
return idx >= 2;
});
expect(list.indexOfRawIndex(2)).toEqual(0);
});
testCase('getDataExtent', function (List) {
var list = new List(['x', 'y']);
list.initData([1, 2, 3]);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册