mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
fix: throw
form DateToDay
(#1628)
This commit is contained in:
@ -26,13 +26,13 @@ const daysNameArr = [
|
|||||||
const DateToDay = (date) => {
|
const DateToDay = (date) => {
|
||||||
// firstly, check that input is a string or not.
|
// firstly, check that input is a string or not.
|
||||||
if (typeof date !== 'string') {
|
if (typeof date !== 'string') {
|
||||||
return new TypeError('Argument is not a string.')
|
throw new TypeError('Argument is not a string.')
|
||||||
}
|
}
|
||||||
// extract the date
|
// extract the date
|
||||||
let [day, month, year] = date.split('/').map((x) => Number(x))
|
let [day, month, year] = date.split('/').map((x) => Number(x))
|
||||||
// check the data are valid or not.
|
// check the data are valid or not.
|
||||||
if (day < 1 || day > 31 || month > 12 || month < 1) {
|
if (day < 1 || day > 31 || month > 12 || month < 1) {
|
||||||
return new TypeError('Date is not valid.')
|
throw new TypeError('Date is not valid.')
|
||||||
}
|
}
|
||||||
|
|
||||||
// In case of Jan and Feb:
|
// In case of Jan and Feb:
|
||||||
|
@ -1,35 +1,28 @@
|
|||||||
import { DateToDay } from '../DateToDay'
|
import { DateToDay } from '../DateToDay'
|
||||||
|
|
||||||
test('The date 18/02/2001 is Sunday', () => {
|
describe('DateToDay', () => {
|
||||||
const res = DateToDay('18/02/2001')
|
it.each([
|
||||||
expect(res).toBe('Sunday')
|
['18/02/2001', 'Sunday'],
|
||||||
})
|
['18/12/2020', 'Friday'],
|
||||||
|
['12/12/2012', 'Wednesday'],
|
||||||
|
['01/01/2001', 'Monday'],
|
||||||
|
['1/1/2020', 'Wednesday'],
|
||||||
|
['2/3/2014', 'Sunday'],
|
||||||
|
['28/2/2017', 'Tuesday'],
|
||||||
|
['02/03/2024', 'Saturday'],
|
||||||
|
['29/02/2024', 'Thursday']
|
||||||
|
])('%s is %s', (date, day) => {
|
||||||
|
expect(DateToDay(date)).toBe(day)
|
||||||
|
})
|
||||||
|
|
||||||
test('The date 18/12/2020 is Friday', () => {
|
it('should throw when input is not a string', () => {
|
||||||
const res = DateToDay('18/12/2020')
|
expect(() => DateToDay(100)).toThrowError()
|
||||||
expect(res).toBe('Friday')
|
})
|
||||||
})
|
|
||||||
|
|
||||||
test('The date 12/12/2012 is Wednesday', () => {
|
it.each(['32/01/2000', '00/01/2000', '15/00/2000', '15/13/2000'])(
|
||||||
const res = DateToDay('12/12/2012')
|
'should throw when input is not a correct date %s',
|
||||||
expect(res).toBe('Wednesday')
|
(wrongDate) => {
|
||||||
})
|
expect(() => DateToDay(wrongDate)).toThrowError()
|
||||||
test('The date 01/01/2001 is Monday', () => {
|
}
|
||||||
const res = DateToDay('01/01/2001')
|
)
|
||||||
expect(res).toBe('Monday')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('The date 1/1/2020 is Wednesday', () => {
|
|
||||||
const res = DateToDay('1/1/2020')
|
|
||||||
expect(res).toBe('Wednesday')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('The date 2/3/2014 is Sunday', () => {
|
|
||||||
const res = DateToDay('2/3/2014')
|
|
||||||
expect(res).toBe('Sunday')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('The date 28/2/2017 is Tuesday', () => {
|
|
||||||
const res = DateToDay('28/2/2017')
|
|
||||||
expect(res).toBe('Tuesday')
|
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user