Skip to content

Add list slice#706

Open
jakearchibald wants to merge 4 commits intowhatwg:mainfrom
jakearchibald:slice
Open

Add list slice#706
jakearchibald wants to merge 4 commits intowhatwg:mainfrom
jakearchibald:slice

Conversation

@jakearchibald
Copy link
Copy Markdown
Contributor

For whatwg/html#12345.

I also changed the definition of clone, since it's just a slice with default args.

infra.bs Outdated
<dfn export for=list/slice><var>to</var></dfn> (default |list|'s <a for=list>size</a>), is to create a new
<a>list</a> |slice|, of the same designation, and <a for=list>append</a> each
<a for=list>item</a> of |list|, where the index is greater than or equal to |from| and less than
|to|, to |slice|, maintaining order.
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.

I think we should make this a list of steps for clarity. That way we can also assert the obvious things about from and to.

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.

Yeah that's fair.

Are you ok with clone being defined in terms of slice?

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.

Yeah that seems good. Always nice to have fewer fundamental primitives.

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.

Done!

@jakearchibald jakearchibald requested a review from annevk April 10, 2026 10:27
Comment on lines +1673 to +1675
<li><p>If |from| is less than 0, then set |from| to 0.

<li><p>If |to| is greater than |list|'s <a for=list>size</a>, then set |to| to |list|'s <a for=list>size</a>.
Copy link
Copy Markdown
Contributor Author

@jakearchibald jakearchibald Apr 10, 2026

Choose a reason for hiding this comment

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

I felt this was preferable to asserting. It means, like in JS, you can slice from 0 to 100 to 'crop' a list to maximum of 100 items.

['foo'].slice(0, 100); // does not throw

Comment on lines +1673 to +1675
<li><p>If |from| is less than 0, then set |from| to 0.

<li><p>If |to| is greater than |list|'s <a for=list>size</a>, then set |to| to |list|'s <a for=list>size</a>.
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.

Is this needed for specifications? I might not be considering all scenarios that well, but I was assuming we'd just want to require both of these to be within bounds of the list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants