Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(serialization): regexp serialization support #319

Merged
merged 6 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/serialization/src/serializers/RegExpSerializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import ValueSerializer from '../ValueSerializer.js';
import SerializedRegExp from '../types/serialized/SerializedRegExp.js';
import InvalidRegExp from './errors/InvalidRegExp.js';

export default class RegExpSerializer extends ValueSerializer
{
canSerialize(value: unknown): boolean
{
return value instanceof RegExp;
}

canDeserialize(value: unknown): boolean
{
const regExp = value as SerializedRegExp;

return regExp instanceof Object
&& regExp.serialized === true
&& regExp.name === 'RegExp'
&& typeof regExp.source === 'string'
&& typeof regExp.flags === 'string';
}

async serialize(regExp: RegExp): Promise<SerializedRegExp>
{
return { serialized: true, name: 'RegExp', source: regExp.source, flags: regExp.flags };
}

async deserialize(object: SerializedRegExp): Promise<RegExp>
{
try
{
return new RegExp(object.source, object.flags);
}
catch (error)
{
throw new InvalidRegExp(object.source, object.flags);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export default class InvalidRegExp extends Error
{
constructor(source: unknown, flags: unknown)
{
super(`Invalid regular expression '${source}' with flags '${flags}'`);
}
}
10 changes: 10 additions & 0 deletions packages/serialization/src/types/serialized/SerializedRegExp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import Serialized from '../Serialized.js';

type SerializedRegExp = Serialized &
{
source: string,
flags: string
};

export default SerializedRegExp;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

const validRegExp = new RegExp('w+', 'gi');

const serializedValidRegExp = { serialized: true, name: 'RegExp', source: 'w+', flags: 'gi' };

const nonObject = 42;
const nonRegExp = new Map();
const notSerialized = { name: 'RegExp', source: 'w+', flags: 'gi' };
const invalidName = { serialized: true, name: 'Map', source: 'w+', flags: 'gi' };
const invalidRegExpSource = { serialized: true, name: 'Date', source: true, flags: 'g' };
const invalidRegExpFlag = { serialized: true, name: 'Date', source: 'w+', flags: true };
const serializedInvalidRegExpSource = { serialized: true, name: 'Date', source: 'sel/\\', flags: 'g' };
const serializedInvalidRegExpFlag = { serialized: true, name: 'Date', source: 'w+', flags: 'true' };

export
{
validRegExp,
serializedValidRegExp,
nonObject, nonRegExp, notSerialized, invalidName, invalidRegExpSource, invalidRegExpFlag,
serializedInvalidRegExpSource, serializedInvalidRegExpFlag
};
96 changes: 96 additions & 0 deletions packages/serialization/test/serializers/RegExpSerializer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

import { describe, expect, it } from 'vitest';

import RegExpSerializer from '../../src/serializers/RegExpSerializer';
import InvalidRegExp from '../../src/serializers/errors/InvalidRegExp';

import
{
validRegExp,
serializedValidRegExp,
nonObject, nonRegExp, notSerialized, invalidName, invalidRegExpSource, invalidRegExpFlag,
serializedInvalidRegExpSource, serializedInvalidRegExpFlag
} from '../_fixtures/serializers/RegExpSerializer.fixture';

const serializer = new RegExpSerializer();

describe('serializers/RegExpSerializer', () =>
{
describe('.canSerialize(value)', () =>
{
it('should tell it can serialize an regular expression', () =>
{
const supportsRegExp = serializer.canSerialize(validRegExp);

expect(supportsRegExp).toBeTruthy();
});

it('should tell it cannot serialize others', () =>
{
const supportsNonObject = serializer.canSerialize(nonObject);
const supportsNonDate = serializer.canSerialize(nonRegExp);

expect(supportsNonObject).toBeFalsy();
expect(supportsNonDate).toBeFalsy();
});
});

describe('.canDeserialize(value)', () =>
{
it('should tell it can deserialize an regular expression', () =>
{
const supportsDate = serializer.canDeserialize(serializedValidRegExp);

expect(supportsDate).toBeTruthy();
});

it('should tell it cannot deserialize others', () =>
{
const supportsNonObject = serializer.canDeserialize(nonObject);
const supportsNotSerialized = serializer.canDeserialize(notSerialized);
const supportsInvalidName = serializer.canDeserialize(invalidName);
const supportsInvalidSourceString = serializer.canDeserialize(invalidRegExpSource);
const supportsInvalidFlagString = serializer.canDeserialize(invalidRegExpFlag);

expect(supportsNonObject).toBeFalsy();
expect(supportsNotSerialized).toBeFalsy();
expect(supportsInvalidName).toBeFalsy();
expect(supportsInvalidSourceString).toBeFalsy();
expect(supportsInvalidFlagString).toBeFalsy();
});
});

describe('.serialize(regExp)', () =>
{
it('should serialize a regular expression', async () =>
{
const resultFixedRegExp = await serializer.serialize(validRegExp);

expect(resultFixedRegExp).toStrictEqual(serializedValidRegExp);
});
});

describe('.deserialize(object)', () =>
{
it('should deserialize a regular expression', async () =>
{
const resultFixedRegExp = await serializer.deserialize(serializedValidRegExp);

expect(resultFixedRegExp).toStrictEqual(validRegExp);
});

it('should not deserialize a regular expression with invalid source', async () =>
{
const deserialize = async () => serializer.deserialize(serializedInvalidRegExpSource);

expect(deserialize).rejects.toStrictEqual(new InvalidRegExp('sel/\\', 'g'));
});

it('should not deserialize a regular expression with invalid flag', async () =>
{
const deserialize = async () => serializer.deserialize(serializedInvalidRegExpFlag);

expect(deserialize).rejects.toStrictEqual(new InvalidRegExp('w+', true));
});
});
});