Skip to content

Commit 2fe0dfd

Browse files
authored
fix: throw form DateToDay (TheAlgorithms#1628)
1 parent 83b4dd8 commit 2fe0dfd

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

‎Conversions/DateToDay.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ const daysNameArr = [
2626
constDateToDay=(date)=>{
2727
// firstly, check that input is a string or not.
2828
if(typeofdate!=='string'){
29-
returnnewTypeError('Argument is not a string.')
29+
thrownewTypeError('Argument is not a string.')
3030
}
3131
// extract the date
3232
let[day,month,year]=date.split('/').map((x)=>Number(x))
3333
// check the data are valid or not.
3434
if(day<1||day>31||month>12||month<1){
35-
returnnewTypeError('Date is not valid.')
35+
thrownewTypeError('Date is not valid.')
3636
}
3737

3838
// In case of Jan and Feb:

‎Conversions/test/DateToDay.test.js‎

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
import{DateToDay}from'../DateToDay'
22

3-
test('The date 18/02/2001 is Sunday',()=>{
4-
constres=DateToDay('18/02/2001')
5-
expect(res).toBe('Sunday')
6-
})
7-
8-
test('The date 18/12/2020 is Friday',()=>{
9-
constres=DateToDay('18/12/2020')
10-
expect(res).toBe('Friday')
11-
})
3+
describe('DateToDay',()=>{
4+
it.each([
5+
['18/02/2001','Sunday'],
6+
['18/12/2020','Friday'],
7+
['12/12/2012','Wednesday'],
8+
['01/01/2001','Monday'],
9+
['1/1/2020','Wednesday'],
10+
['2/3/2014','Sunday'],
11+
['28/2/2017','Tuesday'],
12+
['02/03/2024','Saturday'],
13+
['29/02/2024','Thursday']
14+
])('%s is %s',(date,day)=>{
15+
expect(DateToDay(date)).toBe(day)
16+
})
1217

13-
test('The date 12/12/2012 is Wednesday',()=>{
14-
constres=DateToDay('12/12/2012')
15-
expect(res).toBe('Wednesday')
16-
})
17-
test('The date 01/01/2001 is Monday',()=>{
18-
constres=DateToDay('01/01/2001')
19-
expect(res).toBe('Monday')
20-
})
21-
22-
test('The date 1/1/2020 is Wednesday',()=>{
23-
constres=DateToDay('1/1/2020')
24-
expect(res).toBe('Wednesday')
25-
})
26-
27-
test('The date 2/3/2014 is Sunday',()=>{
28-
constres=DateToDay('2/3/2014')
29-
expect(res).toBe('Sunday')
30-
})
18+
it('should throw when input is not a string',()=>{
19+
expect(()=>DateToDay(100)).toThrowError()
20+
})
3121

32-
test('The date 28/2/2017 is Tuesday',()=>{
33-
constres=DateToDay('28/2/2017')
34-
expect(res).toBe('Tuesday')
22+
it.each(['32/01/2000','00/01/2000','15/00/2000','15/13/2000'])(
23+
'should throw when input is not a correct date %s',
24+
(wrongDate)=>{
25+
expect(()=>DateToDay(wrongDate)).toThrowError()
26+
}
27+
)
3528
})

0 commit comments

Comments
(0)