Skip to content

Instantly share code, notes, and snippets.

@Driptap
Last active March 21, 2018 16:52
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 Driptap/76091c3cb09fc7b9ae9719c156b75001 to your computer and use it in GitHub Desktop.
Save Driptap/76091c3cb09fc7b9ae9719c156b75001 to your computer and use it in GitHub Desktop.
Inspired by rails
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