From 81d0c3217e7d4c282cd1d9c9e2417b35a4e81b9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=95=B7=EF=B8=8F?= <3756473+zendive@users.noreply.github.com> Date: Mon, 24 Feb 2025 08:37:49 +0200 Subject: [PATCH] implement support RegExp type #28 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: đŸ•·ïž <3756473+zendive@users.noreply.github.com> --- src/api/clone.ts | 27 ++++++++++++++++++--------- src/api/const.ts | 1 + tests/clone.test.ts | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/api/clone.ts b/src/api/clone.ts index 488ae86..85ba6c7 100644 --- a/src/api/clone.ts +++ b/src/api/clone.ts @@ -10,6 +10,7 @@ import { TAG_RECURRING_MAP, TAG_RECURRING_OBJECT, TAG_RECURRING_SET, + TAG_REGEXP, TAG_SYMBOL, TAG_UNDEFINED, TAG_UNSERIALIZABLE, @@ -129,6 +130,8 @@ async function recursiveClone( } else if (isSymbol(value)) { const { name } = catalog.lookup(value, TAG_SYMBOL); rv = name; + } else if (isRegExp(value)) { + rv = TAG_REGEXP(value); } else if (isArray(value)) { rv = await serializeArrayAlike(catalog, value, TAG_RECURRING_ARRAY); } else if (isSet(value)) { @@ -147,15 +150,6 @@ async function recursiveClone( return rv; } -function isNumericSpecials(value: unknown): value is bigint | number { - return ( - typeof value === 'bigint' || - Number.isNaN(value) || - value === -Infinity || - value === Infinity - ); -} - async function serializeArrayAlike( catalog: ObjectsCatalog, value: unknown[] | Set, @@ -220,6 +214,8 @@ async function serializeMapKey( } else if (isSymbol(key)) { const { name } = catalog.lookup(key, TAG_SYMBOL); rv = name; + } else if (isRegExp(key)) { + rv = TAG_REGEXP(key); } else if (isArray(key)) { const { name } = catalog.lookup(key, TAG_RECURRING_ARRAY); rv = name; @@ -319,6 +315,15 @@ function stringifyError(error: unknown) { : TAG_EXCEPTION_FALLBACK; } +function isNumericSpecials(value: unknown): value is bigint | number { + return ( + typeof value === 'bigint' || + Number.isNaN(value) || + value === -Infinity || + value === Infinity + ); +} + function isArray(that: unknown): that is unknown[] { return ( that instanceof Array || @@ -379,3 +384,7 @@ function isSymbol(that: unknown): that is Symbol { function isObject(that: unknown): that is object { return (that !== null && typeof that === 'object') || that instanceof Object; } + +function isRegExp(that: unknown): that is RegExp { + return that instanceof RegExp; +} diff --git a/src/api/const.ts b/src/api/const.ts index b135cd1..16b5fed 100644 --- a/src/api/const.ts +++ b/src/api/const.ts @@ -16,6 +16,7 @@ export const TAG_FUNCTION = (name: string, hash: string) => `ƒ${name ? ` ${name}` : ''}âŸȘ${hash}⟫`; export const TAG_NUMERIC = (value: bigint | number) => typeof value === 'bigint' ? `BigIntâŸȘ${value}⟫` : `NumberâŸȘ${value}⟫`; +export const TAG_REGEXP = (value: RegExp) => `RegExpâŸȘ${value}⟫`; export const ERROR_NO_CONNECTION = 'Could not establish connection. Receiving end does not exist.'; export const ERROR_PORT_CLOSED = diff --git a/tests/clone.test.ts b/tests/clone.test.ts index 2516c6b..653732c 100644 --- a/tests/clone.test.ts +++ b/tests/clone.test.ts @@ -156,3 +156,18 @@ test('clone special numerics', async () => { test('clone undefined', async () => { assert.deepEqual(await customClone(undefined), 'âŸȘundefined⟫'); }); + +test('clone RegExp', async () => { + assert.deepEqual( + await customClone({ + test1: new RegExp('test1', 'gim'), + test2: /test2/gim, + map: new Map([[/test3/gim, 'map-key-value']]), + }), + { + test1: 'RegExpâŸȘ/test1/gim⟫', + test2: 'RegExpâŸȘ/test2/gim⟫', + map: { 'RegExpâŸȘ/test3/gim⟫': 'map-key-value' }, + } + ); +});