Skip to content

Commit

Permalink
Clarify the parameters of slice (#32152)
Browse files Browse the repository at this point in the history
* Clarify the parameters of slice

Some explanations are a bit confusing.

- The ranges of negative `start` and `end` overlap with another bullet points. Clarify them.
- "Normalization" used here is neither standardized or defined. Re phrase the sentence.

* fix others

---------

Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
  • Loading branch information
xuhdev and Josh-Cena committed Feb 8, 2024
1 parent 51b2806 commit 85d7482
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 22 deletions.
Expand Up @@ -22,21 +22,21 @@ copyWithin(target, start, end)

- `target`
- : Zero-based index at which to copy the sequence to, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion). This corresponds to where the element at `start` will be copied to, and all elements between `start` and `end` are copied to succeeding indices.
- Negative index counts back from the end of the array — if `target < 0`, `target + array.length` is used.
- Negative index counts back from the end of the array — if `-array.length <= target < 0`, `target + array.length` is used.
- If `target < -array.length`, `0` is used.
- If `target >= array.length`, nothing is copied.
- If `target` is positioned after `start` after normalization, copying only happens until the end of `array.length` (in other words, `copyWithin()` never extends the array).
- `start`
- : Zero-based index at which to start copying elements from, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion).
- Negative index counts back from the end of the array — if `start < 0`, `start + array.length` is used.
- Negative index counts back from the end of the array — if `-array.length <= start < 0`, `start + array.length` is used.
- If `start < -array.length`, `0` is used.
- If `start >= array.length`, nothing is copied.
- `end` {{optional_inline}}
- : Zero-based index at which to end copying elements from, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion). `copyWithin()` copies up to but not including `end`.
- Negative index counts back from the end of the array — if `end < 0`, `end + array.length` is used.
- Negative index counts back from the end of the array — if `-array.length <= end < 0`, `end + array.length` is used.
- If `end < -array.length`, `0` is used.
- If `end >= array.length` or `end` is omitted, `array.length` is used, causing all elements until the end to be copied.
- If `end` is positioned before or at `start` after normalization, nothing is copied.
- If `end` implies a position before or at the position that `start` implies, nothing is extracted.

### Return value

Expand Down
Expand Up @@ -25,15 +25,15 @@ fill(value, start, end)
- : Value to fill the array with. Note all elements in the array will be this exact value: if `value` is an object, each slot in the array will reference that object.
- `start` {{optional_inline}}
- : Zero-based index at which to start filling, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion).
- Negative index counts back from the end of the array — if `start < 0`, `start + array.length` is used.
- Negative index counts back from the end of the array — if `-array.length <= start < 0`, `start + array.length` is used.
- If `start < -array.length` or `start` is omitted, `0` is used.
- If `start >= array.length`, no index is filled.
- `end` {{optional_inline}}
- : Zero-based index at which to end filling, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion). `fill()` fills up to but not including `end`.
- Negative index counts back from the end of the array — if `end < 0`, `end + array.length` is used.
- Negative index counts back from the end of the array — if `-array.length <= end < 0`, `end + array.length` is used.
- If `end < -array.length`, `0` is used.
- If `end >= array.length` or `end` is omitted, `array.length` is used, causing all indices until the end to be filled.
- If `end` is positioned before or at `start` after normalization, no index is filled.
- If `end` implies a position before or at the position that `start` implies, nothing is extracted.

### Return value

Expand Down
Expand Up @@ -26,7 +26,7 @@ includes(searchElement, fromIndex)
- : The value to search for.
- `fromIndex` {{optional_inline}}
- : Zero-based index at which to start searching, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion).
- Negative index counts back from the end of the array — if `fromIndex < 0`, `fromIndex + array.length` is used. However, the array is still searched from front to back in this case.
- Negative index counts back from the end of the array — if `-array.length <= fromIndex < 0`, `fromIndex + array.length` is used. However, the array is still searched from front to back in this case.
- If `fromIndex < -array.length` or `fromIndex` is omitted, `0` is used, causing the entire array to be searched.
- If `fromIndex >= array.length`, the array is not searched and `false` is returned.

Expand Down
Expand Up @@ -25,7 +25,7 @@ indexOf(searchElement, fromIndex)
- : Element to locate in the array.
- `fromIndex` {{optional_inline}}
- : Zero-based index at which to start searching, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion).
- Negative index counts back from the end of the array — if `fromIndex < 0`, `fromIndex + array.length` is used. Note, the array is still searched from front to back in this case.
- Negative index counts back from the end of the array — if `-array.length <= fromIndex < 0`, `fromIndex + array.length` is used. Note, the array is still searched from front to back in this case.
- If `fromIndex < -array.length` or `fromIndex` is omitted, `0` is used, causing the entire array to be searched.
- If `fromIndex >= array.length`, the array is not searched and `-1` is returned.

Expand Down
Expand Up @@ -26,7 +26,7 @@ lastIndexOf(searchElement, fromIndex)
- : Element to locate in the array.
- `fromIndex` {{optional_inline}}
- : Zero-based index at which to start searching backwards, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion).
- Negative index counts back from the end of the array — if `fromIndex < 0`, `fromIndex + array.length` is used.
- Negative index counts back from the end of the array — if `-array.length <= fromIndex < 0`, `fromIndex + array.length` is used.
- If `fromIndex < -array.length`, the array is not searched and `-1` is returned. You can think of it conceptually as starting at a nonexistent position before the beginning of the array and going backwards from there. There are no array elements on the way, so `searchElement` is never found.
- If `fromIndex >= array.length` or `fromIndex` is omitted, `array.length - 1` is used, causing the entire array to be searched. You can think of it conceptually as starting at a nonexistent position beyond the end of the array and going backwards from there. It eventually reaches the real end position of the array, at which point it starts searching backwards through the actual array elements.

