From 07657345140fb951ebf7c9eb26bbd848388ab245 Mon Sep 17 00:00:00 2001 From: Starcea Date: Thu, 6 Apr 2023 19:18:15 +0900 Subject: [PATCH] feat: Meal class --- src/structures/index.ts | 1 + src/structures/meal.ts | 65 ++++++++++++++++++++++++++++++++++++++++ src/structures/school.ts | 26 ++++++++++------ tests/meal.test.ts | 12 ++++---- 4 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 src/structures/meal.ts diff --git a/src/structures/index.ts b/src/structures/index.ts index 37d58be..50bf2d2 100644 --- a/src/structures/index.ts +++ b/src/structures/index.ts @@ -1 +1,2 @@ export * from './school' +export * from './meal' diff --git a/src/structures/meal.ts b/src/structures/meal.ts new file mode 100644 index 0000000..8d4824d --- /dev/null +++ b/src/structures/meal.ts @@ -0,0 +1,65 @@ +import type { MealInfo } from '../types' + +export class Meal implements MealInfo { + ATPT_OFCDC_SC_CODE: string + ATPT_OFCDC_SC_NM: string + SD_SCHUL_CODE: string + SCHUL_NM: string + + MMEAL_SC_CODE: string + MMEAL_SC_NM: string + MLSV_YMD: string + MLSV_FGR: string + DDISH_NM: string + ORPLC_INFO: string + CAL_INFO: string + NTR_INFO: string + MLSV_FROM_YMD: string + MLSV_TO_YMD: string + + constructor(meal: MealInfo) { + this.ATPT_OFCDC_SC_CODE = meal.ATPT_OFCDC_SC_CODE + this.ATPT_OFCDC_SC_NM = meal.ATPT_OFCDC_SC_NM + this.SD_SCHUL_CODE = meal.SD_SCHUL_CODE + this.SCHUL_NM = meal.SCHUL_NM + + this.MMEAL_SC_CODE = meal.MMEAL_SC_CODE + this.MMEAL_SC_NM = meal.MMEAL_SC_NM + this.MLSV_YMD = meal.MLSV_YMD + this.MLSV_FGR = meal.MLSV_FGR + this.DDISH_NM = meal.DDISH_NM + this.ORPLC_INFO = meal.ORPLC_INFO + this.CAL_INFO = meal.CAL_INFO + this.NTR_INFO = meal.NTR_INFO + this.MLSV_FROM_YMD = meal.MLSV_FROM_YMD + this.MLSV_TO_YMD = meal.MLSV_TO_YMD + } + + getDish(): string[] { + return this.DDISH_NM.split('
').map((dish) => + dish.replace(/ {2,}(?!(\((\d{1,2}\.)+\)))/g, '') + ) + } + + getDishName(): string[] { + return this.DDISH_NM.replace(/ {2,}.*/g, '').split('
') + } + + getOrigin(): { name: string; origin: string }[] { + return this.ORPLC_INFO.split('
').map((origin) => { + const [name, originName] = origin.split(' : ') + return { name, origin: originName } + }) + } + + getCal(): number { + return parseFloat(this.CAL_INFO.replace(/[^0-9.]/g, '')) + } + + getNutrition(): { name: string; amount: number }[] { + return this.NTR_INFO.split('
').map((nutrition) => { + const [name, amount] = nutrition.split(' : ') + return { name, amount: parseFloat(amount) } + }) + } +} diff --git a/src/structures/school.ts b/src/structures/school.ts index 2c008cf..28d89ea 100644 --- a/src/structures/school.ts +++ b/src/structures/school.ts @@ -1,11 +1,11 @@ import type { NeisRequest } from '../http' import type { - MealInfo, MealRequestParam, ScheduleInfo, ScheduleRequestParam, SchoolInfo, } from '../types' +import { Meal } from './meal' /** 학교 정보를 담는 클래스입니다. */ export class School implements SchoolInfo { @@ -67,16 +67,24 @@ export class School implements SchoolInfo { this.#neis = neis } - async getMeal(params: MealRequestParam): Promise { - return await this.#neis.mealServiceDietInfoRaw({ - ATPT_OFCDC_SC_CODE: this.ATPT_OFCDC_SC_CODE, - SD_SCHUL_CODE: this.SD_SCHUL_CODE, - ...params, - }) + async getMeal(params: MealRequestParam): Promise { + return await this.#neis + .mealServiceDietInfoRaw({ + ATPT_OFCDC_SC_CODE: this.ATPT_OFCDC_SC_CODE, + SD_SCHUL_CODE: this.SD_SCHUL_CODE, + ...params, + }) + .then((data) => data.map((meal) => new Meal(meal))) } - async getMealOne(params: MealRequestParam): Promise { - return await this.getMeal(params).then((data) => data[0]) + async getMealOne(params: MealRequestParam): Promise { + return await this.#neis + .mealServiceDietInfoRaw({ + ATPT_OFCDC_SC_CODE: this.ATPT_OFCDC_SC_CODE, + SD_SCHUL_CODE: this.SD_SCHUL_CODE, + ...params, + }) + .then((data) => new Meal(data[0])) } async getSchedule(params: ScheduleRequestParam): Promise { diff --git a/tests/meal.test.ts b/tests/meal.test.ts index b306554..4404fad 100644 --- a/tests/meal.test.ts +++ b/tests/meal.test.ts @@ -1,23 +1,23 @@ import { logger, neis } from '.' -import type { MealInfo } from '../src/types' +import type { Meal } from '../src' describe('Meal', () => { - it('should return MealInfo[]', async () => { + it('should return Meal[]', async () => { const schools = await neis.getSchool({ ATPT_OFCDC_SC_CODE: 'B10', }) - const data: MealInfo[] = [] + const data: Meal[] = [] for await (const school of schools) { data.push(await school.getMealOne({ MLSV_YMD: '20230331' })) } logger.info(data) - expect(data).toMatchObject(data) + expect(data).toMatchObject(data) }) - it('should return MealInfo', async () => { + it('should return Meal', async () => { const school = await neis.getSchoolOne({ ATPT_OFCDC_SC_CODE: 'B10', SD_SCHUL_CODE: '7091455', @@ -27,6 +27,6 @@ describe('Meal', () => { logger.info(data) - expect(data).toMatchObject(data) + expect(data).toMatchObject(data) }) })