Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"private": true,
"scripts": {
"start": "node ./bin/www",
"dev": "cross-env NODE_ENV=development nodemon ./bin/www"
"dev": "cross-env NODE_ENV=development nodemon ./bin/www",
"test": "jest --verbose"
},
"dependencies": {
"@slack/web-api": "^6.5.1",
Expand All @@ -17,5 +18,8 @@
"jade": "~1.11.0",
"morgan": "~1.9.1",
"nodemon": "^2.0.15"
},
"devDependencies": {
"jest": "^27.4.7"
}
}
103 changes: 103 additions & 0 deletions utils/dateParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
function parseDate(stringDate) {
const regNotNumber = /[^0-9]/g;
const regYear = /[0-9]*년/;
const regMonth = /[0-9]*월/;
const regDaily = /매./;
const regDay = /[0-9]*일/;
const regHour = /[0-9]*시/;
const regMinute = /[0-9]*분/;
const regSecond = /[0-9]*초/;
const regWeek = /.요일/;
const regAM = /오전/;
const regTomorrow = /내일/;

function isRegExist(reg, stringDate) {
return reg.test(stringDate);
}

function stringDateSplit(reg, stringDate) {
return parseInt(stringDate.match(reg)[0].replace(regNotNumber, ""));
}

function parsing(reg, stringDate, defaultValue) {
return isRegExist(reg, stringDate)
? stringDateSplit(reg, stringDate)
: defaultValue;
}

function regDailyAndWeekSplit(reg, stringDate) {
return stringDate.match(reg)[0];
}

const year = parsing(regYear, stringDate, new Date().getFullYear());
const month = parsing(regMonth, stringDate, new Date().getMonth() + 1);
const minute = parsing(regMinute, stringDate, 0);
const seconds = parsing(regSecond, stringDate, 0);
let date = parsing(regDay, stringDate, new Date().getDate());
let hour = parsing(regHour, stringDate, 0);

if (!isRegExist(regAM, stringDate)) {
hour += 12;
}

if (isRegExist(regDaily, stringDate)) {
const repeat = regDailyAndWeekSplit(regDaily, stringDate);

if (repeat === "매일") {
return {
repeat,
hour,
minute,
seconds,
};
}

if (repeat === "매주") {
const week = regDailyAndWeekSplit(regWeek, stringDate)[0];

return {
repeat,
week,
hour,
minute,
seconds,
};
}

if (repeat === "매달") {
return {
repeat,
date,
hour,
minute,
seconds,
};
}
}

if (isRegExist(regTomorrow, stringDate)) {
date = new Date().getDate() + 1;

return {
year,
month,
date,
hour,
minute,
seconds,
};
}

return {
year,
month,
date,
hour,
minute,
seconds,
};
}

module.exports = {
parseDate,
};
114 changes: 114 additions & 0 deletions utils/dateParser.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const { parseDate } = require("./dateParser");

const answer1 = {
year: 2022,
month: 1,
date: 9,
hour: 19,
minute: 30,
seconds: 0,
};

const answer2 = {
repeat: "매주",
week: "수",
hour: 19,
minute: 30,
seconds: 0,
};

const answer3 = {
year: new Date().getFullYear(),
month: new Date().getMonth() + 1,
date: new Date().getDate(),
hour: 17,
minute: 0,
seconds: 0,
};

const answer4 = {
year: new Date().getFullYear(),
month: new Date().getMonth() + 1,
date: new Date().getDate(),
hour: 18,
minute: 30,
seconds: 0,
};

const answer5 = {
repeat: "매달",
date: 15,
hour: 10,
minute: 30,
seconds: 0,
};

const answer6 = {
repeat: "매일",
hour: 9,
minute: 30,
seconds: 0,
};

const answer7 = {
...answer3,
hour: 10,
};

const answer8 = {
...answer3,
date: new Date().getDate() + 1,
hour: 19,
};

const testCases = [
{
case: "2022년 1월 9일 오후 7시 30분",
answer: answer1,
},
{
case: "1월 9일 오후 7시 30분",
answer: answer1,
},
{
case: "매주 수요일 오후 7시 30분",
answer: answer2,
},
{
case: "5시에",
answer: answer3,
},
{
case: "6시 30분에",
answer: answer4,
},
{
case: "매달 15일 오전 10시 30분",
answer: answer5,
},
{
case: "매일 오전 9시 30분",
answer: answer6,
},
{
case: "오전 10시에",
answer: answer7,
},
{
case: "내일 오후 7시",
answer: answer8,
},
{
case: "오늘 오후 5시",
answer: answer3,
},
{
case: "9일 오후 7시 30분",
answer: answer1,
},
];
for (const testCase of testCases) {
it(testCase.case, () => {
expect(parseDate(testCase.case)).toStrictEqual(testCase.answer);
});
}
Loading