js2ts: entrance

- j_server_get_status 반환형 수정
- getDateTimeNow 추가
- common에서 TemplateEngine 코드 버그 수정
This commit is contained in:
2021-08-17 01:05:41 +09:00
parent 28d71b8ce2
commit 243473a97d
5 changed files with 329 additions and 7 deletions
+6 -5
View File
@@ -206,10 +206,11 @@ export function linkifyStrWithOpt(text: string): string {
export function TemplateEngine(html: string, options: Record<string | number, unknown> = {}): string {
const re = /<%(.+?)%>/g;
const reExp = /(^( )?(var|if|for|else|switch|case|break|{|}|;))(.*)?/g;
const code = ['with(obj) { var r=[];\n'];
let cursor = 0;
const add = function (line: string, js?: boolean) {
js ? (code += line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
(code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
js ? code.push(line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
code.push(line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
return add;
}
options.e = escapeHtml;
@@ -224,10 +225,10 @@ export function TemplateEngine(html: string, options: Record<string | number, un
}
add(html.substr(cursor, html.length - cursor));
let code = 'with(obj) { var r=[];\n';
code = (code + 'return r.join(""); }').replace(/[\r\t\n]/g, ' ');
code.push('return r.join(""); }');
const compiledCode = code.join('').replace(/[\r\t\n]/g, ' ');
try {
return new Function('obj', code).apply(options, [options]);
return new Function('obj', compiledCode).apply(options, [options]);
} catch (err) {
console.error("'" + err.message + "'", " in \n\nCode:\n", code, "\n");
throw err;
+14
View File
@@ -0,0 +1,14 @@
import {DateTime} from 'luxon';
export const DATE_TIME_FORMAT = 'yyyy-MM-dd HH:mm:ss';
export const DATE_TIME_FORMAT_WITH_FRACTION = 'yyyy-MM-dd HH:mm:ss.SSS';
export function getDateTimeNow(withFraction = false): string{
if(withFraction){
return DateTime.now().toFormat(DATE_TIME_FORMAT_WITH_FRACTION);
}
else{
return DateTime.now().toFormat(DATE_TIME_FORMAT);
}
}