Skip to content

Commit

Permalink
typing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
itanka9 committed Nov 6, 2024
1 parent f34778b commit b074738
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/json2pbf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export enum PackMethod {
}

interface PackCtx {
pbf: Pbf;
pbf: typeof Pbf;
stringMap: { [str: string]: number };
strings: string[];
}

interface UnpackCtx {
keys: string[];
pbf: Pbf;
pbf: typeof Pbf;
}

function indexateString(ctx: PackCtx, key: string) {
Expand All @@ -38,7 +38,7 @@ function indexateString(ctx: PackCtx, key: string) {
return i;
}

function writeTag(pbf: Pbf, key: number, type: JsonType) {
function writeTag(pbf: typeof Pbf, key: number, type: JsonType) {
pbf.writeVarint((key << 3) | type);
}

Expand Down Expand Up @@ -84,7 +84,7 @@ const decoders: Record<JsonType, (ctx: UnpackCtx) => any> = {
[JsonType.Object]: (ctx: UnpackCtx) => {
const { pbf } = ctx;
const len = pbf.readVarint();
const obj = {};
const obj: any = {};
for (let i = 0; i < len; i++) {
var val = pbf.readVarint();
obj[ctx.keys[val >> 3]] = fromPbf(ctx, val & 0x7);
Expand Down Expand Up @@ -169,11 +169,11 @@ function fromPbf(ctx: UnpackCtx, type: JsonType) {
}
}
export interface PackOptions {
pbf?: Pbf,
pbf?: typeof Pbf,
method?: PackMethod,
columns?: Record<string, JsonType>
}
function writeColumns(columns: Record<string, JsonType>, pbf: Pbf) {
function writeColumns(columns: Record<string, JsonType>, pbf: typeof Pbf) {
const length = Object.keys(columns).length;
pbf.writeVarint(length);
for (const k in columns) {
Expand All @@ -182,7 +182,7 @@ function writeColumns(columns: Record<string, JsonType>, pbf: Pbf) {
}
}

function readColumns(pbf: Pbf) {
function readColumns(pbf: typeof Pbf) {
const length = pbf.readVarint();
const columns: Array<{ key: string, type: JsonType }> = [];
for (let i = 0; i < length; i++) {
Expand All @@ -200,8 +200,8 @@ export function pack(val: any, options?: PackOptions): ArrayBuffer {

pbf.writeFixed32(VERSION << 24 | method << 16);

const strings = [];
const stringMap = {};
const strings: any[] = [];
const stringMap: Record<string, number> = {};
const ctx: PackCtx = { pbf, strings, stringMap };

switch (method) {
Expand Down Expand Up @@ -300,7 +300,7 @@ export function unpack(arr: ArrayBuffer) {
const ctx: UnpackCtx = { keys: [], pbf };
const len = pbf.readFixed32();
const columns = readColumns(pbf);
const result = {};
const result: any = {};
for (const { key, type } of columns) {
const decoder = decoders[type];
const data: any[] = [];
Expand All @@ -315,7 +315,7 @@ export function unpack(arr: ArrayBuffer) {
const ctx: UnpackCtx = { keys: [], pbf };
const len = pbf.readFixed32();
const columns = readColumns(pbf);
const result = new Array(len).fill(0).map(() => ({}));
const result: any[] = new Array(len).fill(0).map(() => ({}));
for (const { key, type } of columns) {
const decoder = decoders[type];
for (let i = 0; i < len; i++) {
Expand Down

0 comments on commit b074738

Please sign in to comment.