Last active
April 4, 2016 12:48
-
-
Save woss/50f1bee8271b5da446b29876fe5afc57 to your computer and use it in GitHub Desktop.
move-js-function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function move(array, oldIndex, newIndex) { | |
if (newIndex >= array.length) { | |
newIndex = array.length - 1; | |
} | |
array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]); | |
return array; | |
} | |
describe('ArrayHelper', function() { | |
it('Move right', function() { | |
let array = [1, 2, 3]; | |
arrayHelper.move(array, 0, 1); | |
assert.equal(array[0], 2); | |
assert.equal(array[1], 1); | |
assert.equal(array[2], 3); | |
}) | |
it('Move left', function() { | |
let array = [1, 2, 3]; | |
arrayHelper.move(array, 1, 0); | |
assert.equal(array[0], 2); | |
assert.equal(array[1], 1); | |
assert.equal(array[2], 3); | |
}); | |
it('Move out of bounds to the left', function() { | |
let array = [1, 2, 3]; | |
arrayHelper.move(array, 1, -2); | |
assert.equal(array[0], 2); | |
assert.equal(array[1], 1); | |
assert.equal(array[2], 3); | |
}); | |
it('Move out of bounds to the right', function() { | |
let array = [1, 2, 3]; | |
arrayHelper.move(array, 1, 4); | |
assert.equal(array[0], 1); | |
assert.equal(array[1], 3); | |
assert.equal(array[2], 2); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment