82 lines
2.9 KiB
TypeScript
82 lines
2.9 KiB
TypeScript
/**
|
|
* 위치코드/위치명 패턴 변환 유틸리티
|
|
*
|
|
* 사용 가능한 변수:
|
|
* {warehouse} - 창고 코드 (예: WH002)
|
|
* {warehouseName} - 창고명 (예: 2창고)
|
|
* {floor} - 층 (예: 2층)
|
|
* {zone} - 구역 (예: A구역)
|
|
* {row} - 열 번호 (예: 1)
|
|
* {row:02} - 열 번호 2자리 (예: 01)
|
|
* {row:03} - 열 번호 3자리 (예: 001)
|
|
* {level} - 단 번호 (예: 1)
|
|
* {level:02} - 단 번호 2자리 (예: 01)
|
|
* {level:03} - 단 번호 3자리 (예: 001)
|
|
*/
|
|
|
|
interface PatternVariables {
|
|
warehouse?: string;
|
|
warehouseName?: string;
|
|
floor?: string;
|
|
zone?: string;
|
|
row: number;
|
|
level: number;
|
|
}
|
|
|
|
// 기본 패턴 (하드코딩 대체)
|
|
export const DEFAULT_CODE_PATTERN = "{warehouse}-{floor}{zone}-{row:02}-{level}";
|
|
export const DEFAULT_NAME_PATTERN = "{zone}-{row:02}열-{level}단";
|
|
|
|
/**
|
|
* 패턴 문자열에서 변수를 치환하여 결과 문자열 반환
|
|
*/
|
|
export function applyLocationPattern(pattern: string, vars: PatternVariables): string {
|
|
let result = pattern;
|
|
|
|
// zone에 "구역" 포함 여부에 따른 처리 없이 있는 그대로 치환
|
|
const simpleVars: Record<string, string | undefined> = {
|
|
warehouse: vars.warehouse,
|
|
warehouseName: vars.warehouseName,
|
|
floor: vars.floor,
|
|
zone: vars.zone,
|
|
};
|
|
|
|
// 단순 문자열 변수 치환
|
|
for (const [key, value] of Object.entries(simpleVars)) {
|
|
result = result.replace(new RegExp(`\\{${key}\\}`, "g"), value || "");
|
|
}
|
|
|
|
// 숫자 변수 (row, level) - zero-pad 지원
|
|
const numericVars: Record<string, number> = {
|
|
row: vars.row,
|
|
level: vars.level,
|
|
};
|
|
|
|
for (const [key, value] of Object.entries(numericVars)) {
|
|
// {row:02}, {level:03} 같은 zero-pad 패턴
|
|
const padRegex = new RegExp(`\\{${key}:(\\d+)\\}`, "g");
|
|
result = result.replace(padRegex, (_, padWidth) => {
|
|
return value.toString().padStart(parseInt(padWidth), "0");
|
|
});
|
|
|
|
// {row}, {level} 같은 단순 패턴
|
|
result = result.replace(new RegExp(`\\{${key}\\}`, "g"), value.toString());
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// 패턴에서 사용 가능한 변수 목록
|
|
export const PATTERN_VARIABLES = [
|
|
{ token: "{warehouse}", description: "창고 코드", example: "WH002" },
|
|
{ token: "{warehouseName}", description: "창고명", example: "2창고" },
|
|
{ token: "{floor}", description: "층", example: "2층" },
|
|
{ token: "{zone}", description: "구역", example: "A구역" },
|
|
{ token: "{row}", description: "열 번호", example: "1" },
|
|
{ token: "{row:02}", description: "열 번호 (2자리)", example: "01" },
|
|
{ token: "{row:03}", description: "열 번호 (3자리)", example: "001" },
|
|
{ token: "{level}", description: "단 번호", example: "1" },
|
|
{ token: "{level:02}", description: "단 번호 (2자리)", example: "01" },
|
|
{ token: "{level:03}", description: "단 번호 (3자리)", example: "001" },
|
|
];
|