@@ -27,12 +27,18 @@ export function validateProp (
2727 const prop = propOptions [ key ]
2828 const absent = ! hasOwn ( propsData , key )
2929 let value = propsData [ key ]
30- // handle boolean props
31- if ( isType ( Boolean , prop . type ) ) {
30+ // boolean casting
31+ const booleanIndex = getTypeIndex ( Boolean , prop . type )
32+ if ( booleanIndex > - 1 ) {
3233 if ( absent && ! hasOwn ( prop , 'default' ) ) {
3334 value = false
34- } else if ( ! isType ( String , prop . type ) && ( value === '' || value === hyphenate ( key ) ) ) {
35- value = true
35+ } else if ( value === '' || value === hyphenate ( key ) ) {
36+ // only cast empty string / same name to boolean if
37+ // boolean has higher priority
38+ const stringIndex = getTypeIndex ( String , prop . type )
39+ if ( stringIndex < 0 || booleanIndex < stringIndex ) {
40+ value = true
41+ }
3642 }
3743 }
3844 // check default value
@@ -179,15 +185,18 @@ function getType (fn) {
179185 return match ? match [ 1 ] : ''
180186}
181187
182- function isType ( type , fn ) {
183- if ( ! Array . isArray ( fn ) ) {
184- return getType ( fn ) === getType ( type )
188+ function isSameType ( a , b ) {
189+ return getType ( a ) === getType ( b )
190+ }
191+
192+ function getTypeIndex ( type , expectedTypes ) : number {
193+ if ( ! Array . isArray ( expectedTypes ) ) {
194+ return isSameType ( expectedTypes , type ) ? 0 : - 1
185195 }
186- for ( let i = 0 , len = fn . length ; i < len ; i ++ ) {
187- if ( getType ( fn [ i ] ) === getType ( type ) ) {
188- return true
196+ for ( let i = 0 , len = expectedTypes . length ; i < len ; i ++ ) {
197+ if ( isSameType ( expectedTypes [ i ] , type ) ) {
198+ return i
189199 }
190200 }
191- /* istanbul ignore next */
192- return false
201+ return - 1
193202}
0 commit comments