From 7966865d53b1c69920e0ccf86a187e845efd6af0 Mon Sep 17 00:00:00 2001 From: FUJI Goro Date: Sun, 6 Nov 2022 11:31:35 +0900 Subject: [PATCH] avoid using typescript-specific language extensions (const enum) --- src/Decoder.ts | 26 ++++++++++++-------------- test/edge-cases.test.ts | 30 +++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/Decoder.ts b/src/Decoder.ts index eb6fa157..49b4b227 100644 --- a/src/Decoder.ts +++ b/src/Decoder.ts @@ -6,11 +6,9 @@ import { createDataView, ensureUint8Array } from "./utils/typedArrays"; import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder"; import { DecodeError } from "./DecodeError"; -const enum State { - ARRAY, - MAP_KEY, - MAP_VALUE, -} +const STATE_ARRAY = "array"; +const STATE_MAP_KEY = "map_key"; +const STATE_MAP_VALUE = "map_value"; type MapKeyType = string | number; @@ -21,7 +19,7 @@ const isValidMapKeyType = (key: unknown): key is MapKeyType => { }; type StackMapState = { - type: State.MAP_KEY | State.MAP_VALUE; + type: typeof STATE_MAP_KEY | typeof STATE_MAP_VALUE; size: number; key: MapKeyType | null; readCount: number; @@ -29,7 +27,7 @@ type StackMapState = { }; type StackArrayState = { - type: State.ARRAY; + type: typeof STATE_ARRAY; size: number; array: Array; position: number; @@ -397,7 +395,7 @@ export class Decoder { while (stack.length > 0) { // arrays and maps const state = stack[stack.length - 1]!; - if (state.type === State.ARRAY) { + if (state.type === STATE_ARRAY) { state.array[state.position] = object; state.position++; if (state.position === state.size) { @@ -406,7 +404,7 @@ export class Decoder { } else { continue DECODE; } - } else if (state.type === State.MAP_KEY) { + } else if (state.type === STATE_MAP_KEY) { if (!isValidMapKeyType(object)) { throw new DecodeError("The type of key must be string or number but " + typeof object); } @@ -415,7 +413,7 @@ export class Decoder { } state.key = object; - state.type = State.MAP_VALUE; + state.type = STATE_MAP_VALUE; continue DECODE; } else { // it must be `state.type === State.MAP_VALUE` here @@ -428,7 +426,7 @@ export class Decoder { object = state.map; } else { state.key = null; - state.type = State.MAP_KEY; + state.type = STATE_MAP_KEY; continue DECODE; } } @@ -475,7 +473,7 @@ export class Decoder { } this.stack.push({ - type: State.MAP_KEY, + type: STATE_MAP_KEY, size, key: null, readCount: 0, @@ -489,7 +487,7 @@ export class Decoder { } this.stack.push({ - type: State.ARRAY, + type: STATE_ARRAY, size, array: new Array(size), position: 0, @@ -523,7 +521,7 @@ export class Decoder { private stateIsMapKey(): boolean { if (this.stack.length > 0) { const state = this.stack[this.stack.length - 1]!; - return state.type === State.MAP_KEY; + return state.type === STATE_MAP_KEY; } return false; } diff --git a/test/edge-cases.test.ts b/test/edge-cases.test.ts index ca4ad511..3119414c 100644 --- a/test/edge-cases.test.ts +++ b/test/edge-cases.test.ts @@ -1,7 +1,7 @@ // kind of hand-written fuzzing data // any errors should not break Encoder/Decoder instance states import assert from "assert"; -import { encode, decodeAsync, decode, Encoder, Decoder, decodeMulti, decodeMultiStream } from "../src"; +import { encode, decodeAsync, decode, Encoder, Decoder, decodeMulti, decodeMultiStream, ExtensionCodec } from "../src"; import { DataViewIndexOutOfBoundsError } from "../src/Decoder"; function testEncoder(encoder: Encoder): void { @@ -181,4 +181,32 @@ describe("edge cases", () => { assert.deepStrictEqual(results, []); }); }); + + context("xxx", () => { + it("xxx", () => { + function encodeStatsJson(stats: any) { + const extensionCodec = new ExtensionCodec(); + const filteredFields = ['name']; + extensionCodec.register({ + type: 0, + encode: (object: any) => { + console.log(object); + if (object instanceof Object && !(object instanceof Array)) { + for (const field of filteredFields) { + delete object[field]; + } + } + console.log(object); + return null; + }, + decode: () => null, + }); + + const encoded = encode(stats, { extensionCodec }); + return encoded; + } + + console.log(encodeStatsJson({ id: "xxx", name: 'foo', bar: 'baz', xxx: { name: 1, b: 2 } })); + }); + }); });