더북(TheBook)

다음 예제는 전체 코드다.

예제 3-12 객체 주입하기

import moment from "moment";

const RealTimeProvider = () => {
    this.getDay = () => moment().day();
};

const SUNDAY = 0, MONDAY = 1, SATURDAY = 6;

class PasswordVerifier {
    constructor(rules, timeProvider) {
        this.rules = rules;
        this.timeProvider = timeProvider;
    }

    verify(input) {
        if ([SATURDAY, SUNDAY].includes(this.timeProvider.getDay())) {
            throw new Error("It's the weekend!");
        }
        const errors = [];
        // 더 많은 코드가 여기에 있다.
        return errors;
    }
}


신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.