Skip to content

Commit

Permalink
refactor(SchemaStore): introduce serializeRaw
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `serialize` no longer returns an `UnalignedUint16Array`, if you desire the old behaviour, use `serializeRaw`
  • Loading branch information
kyranet committed Jan 22, 2025
1 parent 42db079 commit 48898e3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
13 changes: 12 additions & 1 deletion packages/string-store/src/lib/schema/SchemaStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,25 @@ export class SchemaStore<Entries extends object = object> {
return schema;
}

/**
* Serializes a value using the schema with the given id
*
* @param id The id of the schema to use for serialization
* @param value The value to serialize
* @returns The serialized string
*/
public serialize<const Id extends KeyOfStore<this>>(id: Id, value: SerializeValue<Entries[Id] & object>): string {
return this.serializeRaw(id, value).toString();
}

/**
* Serializes a value using the schema with the given id
*
* @param id The id of the schema to use for serialization
* @param value The value to serialize
* @returns The serialized buffer
*/
public serialize<const Id extends KeyOfStore<this>>(id: Id, value: SerializeValue<Entries[Id] & object>): UnalignedUint16Array {
public serializeRaw<const Id extends KeyOfStore<this>>(id: Id, value: SerializeValue<Entries[Id] & object>): UnalignedUint16Array {
const schema = this.get(id) as Schema<Id, object>;
const buffer = new UnalignedUint16Array(schema.totalBitSize ?? this.defaultMaximumArrayLength);
schema.serialize(buffer, value);
Expand Down
9 changes: 6 additions & 3 deletions packages/string-store/tests/lib/SchemaStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,26 @@ describe('SchemaStore', () => {
test('GIVEN a schema and a value THEN it serializes and deserializes the buffer correctly', () => {
const store = new SchemaStore(10).add(new Schema(2).string('name').float64('height'));

const buffer = store.serialize(2, { name: 'Mario', height: 1.8 });
const buffer = store.serializeRaw(2, { name: 'Mario', height: 1.8 });
const deserialized = store.deserialize(buffer);
expect<{ id: 2; data: { height: number } }>(deserialized).toEqual({ id: 2, data: { name: 'Mario', height: 1.8 } });

expect<2>(store.getIdentifier(buffer)).toBe(2);
expect<2>(store.getIdentifier(buffer.toString())).toBe(2);

expectTypeOf(buffer).toEqualTypeOf<UnalignedUint16Array>();
});

test('GIVEN a schema and a value THEN it serializes and deserializes the binary string correctly', () => {
const store = new SchemaStore(10).add(new Schema(2).string('name').float64('height'));

const buffer = store.serialize(2, { name: 'Mario', height: 1.8 });
const deserialized = store.deserialize(buffer.toString());
const deserialized = store.deserialize(buffer);
expect<{ id: 2; data: { height: number } }>(deserialized).toEqual({ id: 2, data: { name: 'Mario', height: 1.8 } });

expect<2>(store.getIdentifier(buffer)).toBe(2);
expect<2>(store.getIdentifier(buffer.toString())).toBe(2);

expectTypeOf(buffer).toEqualTypeOf<string>();
});

test('GIVEN a schema with a constant THEN it serializes and deserializes the buffer correctly', () => {
Expand Down

0 comments on commit 48898e3

Please sign in to comment.