@@ -88,11 +88,11 @@ function UrlMatcher(pattern, config, parentMatcher) {
88
88
parentParams = parentMatcher ? parentMatcher . params : { } ,
89
89
params = this . params = parentMatcher ? parentMatcher . params . $$new ( ) : new $$UMFP . ParamSet ( ) ;
90
90
91
- function addParameter ( id , type , config , isSearch ) {
91
+ function addParameter ( id , type , config , location ) {
92
92
if ( parentParams [ id ] ) return parentParams [ id ] ;
93
93
if ( ! / ^ \w + ( - + \w + ) * (?: \[ \] ) ? $ / . test ( id ) ) throw new Error ( "Invalid parameter name '" + id + "' in pattern '" + pattern + "'" ) ;
94
94
if ( params [ id ] ) throw new Error ( "Duplicate parameter name '" + id + "' in pattern '" + pattern + "'" ) ;
95
- params [ id ] = new $$UMFP . Param ( id , type , config , isSearch ) ;
95
+ params [ id ] = new $$UMFP . Param ( id , type , config , location ) ;
96
96
return params [ id ] ;
97
97
}
98
98
@@ -128,7 +128,7 @@ function UrlMatcher(pattern, config, parentMatcher) {
128
128
p = matchDetails ( m , false ) ;
129
129
if ( p . segment . indexOf ( '?' ) >= 0 ) break ; // we're into the search part
130
130
131
- param = addParameter ( p . id , p . type , p . cfg , false ) ;
131
+ param = addParameter ( p . id , p . type , p . cfg , "path" ) ;
132
132
compiled += quoteRegExp ( p . segment , param . type . pattern . source , param . squash ) ;
133
133
segments . push ( p . segment ) ;
134
134
last = placeholder . lastIndex ;
@@ -147,7 +147,7 @@ function UrlMatcher(pattern, config, parentMatcher) {
147
147
last = 0 ;
148
148
while ( ( m = searchPlaceholder . exec ( search ) ) ) {
149
149
p = matchDetails ( m , true ) ;
150
- param = addParameter ( p . id , p . type , p . cfg , true ) ;
150
+ param = addParameter ( p . id , p . type , p . cfg , "search" ) ;
151
151
last = placeholder . lastIndex ;
152
152
// check if ?&
153
153
}
@@ -803,7 +803,7 @@ function $UrlMatcherFactory() {
803
803
if ( ! isDefined ( definition ) ) return $types [ name ] ;
804
804
if ( $types . hasOwnProperty ( name ) ) throw new Error ( "A type named '" + name + "' has already been defined." ) ;
805
805
806
- $types [ name ] = new Type ( extend ( { } , { name : name } , definition ) ) ;
806
+ $types [ name ] = new Type ( extend ( { name : name } , definition ) ) ;
807
807
if ( definitionFn ) {
808
808
typeQueue . push ( { name : name , def : definitionFn } ) ;
809
809
if ( ! enqueue ) flushTypeQueue ( ) ;
@@ -821,7 +821,7 @@ function $UrlMatcherFactory() {
821
821
}
822
822
823
823
// Register default types. Store them in the prototype of $types.
824
- forEach ( defaultTypes , function ( type , name ) { $types [ name ] = new Type ( type ) ; } ) ;
824
+ forEach ( defaultTypes , function ( type , name ) { $types [ name ] = new Type ( extend ( { name : name } , type ) ) ; } ) ;
825
825
$types = inherit ( $types , { } ) ;
826
826
827
827
/* No need to document $get, since it returns this */
@@ -836,13 +836,15 @@ function $UrlMatcherFactory() {
836
836
return this ;
837
837
} ] ;
838
838
839
- this . Param = function Param ( id , type , config , isSearch ) {
839
+ this . Param = function Param ( id , type , config , location ) {
840
840
var self = this ;
841
841
var defaultValueConfig = getDefaultValueConfig ( config ) ;
842
842
config = config || { } ;
843
843
type = getType ( config , type ) ;
844
844
var arrayMode = getArrayMode ( ) ;
845
- type = arrayMode ? type . $asArray ( arrayMode , isSearch ) : type ;
845
+ type = arrayMode ? type . $asArray ( arrayMode , location === "search" ) : type ;
846
+ if ( type . name === "string" && ! arrayMode && location === "path" && defaultValueConfig . value === undefined )
847
+ defaultValueConfig . value = "" ; // for 0.2.x; in 0.3.0+ do not automatically default to ""
846
848
var isOptional = defaultValueConfig . value !== undefined ;
847
849
var squash = getSquashPolicy ( config , isOptional ) ;
848
850
var replace = getReplace ( config , arrayMode , isOptional , squash ) ;
@@ -852,10 +854,11 @@ function $UrlMatcherFactory() {
852
854
var isShorthand = indexOf ( keys , "value" ) === - 1 && indexOf ( keys , "type" ) === - 1 &&
853
855
indexOf ( keys , "squash" ) === - 1 && indexOf ( keys , "array" ) === - 1 ;
854
856
var configValue = isShorthand ? config : config . value ;
855
- return {
856
- fn : isInjectable ( configValue ) ? configValue : function ( ) { return configValue ; } ,
857
+ var result = {
858
+ fn : isInjectable ( configValue ) ? configValue : function ( ) { return result . value ; } ,
857
859
value : configValue
858
860
} ;
861
+ return result ;
859
862
}
860
863
861
864
function getType ( config , urlType ) {
@@ -867,7 +870,7 @@ function $UrlMatcherFactory() {
867
870
868
871
// array config: param name (param[]) overrides default settings. explicit config overrides param name.
869
872
function getArrayMode ( ) {
870
- var arrayDefaults = { array : isSearch ? "auto" : false } ;
873
+ var arrayDefaults = { array : ( location === "search" ? "auto" : false ) } ;
871
874
var arrayParamNomenclature = id . match ( / \[ \] $ / ) ? { array : true } : { } ;
872
875
return extend ( arrayDefaults , arrayParamNomenclature , config ) . array ;
873
876
}
0 commit comments