Skip to content

Commit fba39d8

Browse files
committed
feat: update encode methods to return Uint8Array<ArrayBuffer>
1 parent 32710e1 commit fba39d8

14 files changed

Lines changed: 26 additions & 26 deletions

File tree

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nomadshiba/codec",
3-
"version": "0.5.0-preview.1",
3+
"version": "0.5.0-preview.2",
44
"license": "LGPL-2.1-only",
55
"exports": "./lib/mod.ts",
66
"tasks": {

lib/bytes/bytes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class BytesCodec<const O extends BytesOptions | undefined = undefined> ex
105105
* codec.encode(new Uint8Array([1, 2])); // throws RangeError
106106
* ```
107107
*/
108-
public encode(value: Uint8Array): Uint8Array {
108+
public encode(value: Uint8Array): Uint8Array<ArrayBuffer> {
109109
if (this.stride.kind === "fixed") {
110110
if (value.length !== this.stride.size) {
111111
throw new RangeError(

lib/bytes/string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class StringCodec<const O extends StringOptions | undefined = undefined>
108108
* codec.encode("hi"); // throws RangeError
109109
* ```
110110
*/
111-
public encode(value: string): Uint8Array {
111+
public encode(value: string): Uint8Array<ArrayBuffer> {
112112
const utf8 = this.#encoder.encode(value);
113113
if (this.stride.kind === "fixed") {
114114
if (utf8.length !== this.stride.size) {

lib/codec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export abstract class Codec<O extends I = any, I = O> {
101101
* const buf = U32.encode(0xDEADBEEF);
102102
* // buf => Uint8Array [0xDE, 0xAD, 0xBE, 0xEF]
103103
*/
104-
public abstract encode(value: I): Uint8Array;
104+
public abstract encode(value: I): Uint8Array<ArrayBuffer>;
105105
public abstract encodeInto(value: I, target: Uint8Array, offset?: number): number;
106106

107107
/**
@@ -245,7 +245,7 @@ export class TransformCodec<
245245
* @param value - Value to encode.
246246
* @returns Encoded bytes.
247247
*/
248-
encode(value: I): Uint8Array {
248+
encode(value: I): Uint8Array<ArrayBuffer> {
249249
return this.inner.encode(value);
250250
}
251251

lib/composites/array.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class ArrayCodec<T extends ArrayGeneric, const O extends ArrayOptions | u
145145
* codec.encode([1, 2, 3]); // Uint8Array [1, 2, 3]
146146
* codec.encode([1, 2]); // throws RangeError
147147
*/
148-
public encode(value: ArrayInput<T>): Uint8Array {
148+
public encode(value: ArrayInput<T>): Uint8Array<ArrayBuffer> {
149149
if (this.#elementCount !== undefined) {
150150
if (value.length !== this.#elementCount) {
151151
throw new RangeError(

lib/composites/enum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class EnumCodec<const T extends EnumGeneric> extends Codec<EnumOutput<T>,
111111
* @example
112112
* const bytes = codec.encode({ kind: "Quit", value: null });
113113
*/
114-
public encode(value: EnumInput<T>): Uint8Array {
114+
public encode(value: EnumInput<T>): Uint8Array<ArrayBuffer> {
115115
const index = this.keys.indexOf(value.kind);
116116
if (index === -1) {
117117
throw new Error(`Invalid union variant: ${String(value.kind)}`);

lib/composites/mapping.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class MappingCodec<const T extends MappingGeneric> extends Codec<MappingO
8888
* @example
8989
* const bytes = codec.encode(new Map([["a", 1]]));
9090
*/
91-
public encode(value: MappingInput<T>): Uint8Array {
91+
public encode(value: MappingInput<T>): Uint8Array<ArrayBuffer> {
9292
return this.#entriesCodec.encode(value.entries().toArray() as never);
9393
}
9494

lib/composites/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class ModelCodec<const T extends ModelGeneric> extends Codec<ModelOutput<
171171
* @example
172172
* const bytes = codec.encode({ id: 1, name: "Alice" });
173173
*/
174-
public encode(value: ModelInput<T>): Uint8Array {
174+
public encode(value: ModelInput<T>): Uint8Array<ArrayBuffer> {
175175
if (this.stride.kind === "fixed") {
176176
const result = new Uint8Array(this.stride.size);
177177
this.encodeInto(value, result);

lib/composites/nullable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class NullableCodec<T extends NullableGeneric> extends Codec<NullableOutp
8989
* const bytes = codec.encode(null); // presence byte = 0x00
9090
* const bytes = codec.encode("hi"); // presence byte = 0x01, then payload
9191
*/
92-
public encode(value: NullableInput<T>): Uint8Array {
92+
public encode(value: NullableInput<T>): Uint8Array<ArrayBuffer> {
9393
if (value === null) {
9494
const size = this.stride.kind === "fixed" ? this.stride.size : 1;
9595
return new Uint8Array(size); // zero-filled by default

lib/composites/padded_enum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class PaddedEnumCodec<const T extends PaddedEnumGeneric> extends Codec<En
116116
* const bytes = EventCodec.encode({ kind: "MouseMove", value: { x: 100, y: 200 } });
117117
* // bytes.length === EventCodec.stride.size
118118
*/
119-
public encode(value: EnumInput<T>): Uint8Array {
119+
public encode(value: EnumInput<T>): Uint8Array<ArrayBuffer> {
120120
const result = new Uint8Array(this.stride.size);
121121
this.encodeInto(value, result);
122122
return result;

0 commit comments

Comments
 (0)