다음 예제는 전체 코드다.
예제 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;
}
}