diff --git a/packages/array-max/src/__tests__/max.test.js b/packages/array-max/src/__tests__/max.test.js index 14623f7..620b2e8 100644 --- a/packages/array-max/src/__tests__/max.test.js +++ b/packages/array-max/src/__tests__/max.test.js @@ -12,10 +12,38 @@ describe('array-max', () => { expect(max([1, 2])).toBe(2); expect(max([1, 2, 1])).toBe(2); expect(max([3, 2, 1])).toBe(3); + expect(max([3, 2, 1], { fromIndex: 0, toIndex: 2 })).toBe(3); + expect(max([3, 2, 1], { fromIndex: 0, toIndex: 3 })).toBe(3); + expect(max([3, 2, 1], { fromIndex: 1, toIndex: 3 })).toBe(2); + expect(max([3, 2, 1], { fromIndex: 0, toIndex: 2 })).toBe(3); + expect(max([3, 2, 1], { fromIndex: 2, toIndex: 3 })).toBe(1); expect(max(typedArray)).toBe(3); + expect(max(typedArray, { fromIndex: 0, toIndex: 2 })).toBe(2); + expect(max(typedArray, { fromIndex: 0, toIndex: 3 })).toBe(3); }); it('should throw on invalid value', () => { expect(() => max()).toThrow(/input must be an array/); expect(() => max([])).toThrow(/input must not be empty/); + expect(() => max([1, 2, 3], { fromIndex: -1, toIndex: 2 })).toThrow( + /fromIndex must be a positive integer smaller than length/, + ); + expect(() => max([1, 2, 3], { fromIndex: 4, toIndex: 2 })).toThrow( + /fromIndex must be a positive integer smaller than length/, + ); + expect(() => max([1, 2, 3], { fromIndex: 3, toIndex: 3 })).toThrow( + /fromIndex must be a positive integer smaller than length/, + ); + expect(() => max([1, 2, 3], { fromIndex: 1, toIndex: 0 })).toThrow( + /toIndex must be an integer greater than fromIndex and at most equal to length/, + ); + expect(() => max([1, 2, 3], { fromIndex: 1, toIndex: 4 })).toThrow( + /toIndex must be an integer greater than fromIndex and at most equal to length/, + ); + expect(() => max([1, 2, 3], { fromIndex: 0, toIndex: 1.5 })).toThrow( + /toIndex must be an integer greater than fromIndex and at most equal to length/, + ); + expect(() => max([1, 2, 3], { fromIndex: 1.5, toIndex: 2 })).toThrow( + /fromIndex must be a positive integer smaller than length/, + ); }); }); diff --git a/packages/array-max/src/index.js b/packages/array-max/src/index.js index d7c1185..8afcf5f 100644 --- a/packages/array-max/src/index.js +++ b/packages/array-max/src/index.js @@ -3,9 +3,11 @@ import isArray from 'is-any-array'; /** * Computes the maximum of the given values * @param {Array} input + * @param {number} [options.fromIndex] - Start index (inclusive) for the slice within which we look for the maximum + * @param {number} [options.toIndex] - End index (exclusive) for the slice within which we look for the maximum * @return {number} */ -export default function max(input) { +export default function max(input, options = {}) { if (!isArray(input)) { throw new TypeError('input must be an array'); } @@ -14,8 +16,28 @@ export default function max(input) { throw new TypeError('input must not be empty'); } - let maxValue = input[0]; - for (let i = 1; i < input.length; i++) { + const { fromIndex = 0, toIndex = input.length } = options; + + if ( + fromIndex < 0 || + fromIndex >= input.length || + !Number.isInteger(fromIndex) + ) { + throw new Error('fromIndex must be a positive integer smaller than length'); + } + + if ( + toIndex <= fromIndex || + toIndex > input.length || + !Number.isInteger(toIndex) + ) { + throw new Error( + 'toIndex must be an integer greater than fromIndex and at most equal to length', + ); + } + + let maxValue = input[fromIndex]; + for (let i = fromIndex + 1; i < toIndex; i++) { if (input[i] > maxValue) maxValue = input[i]; } return maxValue; diff --git a/packages/array-min/src/__tests__/min.test.js b/packages/array-min/src/__tests__/min.test.js index d0b1e58..e375090 100644 --- a/packages/array-min/src/__tests__/min.test.js +++ b/packages/array-min/src/__tests__/min.test.js @@ -13,9 +13,40 @@ describe('array-min', () => { expect(min([1, 2, 1])).toStrictEqual(1); expect(min([3, 2, 1])).toStrictEqual(1); expect(min(typedArray)).toStrictEqual(1); + + expect(min([3, 2, 1], { fromIndex: 0, toIndex: 2 })).toBe(2); + expect(min([3, 2, 1], { fromIndex: 0, toIndex: 3 })).toBe(1); + expect(min([3, 2, 1], { fromIndex: 1, toIndex: 3 })).toBe(1); + expect(min([3, 2, 1], { fromIndex: 0, toIndex: 2 })).toBe(2); + expect(min([3, 2, 1], { fromIndex: 2, toIndex: 3 })).toBe(1); + expect(min(typedArray)).toBe(1); + expect(min(typedArray, { fromIndex: 0, toIndex: 2 })).toBe(1); + expect(min(typedArray, { fromIndex: 0, toIndex: 3 })).toBe(1); }); it('should throw on invalid value', () => { expect(() => min()).toThrow(/input must be an array/); expect(() => min([])).toThrow(/input must not be empty/); + + expect(() => min([1, 2, 3], { fromIndex: -1, toIndex: 2 })).toThrow( + /fromIndex must be a positive integer smaller than length/, + ); + expect(() => min([1, 2, 3], { fromIndex: 4, toIndex: 2 })).toThrow( + /fromIndex must be a positive integer smaller than length/, + ); + expect(() => min([1, 2, 3], { fromIndex: 3, toIndex: 3 })).toThrow( + /fromIndex must be a positive integer smaller than length/, + ); + expect(() => min([1, 2, 3], { fromIndex: 1, toIndex: 0 })).toThrow( + /toIndex must be an integer greater than fromIndex and at most equal to length/, + ); + expect(() => min([1, 2, 3], { fromIndex: 1, toIndex: 4 })).toThrow( + /toIndex must be an integer greater than fromIndex and at most equal to length/, + ); + expect(() => min([1, 2, 3], { fromIndex: 0, toIndex: 1.5 })).toThrow( + /toIndex must be an integer greater than fromIndex and at most equal to length/, + ); + expect(() => min([1, 2, 3], { fromIndex: 1.5, toIndex: 2 })).toThrow( + /fromIndex must be a positive integer smaller than length/, + ); }); }); diff --git a/packages/array-min/src/index.js b/packages/array-min/src/index.js index c2138a4..25abde4 100644 --- a/packages/array-min/src/index.js +++ b/packages/array-min/src/index.js @@ -3,9 +3,11 @@ import isArray from 'is-any-array'; /** * Computes the minimum of the given values * @param {Array} input + * @param {number} [options.fromIndex] - Start index (inclusive) for the slice within which we look for the minimum + * @param {number} [options.toIndex] - End index (exclusive) for the slice within which we look for the minimum * @return {number} */ -export default function min(input) { +export default function min(input, options = {}) { if (!isArray(input)) { throw new TypeError('input must be an array'); } @@ -14,8 +16,28 @@ export default function min(input) { throw new TypeError('input must not be empty'); } - let minValue = input[0]; - for (let i = 1; i < input.length; i++) { + const { fromIndex = 0, toIndex = input.length } = options; + + if ( + fromIndex < 0 || + fromIndex >= input.length || + !Number.isInteger(fromIndex) + ) { + throw new Error('fromIndex must be a positive integer smaller than length'); + } + + if ( + toIndex <= fromIndex || + toIndex > input.length || + !Number.isInteger(toIndex) + ) { + throw new Error( + 'toIndex must be an integer greater than fromIndex and at most equal to length', + ); + } + + let minValue = input[fromIndex]; + for (let i = fromIndex + 1; i < toIndex; i++) { if (input[i] < minValue) minValue = input[i]; } return minValue;