260701_03_ENH: date, object, url util 추가

This commit is contained in:
2026-07-01 09:36:56 +09:00
parent f57f42baff
commit 510b4a9e68
8 changed files with 164 additions and 21 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@kri/common",
"version": "0.1.1",
"version": "0.2.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@kri/common",
"version": "0.1.1",
"version": "0.2.0",
"license": "UNLICENSED",
"devDependencies": {
"typescript": "^6.0.3"

View File

@@ -1,6 +1,6 @@
{
"name": "@kri/common",
"version": "0.1.1",
"version": "0.2.0",
"description": "KRI 프런트엔드 공통 유틸리티",
"type": "module",
"main": "./dist/index.js",

62
src/date.ts Normal file
View File

@@ -0,0 +1,62 @@
export type DateTimeFormatOption =
| "date"
| "dateHour"
| "dateHourMin"
| "dateTime";
const KST_OFFSET_MS = 9 * 60 * 60 * 1000;
function pad2(value: number): string {
return String(value).padStart(2, "0");
}
/**
* 날짜 값을 한국 표준시(KST) 문자열로 변환한다.
*/
export function formatKst(
input: string | number | Date | null | undefined,
option: DateTimeFormatOption = "dateTime",
): string {
if (
input === null
|| input === undefined
|| input === ""
) {
return "";
}
const date = input instanceof Date
? input
: new Date(input);
if (Number.isNaN(date.getTime())) {
return "";
}
const kstDate = new Date(
date.getTime() + KST_OFFSET_MS,
);
const year = kstDate.getUTCFullYear();
const month = pad2(kstDate.getUTCMonth() + 1);
const day = pad2(kstDate.getUTCDate());
const hour = pad2(kstDate.getUTCHours());
const minute = pad2(kstDate.getUTCMinutes());
const second = pad2(kstDate.getUTCSeconds());
const datePart = `${year}-${month}-${day}`;
switch (option) {
case "date":
return datePart;
case "dateHour":
return `${datePart} ${hour}`;
case "dateHourMin":
return `${datePart} ${hour}:${minute}`;
case "dateTime":
return `${datePart} ${hour}:${minute}:${second}`;
}
}

View File

@@ -1,19 +1,6 @@
/**
* 문자열이 null, undefined 또는 공백인지 확인한다.
*/
export function isBlank(
value: string | null | undefined,
): boolean {
return value === null
|| value === undefined
|| value.trim().length === 0;
}
export type * from "./types.js";
/**
* 문자열에 공백이 아닌 문자가 있는지 확인한다.
*/
export function isNotBlank(
value: string | null | undefined,
): boolean {
return !isBlank(value);
}
export * from "./object.js";
export * from "./url.js";
export * from "./date.js";
export * from "./string.js";

27
src/object.ts Normal file
View File

@@ -0,0 +1,27 @@
export function getKeyByValue<
const T extends Record<string, string>,
>(
map: T,
value: T[keyof T],
): keyof T | undefined {
const entry = Object.entries(map).find(
([, currentValue]) => currentValue === value,
);
return entry
? (entry[0] as keyof T)
: undefined;
}
export function getNumberKeyByValue(
map: Record<number, string>,
value: string,
): number | undefined {
const entry = Object.entries(map).find(
([, currentValue]) => currentValue === value,
);
return entry
? Number(entry[0])
: undefined;
}

19
src/string.ts Normal file
View File

@@ -0,0 +1,19 @@
/**
* 문자열이 null, undefined 또는 공백인지 확인한다.
*/
export function isBlank(
value: string | null | undefined,
): boolean {
return value === null
|| value === undefined
|| value.trim().length === 0;
}
/**
* 문자열에 공백이 아닌 문자가 있는지 확인한다.
*/
export function isNotBlank(
value: string | null | undefined,
): boolean {
return !isBlank(value);
}

9
src/types.ts Normal file
View File

@@ -0,0 +1,9 @@
export type ValueOf<T> = T[keyof T];
export type EnumLikeKey<
T extends Record<string, unknown>,
> = keyof T;
export type EnumLikeValue<
T extends Record<string, unknown>,
> = ValueOf<T>;

39
src/url.ts Normal file
View File

@@ -0,0 +1,39 @@
export type QueryParameterValue =
| string
| string[]
| undefined;
export type QueryParameters =
Record<string, QueryParameterValue>;
export function buildUrlWithParams(
targetPath: string,
parameters: QueryParameters,
extra?: Record<string, string | undefined>,
): string {
const searchParams = new URLSearchParams();
for (const [key, value] of Object.entries(parameters)) {
if (Array.isArray(value)) {
for (const item of value) {
searchParams.append(key, item);
}
} else if (value !== undefined) {
searchParams.set(key, value);
}
}
if (extra) {
for (const [key, value] of Object.entries(extra)) {
if (value !== undefined) {
searchParams.set(key, value);
}
}
}
const queryString = searchParams.toString();
return queryString
? `${targetPath}?${queryString}`
: targetPath;
}