File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 2
2
const { gcd, highestFirst, formatAspectRatio } = require ( './utils' )
3
3
4
4
module . exports = ( height , width , seperator = ':' ) => {
5
+ if ( typeof height !== 'number' ) {
6
+ throw new Error ( `Invalid height: ${ height } ` )
7
+ }
8
+
9
+ if ( typeof width !== 'number' ) {
10
+ throw new Error ( `Invalid width: ${ width } ` )
11
+ }
12
+
5
13
const [ h , w ] = highestFirst ( height , width )
6
14
const divisor = gcd ( h , w )
7
15
return formatAspectRatio ( h , w , divisor , seperator )
Original file line number Diff line number Diff line change @@ -19,4 +19,34 @@ describe('aspect ratio', () => {
19
19
it ( 'using other separator' , ( ) => {
20
20
should ( aspectRatio ( 1920 , 1080 , '/' ) ) . equal ( '16/9' )
21
21
} )
22
+
23
+ it ( 'invalid width' , ( ) => {
24
+ let error = ''
25
+ try {
26
+ should ( aspectRatio ( 1920 , null , '/' ) ) . equal ( '16/9' )
27
+ } catch ( e ) {
28
+ error = e . message
29
+ }
30
+ should ( error ) . equal ( 'Invalid width: null' )
31
+ } )
32
+
33
+ it ( 'invalid height' , ( ) => {
34
+ let error = ''
35
+ try {
36
+ should ( aspectRatio ( null , 1080 , '/' ) ) . equal ( '16/9' )
37
+ } catch ( e ) {
38
+ error = e . message
39
+ }
40
+ should ( error ) . equal ( 'Invalid height: null' )
41
+ } )
42
+
43
+ it ( 'invalid width & height' , ( ) => {
44
+ let error = ''
45
+ try {
46
+ should ( aspectRatio ( null , null ) ) . equal ( '16/9' )
47
+ } catch ( e ) {
48
+ error = e . message
49
+ }
50
+ should ( error ) . equal ( 'Invalid height: null' )
51
+ } )
22
52
} )
You can’t perform that action at this time.
0 commit comments