Last active
April 29, 2016 15:49
-
-
Save flipjs/7690511d88d775102cfa8e441fec01bc to your computer and use it in GitHub Desktop.
Join strings or array of strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Concatenates all the elements of a string array, using the specified separator between each element. | |
function strJoin (...args) { | |
const [delimiter, ...strings] = args | |
if (!delimiter || !strings.length) { | |
throw new Error('Invalid number of arguments.') | |
} | |
if (Array.isArray(...strings)) { | |
const [arr] = strings | |
if (!arr.length) { | |
throw new Error('Array is empty.') | |
} | |
return strings[0].join(delimiter) | |
} | |
return strings.join(delimiter) | |
} | |
var message = strJoin('. ', | |
'Lorem dim sum Shangai steam buns chicken feet', | |
'Lorem dim sum steamed sponge cake tofu with', | |
'Lorem dim sum turnip cake leek dumplings deep', | |
'Lorem dim sum Pot sticker water chestnut cake' | |
) | |
console.log(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment