2021.08.17

이짜젠·2021년 8월 17일
0

긴 연휴가 끝났다.
공부해야지! 생각만하고 탱자탱자놀았다.

출근전날이되니 밀려있는 개발에 조금 마음이 불안했다.
학창시절 시험기간때도 그랬지만, 불안할때 계획을 세워놓으면 마음이 조금 편안해지는 것 같다.
계획을 완벽히 지키진 못하더라도, 하나씩 계획을 완수해나가는 맛이있다.

클래스의 위엄

특정 데이터를가지고 여러형태로 파싱하는 일이 생겼다.

const formattedMsgList = msgList.map((m, idx, arr) => {
  const prev = arr?.[idx - 1];
  const next = arr?.[idx - 1];
  
  let result = formatDate(m, prevMsg, nextMsg);
  result = foramtSender(m);
  result = formatTail(m);
  
  return result;
})

function formatDate(msg, prev) {
  // msg, prevMsg을 비교하여 dateTimeStr을 원하는 형태로 파싱
  
  const prev = arr?.[idx - 1];
  const next = arr?.[idx - 1];

}

function formatSender(msg) {
    // ... msg 데이터의 필드중 하나인 msgType값을 토대로 원하는 형태로 파싱
}

function formatTail(msg, prev, next) {
    // ... msg, psrvMsg와 비교하여 데이터의 필드중 하나인 dateTimeStr값을 기준으로 마지막 메세지인지를 판단하는 데이터 추가
}

공통된 데이터로 조작하게되는 작업이 빈번하다면 클래스로 묶어버리자.

const formattedMsgList = msgList.map((m, idx, arr) => {
  const prev = arr?.[idx - 1];
  const next = arr?.[idx - 1];
  
  return new FormattedMsg(m, prevMsg, nextMsg);
})

class FormattedMsg {
  private nextMsg = null;
  private prevMsg = null;

  //...

  cosntructor (msg, prevMsg, nextMsg) {
    Object.assign(this, msg);
    this.nextMsg = nextMsg;
    this.prevMsg = prevMsg;
  }


  get date () {
    //...formatDate 로직
  }
  
  get sender() {
    //...formatSender 로직
  }

  get isTail() {
    //...foramtTail 로직
  }
}

타입스크립트에서 Optional Parameter

hoc, hof 형태에서 첫번재파라미터를 생략한 채, 두번째파라미터로 제공되는 값을 사용하고 싶을 때가 있다.

function fn (data: IParams | undefined, data2) {}
profile
오늘 먹은 음식도 기억이 안납니다. 그래서 모든걸 기록합니다.

0개의 댓글