Skip to content

Instantly share code, notes, and snippets.

@flipjs
Last active April 29, 2016 15:49
Show Gist options
  • Save flipjs/7690511d88d775102cfa8e441fec01bc to your computer and use it in GitHub Desktop.
Save flipjs/7690511d88d775102cfa8e441fec01bc to your computer and use it in GitHub Desktop.
Join strings or array of strings
// 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