1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 03:25:09 +00:00

LibJS: Implement String.prototype.split

This adds a String.prototype.split implementation modelled after 
ECMA262 specification. 

Additionally, `Value::to_u32` was added as an implementation of
the standard `ToUint32` abstract operation.

There is a tiny kludge for when the separator is an empty string. 
Basic tests and visiting google.com prove that this is working.
This commit is contained in:
Marcin Gasperowicz 2021-01-09 21:52:47 +01:00 committed by Andreas Kling
parent b53664a8ef
commit b24ce0b5ee
5 changed files with 140 additions and 0 deletions

View file

@ -0,0 +1,35 @@
test("basic functionality", () => {
expect(String.prototype.split).toHaveLength(2);
expect("hello friends".split()).toEqual(["hello friends"]);
expect("hello friends".split("")).toEqual([
"h",
"e",
"l",
"l",
"o",
" ",
"f",
"r",
"i",
"e",
"n",
"d",
"s",
]);
expect("hello friends".split(" ")).toEqual(["hello", "friends"]);
expect("a,b,c,d".split(",")).toEqual(["a", "b", "c", "d"]);
expect(",a,b,c,d".split(",")).toEqual(["", "a", "b", "c", "d"]);
expect("a,b,c,d,".split(",")).toEqual(["a", "b", "c", "d", ""]);
expect("a,b,,c,d".split(",")).toEqual(["a", "b", "", "c", "d"]);
expect(",a,b,,c,d,".split(",")).toEqual(["", "a", "b", "", "c", "d", ""]);
expect(",a,b,,,c,d,".split(",,")).toEqual([",a,b", ",c,d,"]);
});
test("limits", () => {
expect("a b c d".split(" ", 0)).toEqual([]);
expect("a b c d".split(" ", 1)).toEqual(["a"]);
expect("a b c d".split(" ", 3)).toEqual(["a", "b", "c"]);
expect("a b c d".split(" ", 100)).toEqual(["a", "b", "c", "d"]);
});