Created
March 1, 2018 02:50
-
-
Save mblarsen/ab627487f039980c713e753b0a8c0fcf to your computer and use it in GitHub Desktop.
Array.prototype extensions
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
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