Skip to content

Instantly share code, notes, and snippets.

@indongyoo
Created April 5, 2018 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indongyoo/c0c90241fbb4239ec0f32a0441fd00be to your computer and use it in GitHub Desktop.
Save indongyoo/c0c90241fbb4239ec0f32a0441fd00be to your computer and use it in GitHub Desktop.
const products = [
{ id: 1, selected: true, name: 'A 반팔티', price: 12000, discount: 2000, quantity: 3 },
{ id: 2, selected: false, name: 'B 후드티', price: 30000, discount: 0, quantity: 3 },
{ id: 3, selected: true, name: 'C 폰케이스', price: 20000, discount: 0, quantity: 2 },
{ id: 4, selected: false, name: 'D 반팔티', price: 12000, discount: 2000, quantity: 4 },
{ id: 5, selected: true, name: 'E 쿠션', price: 25000, discount: 5000, quantity: 2 }
];
// 네임 스페이스 - 상품들에 대한 함수들
const pdts = {};
pdts.total = (getter, products, ...before) => go(
products,
...before, // <--- before에 걸리는 함수가 있으면 여기에 추가될 예정
map(getter),
reduce(add));
// 모든 상품 수량을 뽑기 위해 pdts.total의 getter로 사용할 함수로 수량을 뽑는 quantity를 이용
console.log( pdts.total(quantity, products) );
// 14
// 부분 적용을 이용해 다양한 함수를 더 간결하게 생성하고 사용하도록 만들기
Object.assign(pdts, {
quantity: _ => pdts.total(quantity, _),
price: _ => pdts.total(pdt.totalPrice, _),
discountedPrice: _ => pdts.total(pdt.discountedTotalPrice, _)
});
// 모든 상품 수량
console.log( pdts.quantity(products) ); // 14
// 모든 상품 기본 금액 x 수량
console.log( pdts.price(products) ); // 264000
// 모든 상품 할인된 금액 x 수량
console.log( pdts.discountedPrice(products) ); // 24000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment