Skip to content

Commit 9cebbf4

Browse files
committed
refactor(SchemaStore): introduce serializeRaw
BREAKING CHANGE: `serialize` not longer returns an `UnalignedUint16Array`, if you desire the old behaviour, use `serializeRaw`
1 parent 42db079 commit 9cebbf4

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

packages/string-store/src/lib/schema/SchemaStore.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,25 @@ export class SchemaStore<Entries extends object = object> {
5454
return schema;
5555
}
5656

57+
/**
58+
* Serializes a value using the schema with the given id
59+
*
60+
* @param id The id of the schema to use for serialization
61+
* @param value The value to serialize
62+
* @returns The serialized string
63+
*/
64+
public serialize<const Id extends KeyOfStore<this>>(id: Id, value: SerializeValue<Entries[Id] & object>): string {
65+
return this.serializeRaw(id, value).toString();
66+
}
67+
5768
/**
5869
* Serializes a value using the schema with the given id
5970
*
6071
* @param id The id of the schema to use for serialization
6172
* @param value The value to serialize
6273
* @returns The serialized buffer
6374
*/
64-
public serialize<const Id extends KeyOfStore<this>>(id: Id, value: SerializeValue<Entries[Id] & object>): UnalignedUint16Array {
75+
public serializeRaw<const Id extends KeyOfStore<this>>(id: Id, value: SerializeValue<Entries[Id] & object>): UnalignedUint16Array {
6576
const schema = this.get(id) as Schema<Id, object>;
6677
const buffer = new UnalignedUint16Array(schema.totalBitSize ?? this.defaultMaximumArrayLength);
6778
schema.serialize(buffer, value);

packages/string-store/tests/lib/SchemaStore.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,26 @@ describe('SchemaStore', () => {
2828
test('GIVEN a schema and a value THEN it serializes and deserializes the buffer correctly', () => {
2929
const store = new SchemaStore(10).add(new Schema(2).string('name').float64('height'));
3030

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

3535
expect<2>(store.getIdentifier(buffer)).toBe(2);
3636
expect<2>(store.getIdentifier(buffer.toString())).toBe(2);
37+
38+
expectTypeOf(buffer).toEqualTypeOf<UnalignedUint16Array>();
3739
});
3840

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

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

4648
expect<2>(store.getIdentifier(buffer)).toBe(2);
47-
expect<2>(store.getIdentifier(buffer.toString())).toBe(2);
49+
50+
expectTypeOf(buffer).toEqualTypeOf<string>();
4851
});
4952

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

0 commit comments

Comments
 (0)