From ae68264db4a08e12ac54fac19726db92853c25f2 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 30 Sep 2021 08:42:00 +0200 Subject: [PATCH] don't use switch where single if/else works --- src/utils/typedJSON.ts | 87 ++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 49 deletions(-) diff --git a/src/utils/typedJSON.ts b/src/utils/typedJSON.ts index 2a8a3c60..928df32e 100644 --- a/src/utils/typedJSON.ts +++ b/src/utils/typedJSON.ts @@ -22,63 +22,52 @@ export function parse(value: string): any { return decodeValue(JSON.parse(value)); } - function encodeValue(value: any): any { - switch (typeof value) { - case "object": { - if (value === null || Array.isArray(value)) { - return value; - } - // TypedArray - if (value.byteLength) { - return {_type: value.constructor.name, value: Array.from(value)}; - } - let newObj = {}; - for (const prop in value) { - if (value.hasOwnProperty(prop)) { - newObj[prop] = encodeValue(value[prop]); - } - } - return newObj; + if (typeof value === "object" && value !== null && !Array.isArray(value)) { + // TypedArray + if (value.byteLength) { + return {_type: value.constructor.name, value: Array.from(value)}; } - default: - return value; + let newObj = {}; + for (const prop in value) { + if (value.hasOwnProperty(prop)) { + newObj[prop] = encodeValue(value[prop]); + } + } + return newObj; + } else { + return value; } } function decodeValue(value: any): any { - switch (typeof value) { - case "object": { - if (value === null || Array.isArray(value)) { - return value; + if (typeof value === "object" && value !== null && !Array.isArray(value)) { + if (typeof value._type === "string") { + switch (value._type) { + case "Int8Array": return Int8Array.from(value.value); + case "Uint8Array": return Uint8Array.from(value.value); + case "Uint8ClampedArray": return Uint8ClampedArray.from(value.value); + case "Int16Array": return Int16Array.from(value.value); + case "Uint16Array": return Uint16Array.from(value.value); + case "Int32Array": return Int32Array.from(value.value); + case "Uint32Array": return Uint32Array.from(value.value); + case "Float32Array": return Float32Array.from(value.value); + case "Float64Array": return Float64Array.from(value.value); + case "BigInt64Array": return BigInt64Array.from(value.value); + case "BigUint64Array": return BigUint64Array.from(value.value); + default: + return value.value; } - if (typeof value._type === "string") { - switch (value._type) { - case "Int8Array": return Int8Array.from(value.value); - case "Uint8Array": return Uint8Array.from(value.value); - case "Uint8ClampedArray": return Uint8ClampedArray.from(value.value); - case "Int16Array": return Int16Array.from(value.value); - case "Uint16Array": return Uint16Array.from(value.value); - case "Int32Array": return Int32Array.from(value.value); - case "Uint32Array": return Uint32Array.from(value.value); - case "Float32Array": return Float32Array.from(value.value); - case "Float64Array": return Float64Array.from(value.value); - case "BigInt64Array": return BigInt64Array.from(value.value); - case "BigUint64Array": return BigUint64Array.from(value.value); - default: - return value.value; - } - } - let newObj = {}; - for (const prop in value) { - if (value.hasOwnProperty(prop)) { - newObj[prop] = decodeValue(value[prop]); - } - } - return newObj; } - default: - return value; + let newObj = {}; + for (const prop in value) { + if (value.hasOwnProperty(prop)) { + newObj[prop] = decodeValue(value[prop]); + } + } + return newObj; + } else { + return value; } }