1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 15:17:36 +00:00

LibJS: Implement most of String.prototype.replace

This commit is contained in:
Timothy Flynn 2021-04-01 16:33:51 -04:00 committed by Andreas Kling
parent 96121ddb11
commit 77a601d52e
5 changed files with 308 additions and 0 deletions

View file

@ -0,0 +1,104 @@
test("invariants", () => {
expect(String.prototype.replace).toHaveLength(2);
});
test("error cases", () => {
[null, undefined].forEach(value => {
expect(() => {
value.replace("", "");
}).toThrow(TypeError);
});
});
test("basic string replacement", () => {
expect("".replace("", "")).toBe("");
expect("".replace("a", "")).toBe("");
expect("".replace("", "a")).toBe("a");
expect("a".replace("a", "")).toBe("");
expect("a".replace("a", "b")).toBe("b");
expect("aa".replace("a", "b")).toBe("ba");
expect("ca".replace("a", "b")).toBe("cb");
});
test("convertible string replacement", () => {
expect("123".replace(2, "x")).toBe("1x3");
expect("123".replace("2", 4)).toBe("143");
expect("123".replace(2, 4)).toBe("143");
});
test("functional string replacement", () => {
expect(
"a".replace("a", function () {
return "b";
})
).toBe("b");
expect("a".replace("a", () => "b")).toBe("b");
expect(
"abc".replace("b", (search, position, string) => {
expect(search).toBe("b");
expect(position).toBe(1);
expect(string).toBe("abc");
return "x";
})
).toBe("axc");
});
test("basic regex replacement", () => {
expect("".replace(/a/, "")).toBe("");
expect("a".replace(/a/, "")).toBe("");
expect("abc123def".replace(/\D/, "*")).toBe("*bc123def");
expect("123abc456".replace(/\D/, "*")).toBe("123*bc456");
expect("abc123def".replace(/\D/g, "*")).toBe("***123***");
expect("123abc456".replace(/\D/g, "*")).toBe("123***456");
});
test("functional regex replacement", () => {
expect(
"a".replace(/a/, function () {
return "b";
})
).toBe("b");
expect("a".replace(/a/, () => "b")).toBe("b");
expect(
"abc".replace(/\D/, (matched, position, string) => {
expect(matched).toBe("a");
expect(position).toBe(0);
expect(string).toBe("abc");
return "x";
})
).toBe("xbc");
expect(
"abc".replace(/\D/g, (matched, position, string) => {
expect(matched).toBe(string[position]);
expect(position <= 2).toBeTrue();
expect(string).toBe("abc");
return "x";
})
).toBe("xxx");
expect(
"abc".replace(/(\D)/g, (matched, capture1, position, string) => {
expect(matched).toBe(string[position]);
expect(capture1).toBe(string[position]);
expect(position <= 2).toBeTrue();
expect(string).toBe("abc");
return "x";
})
).toBe("xxx");
expect(
"abcd".replace(/(\D)b(\D)/g, (matched, capture1, capture2, position, string) => {
expect(matched).toBe("abc");
expect(capture1).toBe("a");
expect(capture2).toBe("c");
expect(position).toBe(0);
expect(string).toBe("abcd");
return "x";
})
).toBe("xd");
});