export class StringUtil {
/**
* linefeed >
*/
public static linefeedtohtml(str: string): string {
return str
.replace(/\r\n/g, '
')
.replace(/\r/g, '
')
.replace(/\n/g, '
');
}
/**
*
> linefeed
*/
public static htmltolinefeed(str: string): string {
return str.replace(/<(\/br)([^>]*)/gi, '\n');
}
/**
* prefix zero fill
* @param str target string
* @param len fill in length
*/
public static zeroFill(str: any, len: number): string {
if (typeof str === 'string') {
let fillin = '';
for (let i = 0; i < len - str.length; i++) {
fillin += '0';
}
return fillin + str;
} else if (typeof str === 'number') {
return StringUtil.zeroFill(str.toString(), len);
}
}
/**
* Json String Analization.
* @description Editing with json.util.ts
*/
public static receiveAnalization(jsonStr: string): any {
const startJson = jsonStr.indexOf('{');
const endJson = jsonStr.lastIndexOf('}');
if (startJson >= 0 && startJson < endJson) {
jsonStr = jsonStr
.substring(startJson, endJson + 1)
.replace(/\n"/gi, '"')
.replace(/\n}/gi, '}')
.replace(/\n/gi, '\\n');
return JSON.parse(jsonStr);
} else {
throw new Error('Invalid Json String');
}
}
/**
* date formater
*/
public static dateFormat(date: Date, f: string): string {
if (!date) {
return '';
} else if (!f) {
return date.toDateString();
}
const weekKorName = [
'일요일',
'월요일',
'화요일',
'수요일',
'목요일',
'금요일',
'토요일'
];
const weekKorShortName = ['일', '월', '화', '수', '목', '금', '토'];
const weekEngName = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
];
const weekEngShortName = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
return f.replace(/(yyyy|yy|MM|dd|KS|KL|ES|EL|HH|hh|mm|ss|a\/p)/gi, $1 => {
switch ($1) {
case 'yyyy':
return date.getFullYear().toString(); // 년 (4자리)
case 'yy':
return StringUtil.zeroFill(date.getFullYear() % 1000, 2); // 년 (2자리)
case 'MM':
return StringUtil.zeroFill(date.getMonth() + 1, 2); // 월 (2자리)
case 'dd':
return StringUtil.zeroFill(date.getDate(), 2); // 일 (2자리)
case 'KS':
return weekKorShortName[date.getDay()]; // 요일 (짧은 한글)
case 'KL':
return weekKorName[date.getDay()]; // 요일 (긴 한글)
case 'ES':
return weekEngShortName[date.getDay()]; // 요일 (짧은 영어)
case 'EL':
return weekEngName[date.getDay()]; // 요일 (긴 영어)
case 'HH':
return StringUtil.zeroFill(date.getHours(), 2); // 시간 (24시간 기준, 2자리)
case 'hh':
return StringUtil.zeroFill(date.getHours() % 12, 2); // 시간 (12시간 기준, 2자리)
case 'mm':
return StringUtil.zeroFill(date.getMinutes(), 2); // 분 (2자리)
case 'ss':
return StringUtil.zeroFill(date.getSeconds(), 2); // 초 (2자리)
case 'a/p':
return date.getHours() < 12 ? '오전' : '오후'; // 오전/오후 구분
default:
return $1;
}
});
}
}