Skip to content

Commit

Permalink
feat: Meal class
Browse files Browse the repository at this point in the history
  • Loading branch information
star0202 committed Apr 6, 2023
1 parent e8ebea3 commit 0765734
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/structures/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './school'
export * from './meal'
65 changes: 65 additions & 0 deletions src/structures/meal.ts
Original file line number Diff line number Diff line change
@@ -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('<br/>').map((dish) =>
dish.replace(/ {2,}(?!(\((\d{1,2}\.)+\)))/g, '')
)
}

getDishName(): string[] {
return this.DDISH_NM.replace(/ {2,}.*/g, '').split('<br/>')
}

getOrigin(): { name: string; origin: string }[] {
return this.ORPLC_INFO.split('<br/>').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('<br/>').map((nutrition) => {
const [name, amount] = nutrition.split(' : ')
return { name, amount: parseFloat(amount) }
})
}
}
26 changes: 17 additions & 9 deletions src/structures/school.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -67,16 +67,24 @@ export class School implements SchoolInfo {
this.#neis = neis
}

async getMeal(params: MealRequestParam): Promise<MealInfo[]> {
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<Meal[]> {
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<MealInfo> {
return await this.getMeal(params).then((data) => data[0])
async getMealOne(params: MealRequestParam): Promise<Meal> {
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<ScheduleInfo[]> {
Expand Down
12 changes: 6 additions & 6 deletions tests/meal.test.ts
Original file line number Diff line number Diff line change
@@ -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<MealInfo[]>(data)
expect(data).toMatchObject<Meal[]>(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',
Expand All @@ -27,6 +27,6 @@ describe('Meal', () => {

logger.info(data)

expect(data).toMatchObject<MealInfo>(data)
expect(data).toMatchObject<Meal>(data)
})
})

0 comments on commit 0765734

Please sign in to comment.