Skip to content

Instantly share code, notes, and snippets.

@mblarsen
Created March 1, 2018 02: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 mblarsen/ab627487f039980c713e753b0a8c0fcf to your computer and use it in GitHub Desktop.
Save mblarsen/ab627487f039980c713e753b0a8c0fcf to your computer and use it in GitHub Desktop.
Array.prototype extensions
Object.defineProperty( // eslint-disable-line
Array.prototype, 'flatMap', {
value: function (mapFunction) {
return Array.prototype.concat.call([], ...this.map(mapFunction))
}
}
)
Object.defineProperty( // eslint-disable-line
Array.prototype, 'tap', {
value: function (callable) {
callback && typeof callable === 'function' && callable([...this])
return this
}
}
)
Object.defineProperty( // eslint-disable-line
Array.prototype, 'debug', {
value: function (label = null) {
return this.tap(a => label ? console.log(label, a) : console.log(a))
}
}
)
Object.defineProperty( // eslint-disable-line
Array.prototype, 'unique', {
value: function () {
return [...(new Set(this))]
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment