diff --git a/packages/ssz/src/viewDU/listBasic.ts b/packages/ssz/src/viewDU/listBasic.ts index d269fd76..05bcd06c 100644 --- a/packages/ssz/src/viewDU/listBasic.ts +++ b/packages/ssz/src/viewDU/listBasic.ts @@ -42,16 +42,10 @@ export class ListBasicTreeViewDU> 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 diff --git a/packages/ssz/test/unit/byType/listBasic/tree.test.ts b/packages/ssz/test/unit/byType/listBasic/tree.test.ts index 719e61ff..e6b15672 100644 --- a/packages/ssz/test/unit/byType/listBasic/tree.test.ts +++ b/packages/ssz/test/unit/byType/listBasic/tree.test.ts @@ -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})`);