From 593d299a8fd2dc5b6d90cfe43abf0f59770b4d7c Mon Sep 17 00:00:00 2001 From: Yuji Sugiura Date: Fri, 7 Apr 2017 08:30:34 +0900 Subject: [PATCH 1/3] Test for ObservableArray#findIndex --- test/array.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/array.js b/test/array.js index de0e4b6a1..b6d7776b3 100644 --- a/test/array.js +++ b/test/array.js @@ -121,7 +121,7 @@ test('array should support iterall / iterable ', t => { t.end() }) -test('find and remove', function(t) { +test('find(findIndex) and remove', function(t) { var a = mobx.observable([10,20,20]); var idx = -1; function predicate(item, index) { @@ -134,21 +134,27 @@ test('find and remove', function(t) { t.equal(a.find(predicate), 20); t.equal(idx, 1); + t.equal(a.findIndex(predicate), 1); t.equal(a.find(predicate, null, 1), 20); t.equal(idx, 1); + t.equal(a.findIndex(predicate, null, 1), 1); t.equal(a.find(predicate, null, 2), 20); t.equal(idx, 2); + t.equal(a.findIndex(predicate, null, 2), 2); idx = -1; t.equal(a.find(predicate, null, 3), undefined); t.equal(idx, -1); + t.equal(a.findIndex(predicate, null, 3), -1); t.equal(a.remove(20), true); t.equal(a.find(predicate), 20); t.equal(idx, 1); + t.equal(a.findIndex(predicate), 1); idx = -1; t.equal(a.remove(20), true); t.equal(a.find(predicate), undefined); t.equal(idx, -1); + t.equal(a.findIndex(predicate), -1); t.equal(a.remove(20), false); From 1cc37206182b2404df62bf59db31a8764b3976c6 Mon Sep 17 00:00:00 2001 From: Yuji Sugiura Date: Fri, 7 Apr 2017 08:35:09 +0900 Subject: [PATCH 2/3] Implement ObservableArray#findIndex --- src/types/observablearray.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/types/observablearray.ts b/src/types/observablearray.ts index 582b41859..3642c9095 100644 --- a/src/types/observablearray.ts +++ b/src/types/observablearray.ts @@ -27,6 +27,7 @@ export interface IObservableArray extends Array { peek(): T[]; replace(newItems: T[]): T[]; find(predicate: (item: T, index: number, array: IObservableArray) => boolean, thisArg?: any, fromIndex?: number): T; + findIndex(predicate: (item: T, index: number, array: IObservableArray) => boolean, thisArg?: any, fromIndex?: number): number; remove(value: T): boolean; move(fromIndex: number, toIndex: number): void; } @@ -299,12 +300,18 @@ export class ObservableArray extends StubArray { // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find find(predicate: (item: T, index: number, array: ObservableArray) => boolean, thisArg?, fromIndex = 0): T | undefined { + const idx = this.findIndex.apply(this, arguments); + return idx === -1 ? undefined : this.$mobx.values[idx]; + } + + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex + findIndex(predicate: (item: T, index: number, array: ObservableArray) => boolean, thisArg?, fromIndex = 0): number { this.$mobx.atom.reportObserved(); const items = this.$mobx.values, l = items.length; for (let i = fromIndex; i < l; i++) if (predicate.call(thisArg, items[i], i, this)) - return items[i]; - return undefined; + return i; + return -1; } /* @@ -478,6 +485,7 @@ makeNonEnumerable(ObservableArray.prototype, [ "toJSON", "peek", "find", + "findIndex", "splice", "spliceWithArray", "push", From 3cf0696b706166ae353f939ab66e04d046f19cb5 Mon Sep 17 00:00:00 2001 From: Yuji Sugiura Date: Fri, 7 Apr 2017 08:46:42 +0900 Subject: [PATCH 3/3] Add typings for Flow --- flow-typed/mobx.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/flow-typed/mobx.js b/flow-typed/mobx.js index 28ded7afc..aa09c6fbd 100644 --- a/flow-typed/mobx.js +++ b/flow-typed/mobx.js @@ -150,6 +150,9 @@ declare module 'mobx' { find( predicate: (item: T, index: number, array: Array) => boolean, thisArg?: any, fromIndex?: number ): T | any; + findIndex( + predicate: (item: T, index: number, array: Array) => boolean, thisArg?: any, fromIndex?: number + ): number; remove(value: T): boolean; }