Skip to content

Commit 699a1d6

Browse files
authored
maximum call stack exceeded when no width and height is sent (#14)
maximum call stack exceeded when no width and height is sent
2 parents 4d822d9 + d606bbb commit 699a1d6

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
const { gcd, highestFirst, formatAspectRatio } = require('./utils')
33

44
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+
513
const [h, w] = highestFirst(height, width)
614
const divisor = gcd(h, w)
715
return formatAspectRatio(h, w, divisor, seperator)

test/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,34 @@ describe('aspect ratio', () => {
1919
it('using other separator', () => {
2020
should(aspectRatio(1920, 1080, '/')).equal('16/9')
2121
})
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+
})
2252
})

0 commit comments

Comments
 (0)