Expand Down
Expand Up @@ -26,15 +26,15 @@ slice(start, end)

- `start` {{optional_inline}}
- : Zero-based index at which to start extraction, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion).
- Negative index counts back from the end of the array — if `start < 0`, `start + array.length` is used.
- Negative index counts back from the end of the array — if `-array.length <= start < 0`, `start + array.length` is used.
- If `start < -array.length` or `start` is omitted, `0` is used.
- If `start >= array.length`, nothing is extracted.
- `end` {{optional_inline}}
- : Zero-based index at which to end extraction, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion). `slice()` extracts up to but not including `end`.
- Negative index counts back from the end of the array — if `end < 0`, `end + array.length` is used.
- Negative index counts back from the end of the array — if `-array.length <= end < 0`, `end + array.length` is used.
- If `end < -array.length`, `0` is used.
- If `end >= array.length` or `end` is omitted, `array.length` is used, causing all elements until the end to be extracted.
- If `end` is positioned before or at `start` after normalization, nothing is extracted.
- If `end` implies a position before or at the position that `start` implies, nothing is extracted.

### Return value

Expand Down
Expand Up @@ -29,7 +29,7 @@ splice(start, deleteCount, item1, item2, /* …, */ itemN)
- `start`

- : Zero-based index at which to start changing the array, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion).
- Negative index counts back from the end of the array — if `start < 0`, `start + array.length` is used.
- Negative index counts back from the end of the array — if `-array.length <= start < 0`, `start + array.length` is used.
- If `start < -array.length`, `0` is used.
- If `start >= array.length`, no element will be deleted, but the method will behave as an adding function, adding as many elements as provided.
- If `start` is omitted (and `splice()` is called with no arguments), nothing is deleted. This is different from passing `undefined`, which is converted to `0`.
Expand Down
Expand Up @@ -24,7 +24,7 @@ toSpliced(start, deleteCount, item1, item2, /* …, */ itemN)
- `start`

- : Zero-based index at which to start changing the array, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion).
- Negative index counts back from the end of the array — if `start < 0`, `start + array.length` is used.
- Negative index counts back from the end of the array — if `-array.length <= start < 0`, `start + array.length` is used.
- If `start < -array.length` or `start` is omitted, `0` is used.
- If `start >= array.length`, no element will be deleted, but the method will behave as an adding function, adding as many elements as provided.

Expand Down
Expand Up @@ -19,7 +19,7 @@ arrayInstance.with(index, value)

- `index`
- : Zero-based index at which to change the array, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion).
- Negative index counts back from the end of the array — if `index < 0`, `index + array.length` is used.
- Negative index counts back from the end of the array — if `-array.length <= index < 0`, `index + array.length` is used.
- If the index after normalization is out of bounds, a {{jsxref("RangeError")}} is thrown.
- `value`
- : Any value to be assigned to the given index.
Expand Down
Expand Up @@ -23,15 +23,15 @@ slice(start, end)

- `start` {{optional_inline}}
- : Zero-based index at which to start extraction, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion).
- Negative index counts back from the end of the buffer — if `start < 0`, `start + buffer.length` is used.
- Negative index counts back from the end of the buffer — if `-buffer.length <= start < 0`, `start + buffer.length` is used.
- If `start < -buffer.length` or `start` is omitted, `0` is used.
- If `start >= buffer.length`, nothing is extracted.
- `end` {{optional_inline}}
- : Zero-based index at which to end extraction, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion). `slice()` extracts up to but not including `end`.
- Negative index counts back from the end of the buffer — if `end < 0`, `end + buffer.length` is used.
- Negative index counts back from the end of the buffer — if `-buffer.length <= end < 0`, `end + buffer.length` is used.
- If `end < -buffer.length`, `0` is used.
- If `end >= buffer.length` or `end` is omitted, `buffer.length` is used, causing all elements until the end to be extracted.
- If `end` is positioned before or at `start` after normalization, nothing is extracted.
- If `end` implies a position before or at the position that `start` implies, nothing is extracted.

### Return value

Expand Down
Expand Up @@ -23,15 +23,15 @@ slice(start, end)

- `start` {{optional_inline}}
- : Zero-based index at which to start extraction, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion).
- Negative index counts back from the end of the buffer — if `start < 0`, `start + buffer.length` is used.
- Negative index counts back from the end of the buffer — if `-buffer.length <= start < 0`, `start + buffer.length` is used.
- If `start < -buffer.length` or `start` is omitted, `0` is used.
- If `start >= buffer.length`, nothing is extracted.
- `end` {{optional_inline}}
- : Zero-based index at which to end extraction, [converted to an integer](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion). `slice()` extracts up to but not including `end`.
- Negative index counts back from the end of the buffer — if `end < 0`, `end + buffer.length` is used.
- Negative index counts back from the end of the buffer — if `-buffer.length <= end < 0`, `end + buffer.length` is used.
- If `end < -buffer.length`, `0` is used.
- If `end >= buffer.length` or `end` is omitted, `buffer.length` is used, causing all elements until the end to be extracted.
- If `end` is positioned before or at `start` after normalization, nothing is extracted.
- If `end` implies a position before or at the position that `start` implies, nothing is extracted.

### Return value

Expand Down

0 comments on commit 85d7482

Please sign in to comment.