From 9340fd51e48576a75adc919bff65dbc4a5d4c99b Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Wed, 4 Oct 2023 14:51:14 -0700 Subject: [PATCH] Lazy emojiRegex --- deno/lib/types.ts | 6 +++++- src/types.ts | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/deno/lib/types.ts b/deno/lib/types.ts index 80939da3b..673ab1ec9 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -570,7 +570,8 @@ const emailRegex = // /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i; // from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression -const emojiRegex = /^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u; +const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`; +let emojiRegex: RegExp; const ipv4Regex = /^(((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 { status.dirty(); } } else if (check.kind === "emoji") { + if (!emojiRegex) { + emojiRegex = new RegExp(_emojiRegex, "u"); + } if (!emojiRegex.test(input.data)) { ctx = this._getOrReturnCtx(input, ctx); addIssueToContext(ctx, { diff --git a/src/types.ts b/src/types.ts index 20bb60dae..567936b76 100644 --- a/src/types.ts +++ b/src/types.ts @@ -570,7 +570,8 @@ const emailRegex = // /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i; // from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression -const emojiRegex = /^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u; +const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`; +let emojiRegex: RegExp; const ipv4Regex = /^(((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 { status.dirty(); } } else if (check.kind === "emoji") { + if (!emojiRegex) { + emojiRegex = new RegExp(_emojiRegex, "u"); + } if (!emojiRegex.test(input.data)) { ctx = this._getOrReturnCtx(input, ctx); addIssueToContext(ctx, {