Add ol.array.reverseSubArray

This commit is contained in:
Éric Lemoine
2013-12-08 21:35:27 +01:00
parent 554e17ac22
commit 0d0b19128f
2 changed files with 41 additions and 0 deletions

View File

@@ -84,3 +84,21 @@ ol.array.linearFindNearest = function(arr, target, direction) {
return n - 1;
}
};
/**
* @param {Array.<*>} arr Array.
* @param {number} begin Begin index.
* @param {number} end End index.
*/
ol.array.reverseSubArray = function(arr, begin, end) {
goog.asserts.assert(begin >= 0);
goog.asserts.assert(end < arr.length);
while (begin < end) {
var tmp = arr[begin];
arr[begin] = arr[end];
arr[end] = tmp;
++begin;
--end;
}
};

View File

@@ -73,6 +73,29 @@ describe('ol.array', function() {
expect(ol.array.linearFindNearest(arr, 50, -1)).to.eql(2);
});
});
describe.only('reverseSubArray', function() {
it('returns expected value', function() {
var arr;
var expected = [1, 2, 3, 4, 5, 6];
arr = [1, 5, 4, 3, 2, 6];
ol.array.reverseSubArray(arr, 1, 4);
expect(arr).to.eql(expected);
arr = [3, 2, 1, 4, 5, 6];
ol.array.reverseSubArray(arr, 0, 2);
expect(arr).to.eql(expected);
arr = [1, 2, 3, 6, 5, 4];
ol.array.reverseSubArray(arr, 3, 5);
expect(arr).to.eql(expected);
arr = [6, 5, 4, 3, 2, 1];
ol.array.reverseSubArray(arr, 0, 5);
expect(arr).to.eql(expected);
});
});
});
goog.require('ol.array');