[WIP - feedback request] chore: max accepts from/to option#11
[WIP - feedback request] chore: max accepts from/to option#11kjappelbaum wants to merge 0 commit intomljs:masterfrom
Conversation
|
I like the from-to notation, seems clear enough for me |
| */ | ||
| export default function max(input) { | ||
| export default function max(input, options = {}) { | ||
| let { from, to } = options; |
There was a problem hiding this comment.
| let { from, to } = options; | |
| let { from = 0, to = input.length } = options; |
- Move this line after the validation of
inputto avoid errors if that is not an array - You can then remove the "if undefined" conditions below
| /** | ||
| * Computes the maximum of the given values | ||
| * @param {Array<number>} input | ||
| * @param {int} [options.from] - Start index (inclusive) for the slice within which we look for the max |
There was a problem hiding this comment.
| * @param {int} [options.from] - Start index (inclusive) for the slice within which we look for the max | |
| * @param {number} [options.from] - Start index (inclusive) for the slice within which we look for the max |
int doesn't exist in JS/JSDoc
| for (let i = 1; i < input.length; i++) { | ||
| if (from === undefined) { | ||
| from = 0; | ||
| } else if ((from < 0) | (from > input.length)) { |
There was a problem hiding this comment.
you can also check Number.isInteger(from). same for to
| if (input.length === 0) { | ||
| throw new TypeError('input must not be empty'); | ||
| } | ||
| let { fromIndex, toIndex } = options; |
There was a problem hiding this comment.
| let { fromIndex, toIndex } = options; | |
| const { fromIndex = 0, toIndex = input.length } = options; |
| if (fromIndex === undefined) { | ||
| fromIndex = 0; | ||
| } else if ( |
There was a problem hiding this comment.
| if (fromIndex === undefined) { | |
| fromIndex = 0; | |
| } else if ( | |
| if ( |
| if (fromIndex === undefined) { | ||
| fromIndex = 0; | ||
| } else if ( | ||
| (fromIndex < 0) | |
There was a problem hiding this comment.
| (fromIndex < 0) | | |
| (fromIndex < 0) || |
| fromIndex = 0; | ||
| } else if ( | ||
| (fromIndex < 0) | | ||
| (fromIndex > input.length) | |
There was a problem hiding this comment.
| (fromIndex > input.length) | | |
| (fromIndex >= input.length) || |
fromIndex cannot be === input.length because that would leave no room for toIndex
| ) { | ||
| throw new Error( | ||
| 'start index must be greater than 0 and smaller equal than length', | ||
| 'start index must be a integer greater than 0 and smaller equal than length', |
There was a problem hiding this comment.
| 'start index must be a integer greater than 0 and smaller equal than length', | |
| 'fromIndex must be a positive integer smaller than length', |
| if (toIndex === undefined) { | ||
| toIndex = input.length; | ||
| } else if ( |
There was a problem hiding this comment.
| if (toIndex === undefined) { | |
| toIndex = input.length; | |
| } else if ( | |
| if ( |
| if (toIndex === undefined) { | ||
| toIndex = input.length; | ||
| } else if ( | ||
| (toIndex <= fromIndex) | |
There was a problem hiding this comment.
| (toIndex <= fromIndex) | | |
| (toIndex <= fromIndex) || |
| toIndex = input.length; | ||
| } else if ( | ||
| (toIndex <= fromIndex) | | ||
| (toIndex > input.length) | |
There was a problem hiding this comment.
| (toIndex > input.length) | | |
| (toIndex > input.length) || |
| ) { | ||
| throw new Error( | ||
| 'to index must be greater equal than from index and smaller equal than length', | ||
| 'toIndex index must be a integer greater equal than fromIndex index and smaller equal than length', |
There was a problem hiding this comment.
| 'toIndex index must be a integer greater equal than fromIndex index and smaller equal than length', | |
| 'toIndex must be an integer greater than fromIndex and at most equal to length', |
There was a problem hiding this comment.
wow! many thanks for your time and careful review!
- incorporating feedback from mljs#11
- incorporating feedback from mljs#11
Hi, this is a PR to get feedback about the
from/tooptions to avoid slicing.In particular if
fromIndexas in some other methods?)tois exclusivemakes sense to you.
If it makes sense, I'll go forward and make the changes for all other methods were relevant such that we can use it for the baselines.