2019-11-09 13:35:24 +09:00

200 lines
4.9 KiB
TypeScript

import {
EventType,
EventJson,
FileEventJson,
MassTextEventJson
} from '@ucap-webmessenger/protocol-event';
import { FileType } from '@ucap-webmessenger/protocol-file';
export class StringUtil {
/**
* linefeed > <br>
*/
public static linefeedtohtml(str: string): string {
return str
.replace(/\r\n/g, '<br />')
.replace(/\r/g, '<br />')
.replace(/\n/g, '<br />');
}
/**
* <br> > 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;
}
});
}
public static convertFinalEventMessage(
eventType: EventType,
finalEventMessage: EventJson
): string | null {
let eventMessage: string = null;
switch (eventType) {
case EventType.Join:
case EventType.Exit:
case EventType.RenameRoom:
case EventType.NotificationForTimerRoom:
case EventType.GuideForRoomTimerChanged:
{
/**
* 해당 타입은 메시지를 갱신하지 않는다.
* @description Edit with ui-chat > messages.component.ts
*/
}
break;
case EventType.Sticker:
eventMessage = '스티커';
break;
case EventType.File:
{
const m = finalEventMessage as FileEventJson;
if (FileType.Image === m.fileType) {
eventMessage = '이미지';
} else {
eventMessage = '첨부파일';
}
}
break;
case EventType.VideoConference:
eventMessage = '화상회의';
break;
case EventType.MassText:
{
const m = finalEventMessage as MassTextEventJson;
eventMessage = m.content;
}
break;
default:
{
const m = finalEventMessage as string;
eventMessage = m;
}
break;
}
return eventMessage;
}
}