Skip to content

Commit 2816f32

Browse files
committed
chore: tweaks
1 parent ce9de89 commit 2816f32

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/constructs/__tests__/unicode.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ test('`unicodeChar` nesting matching', () => {
8181
).not.toMatchString('b');
8282
});
8383

84+
test('`unicodeChar` edge cases handling', () => {
85+
expect(() => u(unicodeChar(NaN))).toThrowErrorMatchingInlineSnapshot(
86+
`""unicodeChar": expected valid unicode code point but got: NaN"`,
87+
);
88+
expect(() => u(unicodeChar(1.5))).toThrowErrorMatchingInlineSnapshot(
89+
`""unicodeChar": expected valid unicode code point but got: 1.5"`,
90+
);
91+
expect(() => u(unicodeChar(-1))).toThrowErrorMatchingInlineSnapshot(
92+
`""unicodeChar": expected valid unicode code point but got: -1"`,
93+
);
94+
expect(() => u(unicodeChar(0x110000))).toThrowErrorMatchingInlineSnapshot(
95+
`""unicodeChar": expected valid unicode code point but got: 1114112"`,
96+
);
97+
98+
expect(u(unicodeChar(0x10ffff))).toEqualRegex(/\u{10ffff}/u);
99+
});
100+
84101
test('`unicodeProp` pattern', () => {
85102
expect(unicodeProp('General_Category', 'Letter')).toEqualRegex(/\p{General_Category=Letter}/);
86103
expect(unicodeProp('Letter')).toEqualRegex(/\p{Letter}/);

src/constructs/unicode.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@ import type { CharacterEscape } from '../types';
1313
* @returns A character class representing the unicode escape.
1414
*/
1515
export function unicodeChar(codePoint: number): CharacterEscape {
16-
if (!Number.isInteger(codePoint)) {
17-
throw new TypeError('Expected an integer code point but got: ' + codePoint);
18-
}
19-
20-
if (codePoint < 0) {
21-
throw new RangeError('Code point must be a positive integer but got: ' + codePoint);
16+
if (!Number.isInteger(codePoint) || codePoint < 0 || codePoint > 0x10ffff) {
17+
throw new RangeError('"unicodeChar": expected valid unicode code point but got: ' + codePoint);
2218
}
2319

2420
let escape =

0 commit comments

Comments
 (0)