mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:18:11 +00:00
LibJS: Add spreading in object literals
Supports spreading strings, arrays, and other objects within object literals.
This commit is contained in:
parent
3cc31ff3f3
commit
104969a9f5
4 changed files with 116 additions and 0 deletions
72
Libraries/LibJS/Tests/object-spread.js
Normal file
72
Libraries/LibJS/Tests/object-spread.js
Normal file
|
@ -0,0 +1,72 @@
|
|||
load("test-common.js");
|
||||
|
||||
function testObjSpread(obj) {
|
||||
return obj.foo === 0 &&
|
||||
obj.bar === 1 &&
|
||||
obj.baz === 2 &&
|
||||
obj.qux === 3;
|
||||
}
|
||||
|
||||
function testObjStrSpread(obj) {
|
||||
return obj[0] === "a" &&
|
||||
obj[1] === "b" &&
|
||||
obj[2] === "c" &&
|
||||
obj[3] === "d";
|
||||
}
|
||||
|
||||
try {
|
||||
let obj = {
|
||||
foo: 0,
|
||||
...{ bar: 1, baz: 2 },
|
||||
qux: 3,
|
||||
};
|
||||
assert(testObjSpread(obj));
|
||||
|
||||
obj = { foo: 0, bar: 1, baz: 2 };
|
||||
obj.qux = 3;
|
||||
assert(testObjSpread({ ...obj }));
|
||||
|
||||
let a = { bar: 1, baz: 2 };
|
||||
obj = { foo: 0, ...a, qux: 3 };
|
||||
assert(testObjSpread(obj));
|
||||
|
||||
obj = {
|
||||
...{},
|
||||
...{
|
||||
...{ foo: 0, bar: 1, baz: 2 },
|
||||
},
|
||||
qux: 3,
|
||||
};
|
||||
assert(testObjSpread(obj));
|
||||
|
||||
obj = { ..."abcd" };
|
||||
assert(testObjStrSpread(obj));
|
||||
|
||||
obj = { ...["a", "b", "c", "d"] };
|
||||
assert(testObjStrSpread(obj));
|
||||
|
||||
obj = { ...String("abcd") };
|
||||
assert(testObjStrSpread(obj));
|
||||
|
||||
a = { foo: 0 };
|
||||
Object.defineProperty(a, 'bar', {
|
||||
value: 1,
|
||||
enumerable: false,
|
||||
});
|
||||
obj = { ...a };
|
||||
assert(obj.foo === 0 && obj.bar === undefined);
|
||||
|
||||
let empty = ({
|
||||
...undefined,
|
||||
...null,
|
||||
...1,
|
||||
...true,
|
||||
...function(){},
|
||||
...Date,
|
||||
});
|
||||
assert(Object.getOwnPropertyNames(empty).length === 0);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue