Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions docs/methods/with-other-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```
59 changes: 34 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,7 @@ export class List<T> {
* @returns Array
*/
toArray(): T[] {
const arr = new Array<T>();

let curr = this.#head;
while (curr) {
arr.push(curr.value);
curr = curr.next;
}

return arr;
return Array.from(this);
}

/**
Expand Down Expand Up @@ -445,12 +437,7 @@ export class List<T> {
* @returns new List
*/
clone(): List<T> {
const newList = new List<T>();
let curr = this.#head;
while (curr) {
newList.push(curr.value);
curr = curr.next;
}
const newList = new List<T>(this);
return newList;
}

Expand All @@ -469,15 +456,6 @@ export class List<T> {
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
Expand Down Expand Up @@ -732,7 +710,7 @@ export class List<T> {
*/
sort(callback?: (a: T, b: T) => number): List<T> {
const arr = this.toArray().sort(callback);
return List.fromArray(arr);
return new List(arr);
}

/**
Expand Down Expand Up @@ -764,4 +742,35 @@ export class List<T> {
toString() {
return this.join(",");
}

*[Symbol.iterator]() {
const seenNodes = new Set<ListNode<T>>();
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;
}
}
}
}
45 changes: 45 additions & 0 deletions tests/symbol-iterator.test.ts
Original file line number Diff line number Diff line change
@@ -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<number>();
const outputList = new Array<number>();

// 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();
Loading