Last active
March 21, 2018 16:52
-
-
Save Driptap/76091c3cb09fc7b9ae9719c156b75001 to your computer and use it in GitHub Desktop.
Inspired by rails
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(Array.prototype, 'findAll', { | |
enumerable: false, | |
writable: false, | |
value: condition => this.reduce((col, el, i) => | |
condition(el, i, this) ? [...col, el] : col, []) | |
}); | |
Object.defineProperty(Array.prototype, 'exists', { | |
enumerable: false, | |
writable: false, | |
value: condition => this.findIndex(condition) > -1, | |
}); | |
// Type check in flow with | |
type ExtendedArray<T> = | |
Array<T> | |
& { exists: ((any, number, Array<any>) => boolean) => boolean } | |
& { findAll: ((any, number, Array<any>) => boolean) => Array<any>}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment