LibJS: Implement String.prototype.codePointAt with UTF-16 code units

This also implements the CodePointAt abstract operation. This is needed
to handle invalid code units specific to the JavaScript spec, rather
than e.g. inserting replacement code units. This abstraction is public
because RegExp.prototype will also need it.
This commit is contained in:
Timothy Flynn 2021-07-19 11:34:29 -04:00 committed by Andreas Kling
commit a05ce330b8
Notes: sideshowbarker 2024-07-18 08:36:00 +09:00
3 changed files with 68 additions and 8 deletions

View file

@ -0,0 +1,29 @@
test("basic functionality", () => {
expect(String.prototype.charAt).toHaveLength(1);
var s = "Foobar";
expect(typeof s).toBe("string");
expect(s).toHaveLength(6);
expect(s.codePointAt(0)).toBe(70);
expect(s.codePointAt(1)).toBe(111);
expect(s.codePointAt(2)).toBe(111);
expect(s.codePointAt(3)).toBe(98);
expect(s.codePointAt(4)).toBe(97);
expect(s.codePointAt(5)).toBe(114);
expect(s.codePointAt(6)).toBe(undefined);
expect(s.codePointAt(-1)).toBe(undefined);
expect(s.codePointAt()).toBe(70);
expect(s.codePointAt(NaN)).toBe(70);
expect(s.codePointAt("foo")).toBe(70);
expect(s.codePointAt(undefined)).toBe(70);
});
test("UTF-16", () => {
var s = "😀";
expect(s).toHaveLength(2);
expect(s.codePointAt(0)).toBe(0x1f600);
expect(s.codePointAt(1)).toBe(0xde00);
expect(s.codePointAt(2)).toBe(undefined);
});