From 0d0b19128f593cd11ccf8a4e263abd3d0cf9c91d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Sun, 8 Dec 2013 21:35:27 +0100 Subject: [PATCH] Add ol.array.reverseSubArray --- src/ol/array.js | 18 ++++++++++++++++++ test/spec/ol/array.test.js | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/ol/array.js b/src/ol/array.js index 4474eada9f..882a501262 100644 --- a/src/ol/array.js +++ b/src/ol/array.js @@ -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; + } +}; diff --git a/test/spec/ol/array.test.js b/test/spec/ol/array.test.js index 740b77c8c1..5cf63f1ee3 100644 --- a/test/spec/ol/array.test.js +++ b/test/spec/ol/array.test.js @@ -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');