Skip to content

Commit 3e5d34d

Browse files
author
Vlad Balin
committed
filter methods supports object as parameter
1 parent 7855c8d commit 3e5d34d

File tree

6 files changed

+59
-20
lines changed

6 files changed

+59
-20
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/collection/index.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface CollectionOptions extends TransactionOptions {
77
comparator?: GenericComparator;
88
model?: typeof Record;
99
}
10+
export declare type Predicate = (val: Record, key: number) => boolean | object;
1011
export interface CollectionDefinition extends TransactionalDefinition {
1112
model?: Record;
1213
itemEvents?: EventsDefinition;
@@ -33,9 +34,9 @@ export declare class Collection extends Transactional implements CollectionCore
3334
_onChildrenChange(record: Record, options?: TransactionOptions, initiator?: Transactional): void;
3435
get(objOrId: string | Record | Object): Record;
3536
each(iteratee: (val: Record, key: number) => void, context?: any): void;
36-
every(iteratee: (val: Record, key: number) => boolean, context?: any): boolean;
37-
filter(iteratee: (val: Record, key: number) => boolean, context?: any): Record[];
38-
some(iteratee: (val: Record, key: number) => boolean, context?: any): boolean;
37+
every(iteratee: Predicate, context?: any): boolean;
38+
filter(iteratee: Predicate, context?: any): Record[];
39+
some(iteratee: Predicate, context?: any): boolean;
3940
map<T>(iteratee: (val: Record, key: number) => T, context?: any): T[];
4041
_validateNested(errors: {}): number;
4142
model: typeof Record;

lib/collection/index.js

Lines changed: 20 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/collection/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/collection/index.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export interface CollectionOptions extends TransactionOptions {
2323
model? : typeof Record
2424
}
2525

26+
export type Predicate = ( val : Record, key : number ) => boolean | object;
27+
2628
export interface CollectionDefinition extends TransactionalDefinition {
2729
model? : Record,
2830
itemEvents? : EventsDefinition
@@ -181,16 +183,16 @@ export class Collection extends Transactional implements CollectionCore {
181183
}
182184

183185
each( iteratee : ( val : Record, key : number ) => void, context? : any ){
184-
const fun = context !== void 0 ? ( v, k ) => iteratee.call( context, v, k ) : iteratee,
186+
const fun = bindContext( iteratee, context ),
185187
{ models } = this;
186188

187189
for( let i = 0; i < models.length; i++ ){
188190
fun( models[ i ], i );
189191
}
190192
}
191193

192-
every( iteratee : ( val : Record, key : number ) => boolean, context? : any ) : boolean {
193-
const fun = context !== void 0 ? ( v, k ) => iteratee.call( context, v, k ) : iteratee,
194+
every( iteratee : Predicate, context? : any ) : boolean {
195+
const fun = toPredicateFunction( iteratee, context ),
194196
{ models } = this;
195197

196198
for( let i = 0; i < models.length; i++ ){
@@ -200,15 +202,15 @@ export class Collection extends Transactional implements CollectionCore {
200202
return true;
201203
}
202204

203-
filter( iteratee : ( val : Record, key : number ) => boolean, context? : any ) : Record[] {
204-
const fun = context !== void 0 ? ( v, k ) => iteratee.call( context, v, k ) : iteratee,
205+
filter( iteratee : Predicate, context? : any ) : Record[] {
206+
const fun = toPredicateFunction( iteratee, context ),
205207
{ models } = this;
206208

207209
return this.map( ( x, i ) => fun( x, i ) ? x : void 0 );
208210
}
209211

210-
some( iteratee : ( val : Record, key : number ) => boolean, context? : any ) : boolean {
211-
const fun = context !== void 0 ? ( v, k ) => iteratee.call( context, v, k ) : iteratee,
212+
some( iteratee : Predicate, context? : any ) : boolean {
213+
const fun = toPredicateFunction( iteratee, context ),
212214
{ models } = this;
213215

214216
for( let i = 0; i < models.length; i++ ){
@@ -219,7 +221,7 @@ export class Collection extends Transactional implements CollectionCore {
219221
}
220222

221223
map< T >( iteratee : ( val : Record, key : number ) => T, context? : any ) : T[]{
222-
const fun = context !== void 0 ? ( v, k ) => iteratee.call( context, v, k ) : iteratee,
224+
const fun = bindContext( iteratee, context ),
223225
{ models } = this,
224226
mapped = Array( models.length );
225227

@@ -504,4 +506,25 @@ class CollectionRefsType extends SharedType {
504506

505507
createSharedTypeSpec( Collection, SharedType );
506508

507-
Record.Collection = <any>Collection;
509+
Record.Collection = <any>Collection;
510+
511+
function bindContext( fun : Function, context? : any ){
512+
return context !== void 0 ? ( v, k ) => fun.call( context, v, k ) : fun;
513+
}
514+
515+
function toPredicateFunction( iteratee : Predicate, context ){
516+
if( typeof iteratee === 'object' ){
517+
// Wrap object to the predicate...
518+
return x => {
519+
for( let key in iteratee as any ){
520+
if( iteratee[ key ] !== x[ key ] )
521+
return false;
522+
}
523+
524+
return true;
525+
}
526+
}
527+
528+
return bindContext( iteratee, context );
529+
530+
}

0 commit comments

Comments
 (0)