Skip to content

Commit 68dbfe7

Browse files
fastArrayJoin() refactoring
1 parent 70c9267 commit 68dbfe7

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uquik",
3-
"version": "1.0.51",
3+
"version": "1.0.52",
44
"description": "uQuik HTTP(S) framework",
55
"main": "index.js",
66
"scripts": {

src/utils.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -257,24 +257,36 @@ const byteToHex = [
257257
'ff'
258258
]
259259

260-
const fastArrayJoin = (array, separator = '') => {
260+
const fastArrayJoin = (array, separator = ',') => {
261261
const length = array.length
262-
let result = ''
263262
const last = length - 1
264-
for (let i = 0; i < length; i++) {
265-
if (array[i] === null || array[i] === undefined) {
266-
if (i !== last) result += separator
267-
} else if (typeof array[i] === 'object' || typeof array[i] === 'function') {
263+
let result = ''
264+
const stringifyValue = (anyValue) => {
265+
const type = typeof anyValue
266+
if (type === 'string') {
267+
return anyValue
268+
} else if (type === 'number') {
269+
return anyValue.toString()
270+
} else if (type === 'object' || type === 'function') {
268271
let value = '[object Object]'
269-
if (typeof array[i].toString === 'function') {
270-
const toStringValue = array[i].toString()
272+
if (typeof anyValue.toString === 'function') {
273+
const toStringValue = anyValue.toString()
271274
if (typeof toStringValue === 'string') value = toStringValue
272275
}
273-
result += i !== last ? value + separator : value
274-
} else {
275-
result += i !== last ? array[i] + separator : array[i]
276+
return value
277+
}
278+
}
279+
280+
for (let i = 0; i < length; i++) {
281+
const isLast = i === last
282+
if (array[i] === null || array[i] === undefined) {
283+
if (!isLast) result += separator
284+
continue
276285
}
286+
const value = stringifyValue(array[i])
287+
result += !isLast ? value + separator : value
277288
}
289+
278290
return result
279291
}
280292

0 commit comments

Comments
 (0)