From b92850b6d8761bd659fc6438622fbfb3c68e29a4 Mon Sep 17 00:00:00 2001 From: Mitsunee <19474909+Mitsunee@users.noreply.github.com> Date: Wed, 22 Jan 2025 14:05:19 +0100 Subject: [PATCH 1/3] add: implement symbol iterator method --- src/index.ts | 31 ++++++++++++++++++++++++ tests/symbol-iterator.test.ts | 45 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 tests/symbol-iterator.test.ts diff --git a/src/index.ts b/src/index.ts index 865f7dd..8a92c01 100644 --- a/src/index.ts +++ b/src/index.ts @@ -764,4 +764,35 @@ export class List { toString() { return this.join(","); } + + *[Symbol.iterator]() { + const seenNodes = new Set>(); + let curr = this.#head; + let idx = 0; + + while (idx < this.length) { + if (curr) { + yield curr.value; + seenNodes.add(curr); + curr = curr.next; + idx++; + continue; + } + + if (idx >= this.length) return; + + // find current element assuming list got modified + let temp = this.#head; + let i = 0; + while (temp && i < idx) { + temp = temp.next; + i++; + } + + if (temp && i == idx) { + curr = temp; + continue; + } + } + } } diff --git a/tests/symbol-iterator.test.ts b/tests/symbol-iterator.test.ts new file mode 100644 index 0000000..f0e5017 --- /dev/null +++ b/tests/symbol-iterator.test.ts @@ -0,0 +1,45 @@ +import { test } from "uvu"; +import * as assert from "uvu/assert"; +import { List } from "../src"; + +test("it iterates over basic list", () => { + const list = new List([1, 2, 3, 4, 5, 6, 7, 8, 9]); + let i = 1; + for (const val of list) { + assert.is(val, i); + i++; + } +}); + +test("it continues to iterate after splice", () => { + const subjectArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]; + const subjectList = new List(subjectArray); + const outputArray = new Array(); + const outputList = new Array(); + + // array + for (const v of subjectArray) { + if (v == 4) subjectArray.splice(3, 3); + outputArray.push(v); + } + + // list + for (const v of subjectList) { + if (v == 4) subjectList.remove(3, 3); + outputList.push(v); + } + + try { + assert.equal(outputList, outputArray); + } catch (e) { + console.log({ + subjectArray: subjectArray.join(", "), + subjectList: subjectList.join(", "), + outputArray: outputArray.join(", "), + outputList: outputList.join(", ") + }); + throw e; + } +}); + +test.run(); From 1db4d9f6063a77ae4f4c6efe1e3ba0571a36b0c3 Mon Sep 17 00:00:00 2001 From: Mitsunee <19474909+Mitsunee@users.noreply.github.com> Date: Wed, 22 Jan 2025 14:08:25 +0100 Subject: [PATCH 2/3] change: remove entries method --- docs/methods/with-other-objects.md | 13 ------------- src/index.ts | 9 --------- 2 files changed, 22 deletions(-) diff --git a/docs/methods/with-other-objects.md b/docs/methods/with-other-objects.md index e081bdf..1eda6a4 100644 --- a/docs/methods/with-other-objects.md +++ b/docs/methods/with-other-objects.md @@ -53,16 +53,3 @@ const newList = list.concat(["baz", "foobar"]); list.toString(); // "foo,bar" newList.toString(); // "foo,bar,baz,foobar" ``` - -## `entries` - -Creates iterable of index, value pairs for every entry in the List. - -```js -const list = List.fromArray(["foo", "bar"]); -for (const [i, v] of list.entries()) { - console.log(`Index: ${i}, Value: ${v}`); - // Index: 0, Value: foo - // Index: 1, Value: bar -} -``` diff --git a/src/index.ts b/src/index.ts index 8a92c01..409d259 100644 --- a/src/index.ts +++ b/src/index.ts @@ -469,15 +469,6 @@ export class List { return newList; } - /** - * Creates iterable of index, value pairs for every entry in the List. - * @returns IterableIterator - */ - // not tested as this is basically a proxy of the existing Array method - entries() { - return this.toArray().entries(); - } - /** * Checks if a test callback returns `true` for every value * @param callback Test callback From 88382254f3a7ba999e1928b97e5879096b6c4253 Mon Sep 17 00:00:00 2001 From: Mitsunee <19474909+Mitsunee@users.noreply.github.com> Date: Wed, 22 Jan 2025 14:11:57 +0100 Subject: [PATCH 3/3] refactor: make use of iteratior in toArray, clone and sort --- src/index.ts | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/index.ts b/src/index.ts index 409d259..48f76ef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -324,15 +324,7 @@ export class List { * @returns Array */ toArray(): T[] { - const arr = new Array(); - - let curr = this.#head; - while (curr) { - arr.push(curr.value); - curr = curr.next; - } - - return arr; + return Array.from(this); } /** @@ -445,12 +437,7 @@ export class List { * @returns new List */ clone(): List { - const newList = new List(); - let curr = this.#head; - while (curr) { - newList.push(curr.value); - curr = curr.next; - } + const newList = new List(this); return newList; } @@ -723,7 +710,7 @@ export class List { */ sort(callback?: (a: T, b: T) => number): List { const arr = this.toArray().sort(callback); - return List.fromArray(arr); + return new List(arr); } /**