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

Diff for: Conversions/DateToDay.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ const daysNameArr = [
2626
const DateToDay = (date) => {
2727
// firstly, check that input is a string or not.
2828
if (typeof date !== 'string') {
29-
return new TypeError('Argument is not a string.')
29+
throw new TypeError('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-
return new TypeError('Date is not valid.')
35+
throw new TypeError('Date is not valid.')
3636
}
3737

3838
// In case of Jan and Feb:

Diff for: Conversions/test/DateToDay.test.js

+23-30
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-
const res = DateToDay('18/02/2001')
5-
expect(res).toBe('Sunday')
6-
})
7-
8-
test('The date 18/12/2020 is Friday', () => {
9-
const res = 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-
const res = DateToDay('12/12/2012')
15-
expect(res).toBe('Wednesday')
16-
})
17-
test('The date 01/01/2001 is Monday', () => {
18-
const res = DateToDay('01/01/2001')
19-
expect(res).toBe('Monday')
20-
})
21-
22-
test('The date 1/1/2020 is Wednesday', () => {
23-
const res = DateToDay('1/1/2020')
24-
expect(res).toBe('Wednesday')
25-
})
26-
27-
test('The date 2/3/2014 is Sunday', () => {
28-
const res = 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-
const res = 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)