1
1
import { BUILD } from '@app-data' ;
2
- import { isComplexType , MEMBER_FLAGS } from '@utils' ;
2
+ import { deserializeProperty , isComplexType , MEMBER_FLAGS , SERIALIZED_PREFIX } from '@utils' ;
3
3
4
4
/**
5
5
* Parse a new property value for a given property type.
@@ -24,32 +24,64 @@ import { isComplexType, MEMBER_FLAGS } from '@utils';
24
24
* @param propType the type of the prop, expressed as a binary number
25
25
* @returns the parsed/coerced value
26
26
*/
27
- export const parsePropertyValue = ( propValue : any , propType : number ) : any => {
28
- // ensure this value is of the correct prop type
29
-
30
- if ( propValue != null && ! isComplexType ( propValue ) ) {
31
- if ( BUILD . propBoolean && propType & MEMBER_FLAGS . Boolean ) {
32
- // per the HTML spec, any string value means it is a boolean true value
33
- // but we'll cheat here and say that the string "false" is the boolean false
34
- return propValue === 'false' ? false : propValue === '' || ! ! propValue ;
27
+ export const parsePropertyValue = ( propValue : unknown , propType : number ) : any => {
28
+ /**
29
+ * Allow hydrate parameters that contain a simple object, e.g.
30
+ * ```ts
31
+ * import { renderToString } from 'component-library/hydrate';
32
+ * await renderToString(`<car-detail car=${JSON.stringify({ year: 1234 })}></car-detail>`);
33
+ * ```
34
+ * @deprecated
35
+ */
36
+ if (
37
+ isComplexType ( propValue ) &&
38
+ typeof propValue === 'string' &&
39
+ (
40
+ ( propValue . startsWith ( '{' ) && propValue . endsWith ( '}' ) ) ||
41
+ ( propValue . startsWith ( '[' ) && propValue . endsWith ( ']' ) )
42
+ )
43
+ ) {
44
+ try {
45
+ return JSON . parse ( propValue ) ;
46
+ } catch ( e ) {
47
+ /* ignore */
35
48
}
49
+ }
36
50
37
- if ( BUILD . propNumber && propType & MEMBER_FLAGS . Number ) {
38
- // force it to be a number
39
- return parseFloat ( propValue ) ;
40
- }
51
+ /**
52
+ * Allow hydrate parameters that contain a complex non-serialized values.
53
+ */
54
+ if ( typeof propValue === 'string' && propValue . startsWith ( SERIALIZED_PREFIX ) ) {
55
+ return deserializeProperty ( propValue ) ;
56
+ }
41
57
42
- if ( BUILD . propString && propType & MEMBER_FLAGS . String ) {
43
- // could have been passed as a number or boolean
44
- // but we still want it as a string
45
- return String ( propValue ) ;
46
- }
58
+ /**
59
+ * ensure this value is of the correct prop type
60
+ */
61
+ if ( BUILD . propBoolean && propType & MEMBER_FLAGS . Boolean ) {
62
+ /**
63
+ * per the HTML spec, any string value means it is a boolean true value
64
+ * but we'll cheat here and say that the string "false" is the boolean false
65
+ */
66
+ return propValue === 'false' ? false : propValue === '' || ! ! propValue ;
67
+ }
68
+
69
+ /**
70
+ * force it to be a number
71
+ */
72
+ if ( typeof propValue === 'string' && BUILD . propNumber && propType & MEMBER_FLAGS . Number ) {
73
+ return parseFloat ( propValue ) ;
74
+ }
47
75
48
- // redundant return here for better minification
49
- return propValue ;
76
+ /**
77
+ * could have been passed as a number or boolean but we still want it as a string
78
+ */
79
+ if ( BUILD . propString && propType & MEMBER_FLAGS . String ) {
80
+ return String ( propValue ) ;
50
81
}
51
82
52
- // not sure exactly what type we want
53
- // so no need to change to a different type
83
+ /**
84
+ * not sure exactly what type we want so no need to change to a different type
85
+ */
54
86
return propValue ;
55
87
} ;
0 commit comments