sn: util.ts 추가 정리

This commit is contained in:
2026-09-02 18:10:50 +09:00
parent 80d93c1631
commit 4cde9b67c1
5 changed files with 20 additions and 16 deletions
+29
View File
@@ -0,0 +1,29 @@
import moment from "moment";
/** 빈 값과 화면의 전체 선택값 `0`을 제외하고 API 검색 파라미터를 추가한다. */
export const appendApiSearchParams = (
params: URLSearchParams,
search: Readonly<Record<string, string>>,
) => {
Object.entries(search).forEach(([key, value]) => {
const normalized = value.trim();
if (!normalized || normalized === "0") return;
params.set(key, normalized);
});
};
/** 8자리 또는 구분자가 섞인 날짜 문자열을 YYYY-MM-DD 형식으로 정규화한다. */
export const formatDate = (value?: string | null) => {
if (!value) return "";
const normalized = String(value).trim();
if (!normalized) return "";
const digits = normalized.replace(/\D/g, "");
if (digits.length === 8) {
const parsedDate = moment(digits, "YYYYMMDD", true);
return parsedDate.isValid() ? parsedDate.format("YYYY-MM-DD") : normalized;
}
const parsedDate = moment(normalized);
return parsedDate.isValid() ? parsedDate.format("YYYY-MM-DD") : normalized;
};