이름 : javascript-lotto-personal
기간 : 2022년 3월
인원 : 1인 (개인)
배포 : 데모로 대체함
Clone the project
git clone https://github.com/dltkdals224/javascript-lotto-personal.git
Go to the project directory
cd javascript-lotto-personal
Install dependencies
npm install
Start the server
npm run start-be
npm run start-fe
소프트웨어 개발에서 사용자 인터페이스, 데이터 및 논리 제어를 구현하는데 널리 사용되는 아키텍처 디자인 패턴 중 하나.
모델(Model), 뷰(View), 그리고 컨트롤러(Controller)로 구성되어, 분리되어 동작함으로써 코드의 재사용성과 유지보수성을 높일 수 있다.
javascript-lotto-personal/src/index.js
Lines 1 to 4 in ef28caf
javascript-lotto-personal/src/fe/js/app.js
Lines 1 to 15 in ef28caf
- Model
javascript-lotto-personal/src/fe/js/components/Model/index.js
Lines 2 to 10 in ef28caf
javascript-lotto-personal/src/fe/js/components/Model/index.js
Lines 12 to 83 in ef28caf
데이터와 데이터의 값이 바뀌는 이벤트에 대해 CustomEvent를 통한 발행을 처리.
커스텀 이벤트를 사용해 이벤트의 세부 정보를 자유롭게 정의하고 모델에서 발생하는 이벤트의 내용을 명확하게 전달.
이를 통해 모델과 뷰, 그리고 컨트롤러 간의 결합도를 낮출 수 있으며, 뷰와 컨트롤러가 모델의 상태 변화를 쉽게 감지하고 이에 따른 업데이트를 수행할 수 있다.
- View
javascript-lotto-personal/src/fe/js/components/View/index.js
Lines 177 to 337 in ef28caf
javascript-lotto-personal/src/fe/js/components/View/index.js
Lines 10 to 174 in ef28caf
최초의 InitView HTML과 UI 관련 업데이트 함수를 작성.
DOM 조작과 관련한 모든 처리를 View 클래스에서 담당하게 하므로써, 코드를 구성 요소로 분리하여 추상화 수준을 유지할 수 있도록 함.
- Controller
javascript-lotto-personal/src/fe/js/components/Controller/index.js
Lines 12 to 53 in ef28caf
javascript-lotto-personal/src/fe/js/components/Controller/index.js
Lines 56 to 223 in ef28caf
Controller의 생성자에서 이벤트 리스너를 등록하고 각 이벤트에 대해 콜백 함수를 정의.
코드가 모듈화되어 의존성을 낮추고, 이벤트 처리 로직이 분리되어 가독성이 향상된다.
코드의 모듈화와 이벤트 리스너 등록의 분리로 코드의 가독성이 높아지도록 작성.
node.js 필드에서 axios와 cheerio를 통해 scrapping을 구현.
특정 회차들의 로또 당첨 번호를 html tag를 통해 가져올 수 있도록 함.
함수형 프로그래밍기반 함수형 프로그래밍 적용 시도.
적용에 lodash 라이브러리를 사용.
적용 전
const DATA_ARRAY = HTML_DATA_ARRAY.map((htmlData, idx) => {
const $ = cheerio.load(htmlData.data);
const DATA = {
lottoRound: ROUND_ARRAY[idx],
lottoNumber: $(
'#main_pack > div.sc_new.cs_lotto._lotto > div > div.content_area > div > div > div:nth-child(2) > div.win_number_box > div'
)
.text()
.split(' ')
.filter(target => target),
};
return DATA;
});
적용 후
const DATA_ARRAY = HTML_DATA_ARRAY.map((htmlData, idx) => {
const $ = cheerio.load(htmlData.data);
const NUMBER_LIST = $(
'#main_pack > div.sc_new.cs_lotto._lotto > div > div.content_area > div > div > div:nth-child(2) > div.win_number_box > div'
).text();
return {
lottoRound: ROUND_ARRAY[idx],
lottoNumber: flow(
trim,
split(' '),
filter(target => target !== '')
)(NUMBER_LIST),
};
});
- util func
javascript-lotto-personal/src/be/util/calculate.util.js
Lines 1 to 28 in ef28caf
Date 객체를 사용해 현재 날짜를 scrap하기 위한 최신 회차로 변환하는 util 함수.
📂 src
├─ 📂 fe
│ ├─ 📂 css
│ └─ 📂 js
│ ├─ 📂 api
│ │ ├─ client
│ │ └─ lotto
│ ├─ 📂 components
│ │ ├─ Model
│ │ ├─ View
│ │ └─ Controller
│ ├─ 📂 constant
│ ├─ 📂 util
│ │ ├─ checkValid
│ │ ├─ generateLottoNumber
│ │ └─ sessionStorage
│ └─ app.js
├─ 📂 be
│ ├─ 📂 bin
│ ├─ 📂 config
│ ├─ 📂 controllers
│ │ └─ lottoNumberScrap.Controller
│ ├─ 📂 routes
│ │ ├─ lottoNumber.route
│ │ └─ staticPage.route
│ ├─ 📂 util
│ │ └─ calculate.util
│ └─ app.js
└─ index.js
javascript-lotto-personal/webpack.config.js
Lines 1 to 46 in ef28caf
javascript-lotto-personal/babel.config.js
Lines 1 to 14 in ef28caf
lodash를 babel을 이용해 번들링하여 모듈 번들러의 크기를 줄임.
babel-loader 가 lodash 모듈에서 사용하지 않는 함수들을 제거 및 번들링할 때 필요한 기능들만을 포함.
이런 최적화 설정을 통해, 모듈 번들러가 최소한의 기능만을 번들링하도록 설정하여 성능이 약간 개선.
--
If you have any feedback, please reach out to us at [email protected]