Skip to content

Commit

Permalink
chore: drop support index -1
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Sep 19, 2023
1 parent b8243dc commit 04d8d41
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
10 changes: 2 additions & 8 deletions packages/ssz/src/viewDU/listBasic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,10 @@ export class ListBasicTreeViewDU<ElementType extends BasicType<unknown>> extends
* Returns a new ListBasicTreeViewDU instance with the values from 0 to `index`.
* To achieve it, rebinds the underlying tree zero-ing all nodes right of `chunkIindex`.
* Also set all value right of `index` in the same chunk to 0.
*
* Note: Using index = -1, returns an empty list of length 0.
*/
sliceTo(index: number): this {
if (index < -1) {
throw new Error("Does not support sliceTo() with index less than -1");
}

if (index === -1) {
return this.type.defaultViewDU() as this;
if (index < 0) {
throw new Error(`Does not support sliceTo() with negative index ${index}`);
}

// Commit before getting rootNode to ensure all pending data is in the rootNode
Expand Down
10 changes: 3 additions & 7 deletions packages/ssz/test/unit/byType/listBasic/tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,13 @@ describe("ListBasicType.sliceTo", () => {
const listRoots: string[] = [];
const listSerialized: string[] = [];

for (let i = -1; i < 16; i++) {
if (i >= 0) {
listView.push(i);
}
// Javascript arrays can handle negative indexes (ok for tests)
for (let i = 0; i < 16; i++) {
listView.push(i);
listSerialized[i] = toHexString(listView.serialize());
listRoots[i] = toHexString(listView.hashTreeRoot());
}

// Start at -1 to test the empty case.
for (let i = -1; i < 16; i++) {
for (let i = 0; i < 16; i++) {
const listSlice = listView.sliceTo(i);
expect(listSlice.length).to.equal(i + 1, `Wrong length at .sliceTo(${i})`);
expect(toHexString(listSlice.serialize())).equals(listSerialized[i], `Wrong serialize at .sliceTo(${i})`);
Expand Down

0 comments on commit 04d8d41

Please sign in to comment.