Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/array-max/src/__tests__/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/,
);
});
});
28 changes: 25 additions & 3 deletions packages/array-max/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import isArray from 'is-any-array';
/**
* Computes the maximum of the given values
* @param {Array<number>} 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');
}
Expand All @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions packages/array-min/src/__tests__/min.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/,
);
});
});
28 changes: 25 additions & 3 deletions packages/array-min/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import isArray from 'is-any-array';
/**
* Computes the minimum of the given values
* @param {Array<number>} 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');
}
Expand All @@ -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;
Expand Down