요청 1: 함수 두 개, 규칙을 다 정해 준 요청
요청문: src/stock.mjs에 두 함수를 추가해 줘. expiringSoon(items, today)는 유통기한이 오늘부터 3일 뒤까지(오늘 포함)인 재료 이름 목록을, expired(items, today)는 유통기한이 이미 지난 재료 이름 목록을 돌려줘. today는 'YYYY-MM-DD' 문자열이야. 순서는 들어온 순서 그대로. test/stock.test.mjs에 테스트도 추가하고 돌려 봐.
diff --git a/src/stock.mjs b/src/stock.mjs
index f81b897..0d1b19f 100644
--- a/src/stock.mjs
+++ b/src/stock.mjs
@@ -19,3 +19,16 @@ export function orderList(items) {
.filter((it) => it.qty < it.minQty)
.map((it) => ({ name: it.name, need: it.minQty * 2 - it.qty, unit: it.unit }));
}
+
+export function expiringSoon(items, today) {
+ return items
+ .filter((it) => {
+ const left = daysBetween(today, it.expires);
+ return left >= 0 && left <= 3;
+ })
+ .map((it) => it.name);
+}
+
+export function expired(items, today) {
+ return items.filter((it) => daysBetween(today, it.expires) < 0).map((it) => it.name);
+}
diff --git a/test/stock.test.mjs b/test/stock.test.mjs
index 71de632..89bd877 100644
--- a/test/stock.test.mjs
+++ b/test/stock.test.mjs
@@ -1,5 +1,5 @@
import assert from 'node:assert/strict';
-import { lowStock, orderList, daysBetween } from '../src/stock.mjs';
+import { lowStock, orderList, daysBetween, expiringSoon, expired } from '../src/stock.mjs';
const items = [
{ name: '우유', qty: 3, unit: 'L', expires: '2026-10-20', minQty: 5 },
{ name: '원두', qty: 4, unit: 'kg', expires: '2027-01-10', minQty: 2 },
@@ -8,3 +8,21 @@ assert.deepEqual(lowStock(items), ['우유']);
assert.deepEqual(orderList(items), [{ name: '우유', need: 7, unit: 'L' }]);
assert.equal(daysBetween('2026-10-18', '2026-10-20'), 2);
console.log('기본 테스트 통과');
+
+// 유통기한: 오늘(0일) ~ 3일 뒤 경계, 어제(-1일), 4일 뒤는 제외
+const today = '2026-10-18';
+const shelf = [
+ { name: '생크림', qty: 2, unit: '팩', expires: '2026-10-18', minQty: 2 },
+ { name: '딸기', qty: 1, unit: 'kg', expires: '2026-10-17', minQty: 1 },
+ { name: '바닐라 시럽', qty: 1, unit: '병', expires: '2026-10-21', minQty: 1 },
+ { name: '레몬', qty: 5, unit: '개', expires: '2026-10-22', minQty: 3 },
+ { name: '우유', qty: 3, unit: 'L', expires: '2026-10-20', minQty: 5 },
+ { name: '버터', qty: 1, unit: '개', expires: '2026-09-30', minQty: 1 },
+];
+assert.deepEqual(expiringSoon(shelf, today), ['생크림', '바닐라 시럽', '우유']);
+assert.deepEqual(expired(shelf, today), ['딸기', '버터']);
+// 월이 바뀌는 경계
+assert.deepEqual(expiringSoon([{ name: 'A', expires: '2026-11-01' }], '2026-10-29'), ['A']);
+assert.deepEqual(expiringSoon([], today), []);
+assert.deepEqual(expired([], today), []);
+console.log('유통기한 테스트 통과');