js2ts: util.ts 분리

This commit is contained in:
2021-08-21 20:45:10 +09:00
parent 1496fbe0b7
commit 6cc37dedd2
16 changed files with 56 additions and 86 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ import $ from 'jquery';
import 'bootstrap';
import { DateTime } from 'luxon';
import download from 'downloadjs';
import { unwrap } from './util';
import { unwrap } from "./util/unwrap";
import { isInteger } from 'lodash';
import { combineArray, errUnknown, getNpcColor, isBrightColor, numberWithCommas } from './common_legacy';
import { unwrap_any } from './util/unwrap_any';
+1 -1
View File
@@ -4,7 +4,7 @@ import { range } from 'lodash';
import { DateTime } from 'luxon';
import { errUnknown, getNpcColor } from './common_legacy';
import { InvalidResponse } from './defs';
import { unwrap } from './util';
import { unwrap } from "./util/unwrap";
import { convertFormData } from './util/convertFormData';
import { unwrap_any } from "./util/unwrap_any";
+6 -6
View File
@@ -1,4 +1,4 @@
import { unwrap } from "./util";
import { unwrap } from "./util/unwrap";
import jQuery from "jquery";
import 'bootstrap';
@@ -8,7 +8,7 @@ import axios from "axios";
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
//TODO: X-Requested-With 믿지 말자.
/**
/**
* <>& 등을 html에서도 그대로 보이도록 escape주는 함수
* @see https://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery
*/
@@ -121,7 +121,7 @@ export function convertSet<K extends string | number>(arr: ArrayLike<K>): Record
}
/**
/**
* {0}, {1}, {2}형태로 포맷해주는 함수
*/
@@ -199,8 +199,8 @@ export function linkifyStrWithOpt(text: string): string {
/**
* 단순한 Template 함수. <%변수명%>으로 template 가능
* @see https://github.com/krasimir/absurd/blob/master/lib/processors/html/helpers/TemplateEngine.js
* @param {string} html
* @param {object} options
* @param {string} html
* @param {object} options
* @returns {string}
*/
export function TemplateEngine(html: string, options: Record<string | number, unknown> = {}): string {
@@ -332,7 +332,7 @@ export function nl2br(text: string): string {
return text.replace(/\n/g, "<br>");
}
/*
function br2nl (text) {
function br2nl (text) {
return text.replace(/<\s*\/?br\s*[\/]?>/gi, '\n');
}
*/
+3 -2
View File
@@ -1,5 +1,6 @@
import axios from "axios";
import { RuntimeError, unwrap } from "./util";
import { unwrap } from "./util/unwrap";
import { RuntimeError } from "./util/RuntimeError";
declare global {
interface Window {
@@ -46,7 +47,7 @@ export function launchTroopPlugin($: JQueryStatic): void {
const rawData = response.data;
try {
const $html = $(rawData);
const $html = $(rawData);
const tmpUsers: JQuery<HTMLElement> = (() => {
let tmpUsers = undefined;
+2 -2
View File
@@ -1,9 +1,9 @@
import "../scss/inheritPoint.scss";
import { sum } from "lodash";
import { unwrap } from "./util";
import { unwrap } from "./util/unwrap";
declare global {
interface Window {
interface Window {
formStart: ()=>void;
items: {[name: string]:number};
helpText: {[name: string]:string};
+1 -1
View File
@@ -3,7 +3,7 @@ import $ from 'jquery';
import { extend, isNumber } from 'lodash';
import { convColorValue, convertDictById, convertSet } from './common_legacy';
import { InvalidResponse } from './defs';
import { unwrap } from './util';
import { unwrap } from "./util/unwrap";
import { convertFormData } from './util/convertFormData';
declare const serverNick: string;
+1 -1
View File
@@ -7,7 +7,7 @@ import { convertFormData } from './util/convertFormData';
import { InvalidResponse } from './defs';
import { unwrap_any } from './util/unwrap_any';
import { DataFormat, IdTextPair, OptionData } from 'select2';
import { unwrap } from './util';
import { unwrap } from "./util/unwrap";
declare const isChiefTurn: boolean;
declare global {
-35
View File
@@ -1,35 +0,0 @@
type ErrType<T> = { new(msg?: string): T }
export type Nullable<T> = T | null | undefined
export class RuntimeError extends Error {
public name = 'RuntimeError';
constructor(public message: string = '') {
super(message);
}
toString(): string {
if (this.message) {
return this.name + ': ' + this.message;
}
else {
return this.name;
}
}
}
export class NotNullExpected extends RuntimeError {
public name = 'NotNullExpected';
}
export function unwrap<T>(result: Nullable<T>): T {
if (result === null || result === undefined) {
throw new NotNullExpected();
}
return result;
}
export function unwrap_err<T, ErrT extends Error>(result: Nullable<T>, errType: ErrType<ErrT>, errMsg?: string): T {
if (result === null || result === undefined) {
throw new errType(errMsg);
}
return result;
}
+4
View File
@@ -0,0 +1,4 @@
export class NotNullExpected extends TypeError {
public name = 'NotNullExpected';
}
+1
View File
@@ -0,0 +1 @@
export type Nullable<T> = T | null | undefined;
+14
View File
@@ -0,0 +1,14 @@
export class RuntimeError extends Error {
public name = 'RuntimeError';
constructor(public message: string = '') {
super(message);
}
toString(): string {
if (this.message) {
return this.name + ': ' + this.message;
}
else {
return this.name;
}
}
}
+9
View File
@@ -0,0 +1,9 @@
import { Nullable } from "./Nullable";
import { NotNullExpected } from "./NotNullExpected";
export function unwrap<T>(result: Nullable<T>): T {
if (result === null || result === undefined) {
throw new NotNullExpected();
}
return result;
}
+2 -1
View File
@@ -1,4 +1,5 @@
import { Nullable, NotNullExpected } from "../util";
import { Nullable } from "./Nullable";
import { NotNullExpected } from "./NotNullExpected";
export function unwrap_any<T>(result: Nullable<unknown>): T {
+10
View File
@@ -0,0 +1,10 @@
import { Nullable } from "./Nullable";
type ErrType<T> = { new(msg?: string): T }
export function unwrap_err<T, ErrT extends Error>(result: Nullable<T>, errType: ErrType<ErrT>, errMsg?: string): T {
if (result === null || result === undefined) {
throw new errType(errMsg);
}
return result;
}
+1 -1
View File
@@ -7,7 +7,7 @@ import { DATE_TIME_FORMAT, getDateTimeNow } from "../hwe/ts/util/getDateTimeNow"
import { sha512 } from "js-sha512";
import { convertFormData } from "../hwe/ts/util/convertFormData";
import { InvalidResponse } from "../hwe/ts/defs";
import { unwrap } from "./util";
import { unwrap } from "../hwe/ts/util/unwrap";
type ResultUserInfo = {
result: true,
-35
View File
@@ -1,35 +0,0 @@
type ErrType<T> = { new(msg?: string): T }
type Nullable<T> = T | null | undefined
export class RuntimeError extends Error {
public name = 'RuntimeError';
constructor(public message: string = '') {
super(message);
}
toString(): string {
if (this.message) {
return this.name + ': ' + this.message;
}
else {
return this.name;
}
}
}
export class NotNullExpected extends RuntimeError {
public name = 'NotNullExpected';
}
export function unwrap<T>(result: Nullable<T>): T {
if (result === null || result === undefined) {
throw new NotNullExpected();
}
return result;
}
export function unwrap_err<T, ErrT extends Error>(result: Nullable<T>, errType: ErrType<ErrT>, errMsg?: string): T {
if (result === null || result === undefined) {
throw new errType(errMsg);
}
return result;
}