mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 03:27:34 +00:00
LibJS: Add Array.prototype.splice
Adds splice to Array.prototype according to the specification: https://tc39.es/ecma262/#sec-array.prototype.splice
This commit is contained in:
parent
6137475c39
commit
f9f7cb4583
4 changed files with 201 additions and 0 deletions
|
@ -27,6 +27,18 @@ try {
|
|||
assert(o.length === 0);
|
||||
});
|
||||
|
||||
{
|
||||
const o = { length: 3, 0: "hello", 2: "serenity" };
|
||||
const removed = Array.prototype.splice.call(o, 0, 2, "hello", "friends");
|
||||
assert(o.length === 3);
|
||||
assert(o[0] === "hello");
|
||||
assert(o[1] === "friends");
|
||||
assert(o[2] === "serenity");
|
||||
assert(removed.length === 2);
|
||||
assert(removed[0] === "hello");
|
||||
assert(removed[1] === undefined);
|
||||
}
|
||||
|
||||
{
|
||||
assert(Array.prototype.join.call({}) === "");
|
||||
assert(Array.prototype.join.call({ length: "foo" }) === "");
|
||||
|
|
87
Libraries/LibJS/Tests/Array.prototype.splice.js
Normal file
87
Libraries/LibJS/Tests/Array.prototype.splice.js
Normal file
|
@ -0,0 +1,87 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.splice.length === 2);
|
||||
|
||||
var array = ["hello", "friends", "serenity", 1, 2];
|
||||
var removed = array.splice(3);
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(removed.length === 2);
|
||||
assert(removed[0] === 1);
|
||||
assert(removed[1] === 2);
|
||||
|
||||
array = ["hello", "friends", "serenity", 1, 2];
|
||||
removed = array.splice(-2);
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(removed.length === 2);
|
||||
assert(removed[0] === 1);
|
||||
assert(removed[1] === 2);
|
||||
|
||||
array = ["hello", "friends", "serenity", 1, 2];
|
||||
removed = array.splice(-2, 1);
|
||||
assert(array.length === 4);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(array[3] === 2);
|
||||
assert(removed.length === 1);
|
||||
assert(removed[0] === 1);
|
||||
|
||||
array = ["serenity"];
|
||||
removed = array.splice(0, 0, "hello", "friends");
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(removed.length === 0);
|
||||
|
||||
array = ["goodbye", "friends", "serenity"];
|
||||
removed = array.splice(0, 1, "hello");
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(removed.length === 1);
|
||||
assert(removed[0] === "goodbye");
|
||||
|
||||
array = ["foo", "bar", "baz"];
|
||||
removed = array.splice();
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "foo");
|
||||
assert(array[1] === "bar");
|
||||
assert(array[2] === "baz");
|
||||
assert(removed.length === 0);
|
||||
|
||||
removed = array.splice(0, 123);
|
||||
assert(array.length === 0);
|
||||
assert(removed.length === 3);
|
||||
assert(removed[0] === "foo");
|
||||
assert(removed[1] === "bar");
|
||||
assert(removed[2] === "baz");
|
||||
|
||||
array = ["foo", "bar", "baz"];
|
||||
removed = array.splice(123, 123);
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "foo");
|
||||
assert(array[1] === "bar");
|
||||
assert(array[2] === "baz");
|
||||
assert(removed.length === 0);
|
||||
|
||||
array = ["foo", "bar", "baz"];
|
||||
removed = array.splice(-123, 123);
|
||||
assert(array.length === 0);
|
||||
assert(removed.length === 3);
|
||||
assert(removed[0] === "foo");
|
||||
assert(removed[1] === "bar");
|
||||
assert(removed[2] === "baz");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue