Skip to content

Commit

Permalink
Merge pull request #936 from leader22/array-find_index
Browse files Browse the repository at this point in the history
ObservableArray#findIndex
  • Loading branch information
capaj authored Apr 14, 2017
2 parents 7db4f49 + bcdae41 commit 08e5a4f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions flow-typed/mobx.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ declare module 'mobx' {
find(
predicate: (item: T, index: number, array: Array<T>) => boolean, thisArg?: any, fromIndex?: number
): T | any;
findIndex(
predicate: (item: T, index: number, array: Array<T>) => boolean, thisArg?: any, fromIndex?: number
): number;
remove(value: T): boolean;
}

Expand Down
12 changes: 10 additions & 2 deletions src/types/observablearray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface IObservableArray<T> extends Array<T> {
peek(): T[];
replace(newItems: T[]): T[];
find(predicate: (item: T, index: number, array: IObservableArray<T>) => boolean, thisArg?: any, fromIndex?: number): T;
findIndex(predicate: (item: T, index: number, array: IObservableArray<T>) => boolean, thisArg?: any, fromIndex?: number): number;
remove(value: T): boolean;
move(fromIndex: number, toIndex: number): void;
}
Expand Down Expand Up @@ -299,12 +300,18 @@ export class ObservableArray<T> extends StubArray {

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
find(predicate: (item: T, index: number, array: ObservableArray<T>) => 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<T>) => 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;
}

/*
Expand Down Expand Up @@ -478,6 +485,7 @@ makeNonEnumerable(ObservableArray.prototype, [
"toJSON",
"peek",
"find",
"findIndex",
"splice",
"spliceWithArray",
"push",
Expand Down
8 changes: 7 additions & 1 deletion test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);

Expand Down

0 comments on commit 08e5a4f

Please sign in to comment.