Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F4444354
test_object_fromentries_iterables.cjs
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
test_object_fromentries_iterables.cjs
View Options
const assert = require("node:assert");
assert.deepStrictEqual(
Object.fromEntries(new Map([
["a", 1],
["b", 2],
])),
{ a: 1, b: 2 },
);
function* pairGenerator() {
yield ["x", 10];
yield ["y", 20];
}
assert.deepStrictEqual(
Object.fromEntries(pairGenerator()),
{ x: 10, y: 20 },
);
const iterable = {
[Symbol.iterator]: function* () {
yield ["left", "right"];
yield ["up", "down"];
},
};
assert.deepStrictEqual(
Object.fromEntries(iterable),
{ left: "right", up: "down" },
);
class RequestContextLike {
constructor() {
this.registry = new Map();
}
set(key, value) {
this.registry.set(key, value);
}
entries() {
return this.registry.entries();
}
}
const ctx = new RequestContextLike();
ctx.set("harness", {
state: { projectPath: "/tmp/project" },
getState() {
return this.state;
},
});
const snapshot = Object.fromEntries(ctx.entries());
assert.strictEqual(snapshot.harness.getState().projectPath, "/tmp/project");
let invalidEntryError;
try {
Object.fromEntries((function* () { yield 1; })());
} catch (error) {
invalidEntryError = error;
}
assert.ok(invalidEntryError instanceof Error);
assert.match(String(invalidEntryError), /requires an iterable|entry object/);
console.log("Object.fromEntries consumes Map iterators, generators, and custom iterables");
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Sat, May 2, 10:16 AM (2 d)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
544129
Default Alt Text
test_object_fromentries_iterables.cjs (1 KB)
Attached To
Mode
rANT Ant
Attached
Detach File
Event Timeline
Log In to Comment