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
19 changes: 17 additions & 2 deletions src/generics/lib.generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3428,7 +3428,9 @@ class Map<K = any, V = any> {
if (this._entries[i].next >= -1) {
const key = this._entries[i].key;
const value = this._entries[i].value;
yield <[key: K, value: V]>[key, value];
// `[key, value]`, as in JavaScript: a tuple without labels, which JSON.stringify writes as an array
// (a labelled one was `{"key": ..., "value": ...}`)
yield <[K, V]>[key, value];
}
}
}
Expand Down Expand Up @@ -3556,9 +3558,10 @@ class Set<V = any> {
return this.removeValue(v);
}

// `[value, value]` for each value, as in JavaScript (a set's key is its value): it gave the values alone
@borrows("this")
entries() {
return this.iter();
return this.iterEntries();
}

@borrows("this")
Expand Down Expand Up @@ -3866,6 +3869,18 @@ class Set<V = any> {
}
}
}

@borrows("this")
private *iterEntries() {
for (let i = 0; i < this.count; i++) {
if (this._entries[i].next >= -1) {
// (two reads of the field, as Map's two: one value put into the tuple twice crashed under -mm=own)
const key = this._entries[i].value;
const value = this._entries[i].value;
yield <[V, V]>[key, value];
}
}
}
}

// The value of a string index signature, `{ [key: string]: T }`, and of an object literal with a key known
Expand Down
2 changes: 1 addition & 1 deletion tests/map_entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ const map1 = new Map();
map1.set('0', 'foo');
map1.set(1, 'bar');

for (const entry of map1.entries()) console.log(`entry: ${entry.key} = ${entry.value}`);
for (const entry of map1.entries()) console.log(`entry: ${entry[0]} = ${entry[1]}`);

console.log("ALL DONE");
14 changes: 14 additions & 0 deletions tests/set_entries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Set.entries() yields `[value, value]` for each value, as in JavaScript, and Map.entries() `[key, value]` tuples
// that JSON.stringify writes as arrays
const set1 = new Set<string>();
set1.add("x" + 1);
set1.add("y" + 2);
for (const entry of set1.entries()) console.log(`entry: ${entry[0]} = ${entry[1]}`);
for (const [a, b] of set1.entries()) console.log(a == b ? "same" : "different");

const map1 = new Map<string, number>();
map1.set("a", 1);
map1.set("b", 2);
console.log(JSON.stringify([...map1.entries()]));

console.log("ALL DONE");
Loading