Skip to content

Commit

Permalink
implement support RegExp type #28
Browse files Browse the repository at this point in the history
Signed-off-by: 🕷️ <[email protected]>
  • Loading branch information
zendive committed Feb 24, 2025
1 parent 2f7692f commit 81d0c32
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/api/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TAG_RECURRING_MAP,
TAG_RECURRING_OBJECT,
TAG_RECURRING_SET,
TAG_REGEXP,
TAG_SYMBOL,
TAG_UNDEFINED,
TAG_UNSERIALIZABLE,
Expand Down Expand Up @@ -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)) {
Expand All @@ -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<unknown>,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions src/api/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
15 changes: 15 additions & 0 deletions tests/clone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, any>([[/test3/gim, 'map-key-value']]),
}),
{
test1: 'RegExp⟪/test1/gim⟫',
test2: 'RegExp⟪/test2/gim⟫',
map: { 'RegExp⟪/test3/gim⟫': 'map-key-value' },
}
);
});

0 comments on commit 81d0c32

Please sign in to comment.