diff --git a/spec/core/array/operator/rotate.spec.ts b/spec/core/array/operator/rotate.spec.ts index 988518d..32fff7b 100644 --- a/spec/core/array/operator/rotate.spec.ts +++ b/spec/core/array/operator/rotate.spec.ts @@ -7,7 +7,29 @@ describe('rotate', () => { const array = [1, 2, 3, 4, 5]; const fn = rotate(2); expect(fn(array)).to.eql([3, 4, 5, 1, 2,]); - + }); + it('should rotate negative', () => { + const array = [1, 2, 3, 4, 5]; + const fn = rotate(-2); + expect(fn(array)).to.eql([4, 5, 1, 2, 3,]); + }); + it('should rotate negative twice', () => { + const array = [1, 2, 3, 4, 5]; + expect(rotate(-2)(array)).to.eql(rotate(-12)(array)); + }); + it('should rotate another test case', () => { + const array = [1, 2, 3, 4, 5, 6, 7]; + expect(rotate(-2)(array)).to.eql([6, 7, 1, 2, 3, 4, 5,]); + expect(rotate(2)(array)).to.eql([3, 4, 5, 6, 7, 1, 2,]); + }); + it('should rotate mod length', () => { + const array = [1, 2, 3, 4, 5]; + const offset = Math.floor(Math.random() * array.length); + const n = Math.round(Math.random() * 10) - 5; + const fn = rotate(offset); + expect(fn(array)).to.eql(rotate(offset + array.length)(array)); + expect(fn(array)).to.eql(rotate(offset - array.length)(array)); + expect(fn(array)).to.eql(rotate(offset + array.length * n)(array)); }); it('should rotate negative', () => { @@ -17,7 +39,7 @@ describe('rotate', () => { it('should rotate modulo', () => { const array = [1, 2, 3, 4, 5]; - expect(rotate(6)(array)).to.eql(rotate(6%array.length)(array)); + expect(rotate(6)(array)).to.eql(rotate(6 % array.length)(array)); }); @@ -28,11 +50,11 @@ describe('rotate', () => { it('should rotate early', () => { const fn = rotate(2); - expect(fn([1])).to.eql([1,]); + expect(fn([1])).to.eql([1]); }); it('should rotate last', () => { const fn = rotate(2); - expect(fn([1, 2])).to.eql([1, 2,]); + expect(fn([1, 2])).to.eql([1, 2]); }); it('should rotate 0', () => { diff --git a/src/array/operator/rotate.ts b/src/array/operator/rotate.ts index a1fd09f..743ed63 100644 --- a/src/array/operator/rotate.ts +++ b/src/array/operator/rotate.ts @@ -1,7 +1,6 @@ export function rotate(offset: number) { - return (array: S[]): S[] => { - const length = array.length; - const index = ((offset % length) + length) % length; + return (array: T[]): T[] => { + const index = offset % array.length; return array.slice(index).concat(array.slice(0, index)); }; } diff --git a/src/array/operator/rotateBack.ts b/src/array/operator/rotateBack.ts index 3b56185..53689c6 100644 --- a/src/array/operator/rotateBack.ts +++ b/src/array/operator/rotateBack.ts @@ -1,3 +1,3 @@ import {rotate} from './rotate'; -export const rotateBack = (offset: number) => rotate(-offset) +export const rotateBack = (offset: number) => rotate(-offset);