Files
frontend/apps/wr/app/store/common/code/normalizeSelectData.ts
T

29 lines
742 B
TypeScript

type SelectDataInput = {
text?: string | null;
val?: string | number | null;
};
export type NormalizedSelectData = {
text: string;
val: string;
};
/** 선택 목록을 값 기준으로 중복 제거하고 프런트 표준 전체 항목을 한 번만 추가한다. */
export const normalizeSelectData = (
items: readonly SelectDataInput[],
): NormalizedSelectData[] => {
const uniqueItems = new Map<string, NormalizedSelectData>();
items.forEach((item) => {
const val = String(item.val ?? "").trim();
if (!val || val === "0" || uniqueItems.has(val)) return;
uniqueItems.set(val, {
text: String(item.text ?? ""),
val,
});
});
return [{ text: "전체", val: "0" }, ...uniqueItems.values()];
};