From 68dbfe7bdd17e0aa67e06964fe9e0ef9ea607ace Mon Sep 17 00:00:00 2001 From: Anton Piliugin Date: Sun, 24 Jul 2022 06:50:33 +0500 Subject: [PATCH] fastArrayJoin() refactoring --- package.json | 2 +- src/utils.js | 34 +++++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 27b2f07..550a20c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uquik", - "version": "1.0.51", + "version": "1.0.52", "description": "uQuik HTTP(S) framework", "main": "index.js", "scripts": { diff --git a/src/utils.js b/src/utils.js index 2516715..fe6f064 100644 --- a/src/utils.js +++ b/src/utils.js @@ -257,24 +257,36 @@ const byteToHex = [ 'ff' ] -const fastArrayJoin = (array, separator = '') => { +const fastArrayJoin = (array, separator = ',') => { const length = array.length - let result = '' const last = length - 1 - for (let i = 0; i < length; i++) { - if (array[i] === null || array[i] === undefined) { - if (i !== last) result += separator - } else if (typeof array[i] === 'object' || typeof array[i] === 'function') { + let result = '' + const stringifyValue = (anyValue) => { + const type = typeof anyValue + if (type === 'string') { + return anyValue + } else if (type === 'number') { + return anyValue.toString() + } else if (type === 'object' || type === 'function') { let value = '[object Object]' - if (typeof array[i].toString === 'function') { - const toStringValue = array[i].toString() + if (typeof anyValue.toString === 'function') { + const toStringValue = anyValue.toString() if (typeof toStringValue === 'string') value = toStringValue } - result += i !== last ? value + separator : value - } else { - result += i !== last ? array[i] + separator : array[i] + return value + } + } + + for (let i = 0; i < length; i++) { + const isLast = i === last + if (array[i] === null || array[i] === undefined) { + if (!isLast) result += separator + continue } + const value = stringifyValue(array[i]) + result += !isLast ? value + separator : value } + return result }