Skip to content

Commit

Permalink
Add support for numbers as map keys (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyzenchenko authored Aug 30, 2019
1 parent efff31d commit 695f3b4
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ const enum State {
MAP_VALUE,
}

type MapKeyType = string | number;

const isValidMapKeyType = (key: unknown): key is MapKeyType => {
const keyType = typeof key;

return keyType === "string" || keyType === "number";
};

type StackMapState = {
type: State.MAP_KEY | State.MAP_VALUE;
size: number;
key: string | null;
key: MapKeyType | null;
readCount: number;
map: Record<string, unknown>;
};
Expand Down Expand Up @@ -375,9 +383,10 @@ export class Decoder {
continue DECODE;
}
} else if (state.type === State.MAP_KEY) {
if (typeof object !== "string") {
throw new Error("The type of key must be string but " + typeof object);
if (!isValidMapKeyType(object)) {
throw new Error("The type of key must be string or number but " + typeof object);
}

state.key = object;
state.type = State.MAP_VALUE;
continue DECODE;
Expand Down

0 comments on commit 695f3b4

Please sign in to comment.