@@ -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+
2628export 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
505507createSharedTypeSpec ( 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