Skip to content

Commit 9340fd5

Browse files
committedOct 4, 2023
Lazy emojiRegex
1 parent 7cb4ba2 commit 9340fd5

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed
 

‎deno/lib/types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,8 @@ const emailRegex =
570570
// /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
571571

572572
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
573-
const emojiRegex = /^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u;
573+
const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
574+
let emojiRegex: RegExp;
574575

575576
const ipv4Regex =
576577
/^(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))$/;
@@ -710,6 +711,9 @@ export class ZodString extends ZodType<string, ZodStringDef> {
710711
status.dirty();
711712
}
712713
} else if (check.kind === "emoji") {
714+
if (!emojiRegex) {
715+
emojiRegex = new RegExp(_emojiRegex, "u");
716+
}
713717
if (!emojiRegex.test(input.data)) {
714718
ctx = this._getOrReturnCtx(input, ctx);
715719
addIssueToContext(ctx, {

‎src/types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,8 @@ const emailRegex =
570570
// /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
571571

572572
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
573-
const emojiRegex = /^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u;
573+
const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
574+
let emojiRegex: RegExp;
574575

575576
const ipv4Regex =
576577
/^(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))$/;
@@ -710,6 +711,9 @@ export class ZodString extends ZodType<string, ZodStringDef> {
710711
status.dirty();
711712
}
712713
} else if (check.kind === "emoji") {
714+
if (!emojiRegex) {
715+
emojiRegex = new RegExp(_emojiRegex, "u");
716+
}
713717
if (!emojiRegex.test(input.data)) {
714718
ctx = this._getOrReturnCtx(input, ctx);
715719
addIssueToContext(ctx, {

0 commit comments

Comments
 (0)
Please sign in to comment.