Skip to content

Commit

Permalink
fastArrayJoin() refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
piliugin-anton committed Jul 24, 2022
1 parent 70c9267 commit 68dbfe7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uquik",
"version": "1.0.51",
"version": "1.0.52",
"description": "uQuik HTTP(S) framework",
"main": "index.js",
"scripts": {
Expand Down
34 changes: 23 additions & 11 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 68dbfe7

Please sign in to comment.