Skip to content

Instantly share code, notes, and snippets.

@lubien
Last active June 26, 2019 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lubien/8412f87fdf6756a25ee8e40418211125 to your computer and use it in GitHub Desktop.
Save lubien/8412f87fdf6756a25ee8e40418211125 to your computer and use it in GitHub Desktop.
['a', 'b', 'c'] => "a, b e c" https://repl.it/@lubien/allowedExtensions
function allowedExtensions (exts) {
return exts.map((item, i) => {
const first = i === 0
const last = i === (exts.length - 1)
if (first) {
return [item]
}
if (last) {
return [' e ', item]
}
return [', ', item]
})
// flatten
.reduce((acc, x) => acc.concat(x), [])
.join('')
}
console.log(allowedExtensions(['erick', 'azul', 'amarelo', 'estranhuS']))
console.log(allowedExtensions(['feIo', 'estranhuS']))
console.log(allowedExtensions(['estranhuS']))
console.log(allowedExtensions([]))
@lubien
Copy link
Author

lubien commented Jun 26, 2019

Please come home Array#flatMap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment