Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObservableArray#findIndex #936

Merged
merged 5 commits into from
Apr 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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