From fbb2d71f5838197542d099d9044277db630b0d69 Mon Sep 17 00:00:00 2001 From: Azgaar Date: Fri, 3 Jun 2022 00:18:18 +0300 Subject: [PATCH] refactor - create types array to have named attributes --- utils/arrayUtils.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/utils/arrayUtils.js b/utils/arrayUtils.js index 241d2fac..431988c3 100644 --- a/utils/arrayUtils.js +++ b/utils/arrayUtils.js @@ -103,12 +103,14 @@ function getTypedArray(maxValue) { `Array maxValue must be an integer between 0 and 4294967295, got ${maxValue}` ); - if (maxValue <= 255) return Uint8Array; - if (maxValue <= 65535) return Uint16Array; - if (maxValue <= 4294967295) return Uint32Array; + if (maxValue <= UINT8_MAX) return Uint8Array; + if (maxValue <= UINT16_MAX) return Uint16Array; + if (maxValue <= UINT32_MAX) return Uint32Array; return Uint32Array; } -function createTypedArray({maxValue, length}) { - return new (getTypedArray(maxValue))(length); +function createTypedArray({maxValue, length, from}) { + const typedArray = getTypedArray(maxValue); + if (!from) return new typedArray(length); + return typedArray.from(from); }