File tree Expand file tree Collapse file tree 3 files changed +29
-16
lines changed Expand file tree Collapse file tree 3 files changed +29
-16
lines changed Original file line number Diff line number Diff line change 1
1
// Jest Snapshot v1, https://goo.gl/fbAQLP
2
2
3
- exports [` processAspectRatio should not accept invalid formats 1` ] = ` "aspectRatio must either be a number or a ratio. You passed: -1 "` ;
3
+ exports [` processAspectRatio should not accept invalid formats 1` ] = ` "aspectRatio must either be a number, a ratio or \` auto \` . You passed: 0a "` ;
4
4
5
- exports [` processAspectRatio should not accept invalid formats 2` ] = ` "aspectRatio must either be a number or a ratio. You passed: 0a "` ;
5
+ exports [` processAspectRatio should not accept invalid formats 2` ] = ` "aspectRatio must either be a number, a ratio or \` auto \` . You passed: 1 / 1 1 "` ;
6
6
7
- exports [` processAspectRatio should not accept invalid formats 3` ] = ` "aspectRatio must either be a number or a ratio. You passed: / 0"` ;
8
-
9
- exports [` processAspectRatio should not accept invalid formats 4` ] = ` "aspectRatio must either be a number or a ratio. You passed: 1 / 1 1"` ;
7
+ exports [` processAspectRatio should not accept invalid formats 3` ] = ` "aspectRatio must either be a number, a ratio or \` auto\` . You passed: auto 1/1"` ;
Original file line number Diff line number Diff line change @@ -28,6 +28,12 @@ describe('processAspectRatio', () => {
28
28
expect ( processAspectRatio ( ' 0 ' ) ) . toBe ( 0 ) ;
29
29
} ) ;
30
30
31
+ it ( 'should accept `auto`' , ( ) => {
32
+ expect ( processAspectRatio ( 'auto' ) ) . toBe ( undefined ) ;
33
+ expect ( processAspectRatio ( ' auto' ) ) . toBe ( undefined ) ;
34
+ expect ( processAspectRatio ( ' auto ' ) ) . toBe ( undefined ) ;
35
+ } ) ;
36
+
31
37
it ( 'should accept ratios' , ( ) => {
32
38
expect ( processAspectRatio ( '+1/1' ) ) . toBe ( 1 ) ;
33
39
expect ( processAspectRatio ( '0 / 10' ) ) . toBe ( 0 ) ;
@@ -37,9 +43,8 @@ describe('processAspectRatio', () => {
37
43
} ) ;
38
44
39
45
it ( 'should not accept invalid formats' , ( ) => {
40
- expect ( ( ) => processAspectRatio ( '-1' ) ) . toThrowErrorMatchingSnapshot ( ) ;
41
46
expect ( ( ) => processAspectRatio ( '0a' ) ) . toThrowErrorMatchingSnapshot ( ) ;
42
- expect ( ( ) => processAspectRatio ( '/ 0' ) ) . toThrowErrorMatchingSnapshot ( ) ;
43
47
expect ( ( ) => processAspectRatio ( '1 / 1 1' ) ) . toThrowErrorMatchingSnapshot ( ) ;
48
+ expect ( ( ) => processAspectRatio ( 'auto 1/1' ) ) . toThrowErrorMatchingSnapshot ( ) ;
44
49
} ) ;
45
50
} ) ;
Original file line number Diff line number Diff line change @@ -17,27 +17,37 @@ function processAspectRatio(aspectRatio: number | string): ?number {
17
17
return aspectRatio ;
18
18
}
19
19
20
- const match = new RegExp (
21
- / ( ^ \s * [ + ] ? \d + ( [ . ] \d + ) ? \s * $ ) | ( ^ \s * ( [ + ] ? \d + ( [ . ] \d + ) ? ) \s * \/ \s * ( [ + ] ? \d + ( [ . ] \d + ) ? ) \s * $ ) / ,
22
- ) . exec ( aspectRatio ) ;
20
+ const matches = aspectRatio . split ( '/' ) . map ( s => s . trim ( ) ) ;
21
+
22
+ if ( matches . includes ( 'auto' ) ) {
23
+ if ( __DEV__ ) {
24
+ invariant (
25
+ matches . length ,
26
+ 'aspectRatio does not support `auto <ratio>`. You passed: %s' ,
27
+ aspectRatio ,
28
+ ) ;
29
+ }
30
+ return ;
31
+ }
23
32
33
+ const hasNonNumericValues = matches . some ( n => Number . isNaN ( Number ( n ) ) ) ;
24
34
if ( __DEV__ ) {
25
35
invariant (
26
- Boolean ( match ) ,
27
- 'aspectRatio must either be a number or a ratio. You passed: %s' ,
36
+ ! hasNonNumericValues && ( matches . length === 1 || matches . length === 2 ) ,
37
+ 'aspectRatio must either be a number, a ratio or `auto` . You passed: %s' ,
28
38
aspectRatio,
29
39
) ;
30
40
}
31
41
32
- if ( ! match ) {
42
+ if ( hasNonNumericValues ) {
33
43
return;
34
44
}
35
45
36
- if ( match [ 4 ] !== undefined ) {
37
- return Number ( match [ 4 ] ) / N u m b e r ( m a t c h [ 6 ] ) ;
46
+ if ( matches . length === 2 ) {
47
+ return Number ( matches [ 0 ] ) / N u m b e r ( m a t c h e s [ 1 ] ) ;
38
48
}
39
49
40
- return Number ( match [ 1 ] ) ;
50
+ return Number ( matches [ 0 ] ) ;
41
51
}
42
52
43
53
module . exports = processAspectRatio ;
You can’t perform that action at this time.
0 commit comments