Skip to content

[WIP - feedback request] chore: max accepts from/to option#11

Closed
kjappelbaum wants to merge 0 commit intomljs:masterfrom
kjappelbaum:master
Closed

[WIP - feedback request] chore: max accepts from/to option#11
kjappelbaum wants to merge 0 commit intomljs:masterfrom
kjappelbaum:master

Conversation

@kjappelbaum
Copy link
Copy Markdown
Contributor

Hi, this is a PR to get feedback about the from/to options to avoid slicing.

In particular if

  • the naming is fine by you (or shall we better use fromIndex as in some other methods?)
  • the choice that to is exclusive

makes 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.

@maasencioh
Copy link
Copy Markdown
Member

I like the from-to notation, seems clear enough for me

Comment thread packages/array-max/src/index.js Outdated
*/
export default function max(input) {
export default function max(input, options = {}) {
let { from, to } = options;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let { from, to } = options;
let { from = 0, to = input.length } = options;
  • Move this line after the validation of input to avoid errors if that is not an array
  • You can then remove the "if undefined" conditions below

Comment thread packages/array-max/src/index.js Outdated
/**
* 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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @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

Comment thread packages/array-max/src/index.js Outdated
for (let i = 1; i < input.length; i++) {
if (from === undefined) {
from = 0;
} else if ((from < 0) | (from > input.length)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also check Number.isInteger(from). same for to

Comment thread packages/array-max/src/index.js Outdated
if (input.length === 0) {
throw new TypeError('input must not be empty');
}
let { fromIndex, toIndex } = options;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let { fromIndex, toIndex } = options;
const { fromIndex = 0, toIndex = input.length } = options;

Comment thread packages/array-max/src/index.js Outdated
Comment on lines +20 to +22
if (fromIndex === undefined) {
fromIndex = 0;
} else if (
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (fromIndex === undefined) {
fromIndex = 0;
} else if (
if (

Comment thread packages/array-max/src/index.js Outdated
if (fromIndex === undefined) {
fromIndex = 0;
} else if (
(fromIndex < 0) |
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(fromIndex < 0) |
(fromIndex < 0) ||

Comment thread packages/array-max/src/index.js Outdated
fromIndex = 0;
} else if (
(fromIndex < 0) |
(fromIndex > input.length) |
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(fromIndex > input.length) |
(fromIndex >= input.length) ||

fromIndex cannot be === input.length because that would leave no room for toIndex

Comment thread packages/array-max/src/index.js Outdated
) {
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',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'start index must be a integer greater than 0 and smaller equal than length',
'fromIndex must be a positive integer smaller than length',

Comment thread packages/array-max/src/index.js Outdated
Comment on lines +32 to +34
if (toIndex === undefined) {
toIndex = input.length;
} else if (
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (toIndex === undefined) {
toIndex = input.length;
} else if (
if (

Comment thread packages/array-max/src/index.js Outdated
if (toIndex === undefined) {
toIndex = input.length;
} else if (
(toIndex <= fromIndex) |
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(toIndex <= fromIndex) |
(toIndex <= fromIndex) ||

Comment thread packages/array-max/src/index.js Outdated
toIndex = input.length;
} else if (
(toIndex <= fromIndex) |
(toIndex > input.length) |
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(toIndex > input.length) |
(toIndex > input.length) ||

Comment thread packages/array-max/src/index.js Outdated
) {
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',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'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',

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow! many thanks for your time and careful review!

@kjappelbaum kjappelbaum closed this Oct 5, 2020
kjappelbaum pushed a commit to kjappelbaum/array that referenced this pull request Oct 5, 2020
kjappelbaum pushed a commit to kjappelbaum/array that referenced this pull request Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants