Skip to content

Commit

Permalink
chore: tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski committed May 6, 2024
1 parent ce9de89 commit 2816f32
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/constructs/__tests__/unicode.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ test('`unicodeChar` nesting matching', () => {
).not.toMatchString('b');
});

test('`unicodeChar` edge cases handling', () => {
expect(() => u(unicodeChar(NaN))).toThrowErrorMatchingInlineSnapshot(
`""unicodeChar": expected valid unicode code point but got: NaN"`,
);
expect(() => u(unicodeChar(1.5))).toThrowErrorMatchingInlineSnapshot(
`""unicodeChar": expected valid unicode code point but got: 1.5"`,
);
expect(() => u(unicodeChar(-1))).toThrowErrorMatchingInlineSnapshot(
`""unicodeChar": expected valid unicode code point but got: -1"`,
);
expect(() => u(unicodeChar(0x110000))).toThrowErrorMatchingInlineSnapshot(
`""unicodeChar": expected valid unicode code point but got: 1114112"`,
);

expect(u(unicodeChar(0x10ffff))).toEqualRegex(/\u{10ffff}/u);
});

test('`unicodeProp` pattern', () => {
expect(unicodeProp('General_Category', 'Letter')).toEqualRegex(/\p{General_Category=Letter}/);
expect(unicodeProp('Letter')).toEqualRegex(/\p{Letter}/);
Expand Down
8 changes: 2 additions & 6 deletions src/constructs/unicode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ import type { CharacterEscape } from '../types';
* @returns A character class representing the unicode escape.
*/
export function unicodeChar(codePoint: number): CharacterEscape {
if (!Number.isInteger(codePoint)) {
throw new TypeError('Expected an integer code point but got: ' + codePoint);
}

if (codePoint < 0) {
throw new RangeError('Code point must be a positive integer but got: ' + codePoint);
if (!Number.isInteger(codePoint) || codePoint < 0 || codePoint > 0x10ffff) {
throw new RangeError('"unicodeChar": expected valid unicode code point but got: ' + codePoint);
}

let escape =
Expand Down

0 comments on commit 2816f32

Please sign in to comment.