ml-array-max
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -6,2 +6,14 @@ # Change Log | ||
# [1.2.0](https://github.com/mljs/array/compare/ml-array-max@1.1.2...ml-array-max@1.2.0) (2020-10-07) | ||
### Features | ||
* adding fromIndex/toIndex options for min and max. ([#13](https://github.com/mljs/array/issues/13)) ([d32b851](https://github.com/mljs/array/commit/d32b85106f703b4d6a82441f6db166ff057383c1)) | ||
## [1.1.2](https://github.com/mljs/array/compare/ml-array-max@1.1.1...ml-array-max@1.1.2) (2019-08-30) | ||
@@ -8,0 +20,0 @@ |
import isArray from 'is-any-array'; | ||
/** | ||
* Computes the maximum of the given values | ||
* @param {Array<number>} input | ||
* @return {number} | ||
*/ | ||
function max(input) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
function max(input) { | ||
if (!isArray(input)) { | ||
@@ -18,5 +14,18 @@ throw new TypeError('input must be an array'); | ||
var maxValue = input[0]; | ||
var _options$fromIndex = options.fromIndex, | ||
fromIndex = _options$fromIndex === void 0 ? 0 : _options$fromIndex, | ||
_options$toIndex = options.toIndex, | ||
toIndex = _options$toIndex === void 0 ? input.length : _options$toIndex; | ||
for (var i = 1; i < input.length; i++) { | ||
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'); | ||
} | ||
var maxValue = input[fromIndex]; | ||
for (var i = fromIndex + 1; i < toIndex; i++) { | ||
if (input[i] > maxValue) maxValue = input[i]; | ||
@@ -23,0 +32,0 @@ } |
@@ -7,8 +7,3 @@ 'use strict'; | ||
/** | ||
* Computes the maximum of the given values | ||
* @param {Array<number>} input | ||
* @return {number} | ||
*/ | ||
function max(input) { | ||
function max(input, options = {}) { | ||
if (!isArray(input)) { | ||
@@ -22,4 +17,24 @@ throw new TypeError('input must be an array'); | ||
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]; | ||
@@ -26,0 +41,0 @@ } |
{ | ||
"name": "ml-array-max", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Get the maximum value in an array", | ||
@@ -10,3 +10,4 @@ "main": "lib/index.js", | ||
"lib-es6", | ||
"src" | ||
"src", | ||
"types.d.ts" | ||
], | ||
@@ -25,5 +26,5 @@ "repository": { | ||
"dependencies": { | ||
"is-any-array": "^0.0.3" | ||
"is-any-array": "^0.1.0" | ||
}, | ||
"gitHead": "6a3b7e1b0fab7f3996b9af3a75d6234a68c9e4d0" | ||
"gitHead": "942138d036d4c0753ad4c261cc692652c0a9cac1" | ||
} |
@@ -0,0 +0,0 @@ # array-max |
@@ -15,3 +15,10 @@ import max from '..'; | ||
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); | ||
}); | ||
@@ -21,3 +28,24 @@ it('should throw on invalid value', () => { | ||
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/, | ||
); | ||
}); | ||
}); |
import isArray from 'is-any-array'; | ||
/** | ||
* Computes the maximum of the given values | ||
* @param {Array<number>} input | ||
* @return {number} | ||
*/ | ||
export default function max(input) { | ||
export default function max(input, options = {}) { | ||
if (!isArray(input)) { | ||
@@ -17,4 +12,24 @@ throw new TypeError('input must be an array'); | ||
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]; | ||
@@ -21,0 +36,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9303
9
158
+ Addedis-any-array@0.1.1(transitive)
- Removedis-any-array@0.0.3(transitive)
Updatedis-any-array@^0.1.0