@@ -499,22 +499,37 @@ Type.prototype.$asArray = function(mode, isSearch) {
499
499
} ;
500
500
}
501
501
502
- function arrayHandler ( callback , reducefn ) {
503
- // Wraps type functions to operate on each value of an array
502
+ function toArray ( val ) { return isArray ( val ) ? val : [ val ] }
503
+ function fromArray ( val ) { return mode === "auto" && val && val . length === 1 ? val [ 0 ] : val ; }
504
+ function falsey ( val ) { return ! val ; }
505
+
506
+ // Wraps type (.is/.encode/.decode) functions to operate on each value of an array
507
+ function arrayHandler ( callback , alltrue ) {
504
508
return function handleArray ( val ) {
505
- if ( ! isArray ( val ) ) val = [ val ] ;
509
+ val = toArray ( val ) ;
506
510
var result = map ( val , callback ) ;
507
- if ( reducefn )
508
- return result . reduce ( reducefn , true ) ;
509
- return ( result && result . length == 1 && mode === "auto" ) ? result [ 0 ] : result ;
511
+ if ( alltrue === true )
512
+ return result . filter ( falsey ) . length === 0 ;
513
+ return fromArray ( result ) ;
514
+ } ;
515
+ }
516
+
517
+ // Wraps type (.equals) functions to operate on each value of an array
518
+ function arrayEqualsHandler ( callback ) {
519
+ return function handleArray ( val1 , val2 ) {
520
+ var left = toArray ( val1 ) , right = toArray ( val2 ) ;
521
+ if ( left . length !== right . length ) return false ;
522
+ for ( var i = 0 ; i < left . length ; i ++ ) {
523
+ if ( ! callback ( left [ i ] , right [ i ] ) ) return false ;
524
+ }
525
+ return true ;
510
526
} ;
511
527
}
512
528
513
- function alltruthy ( val , memo ) { return val && memo ; }
514
529
this . encode = arrayHandler ( bindTo ( this , type . encode ) ) ;
515
530
this . decode = arrayHandler ( bindTo ( this , type . decode ) ) ;
516
- this . equals = arrayHandler ( bindTo ( this , type . equals ) , alltruthy ) ;
517
- this . is = arrayHandler ( bindTo ( this , type . is ) , alltruthy ) ;
531
+ this . is = arrayHandler ( bindTo ( this , type . is ) , true ) ;
532
+ this . equals = arrayEqualsHandler ( bindTo ( this , type . equals ) ) ;
518
533
this . pattern = type . pattern ;
519
534
this . $arrayMode = mode ;
520
535
}
0 commit comments