Skip to content

Commit

Permalink
[style/utils][taier-ui]remove unused functions (#949)
Browse files Browse the repository at this point in the history
### 简介
- 移除没有用到的工具类函数



### Related Issues

Closed #947
  • Loading branch information
mortalYoung committed Dec 22, 2022
1 parent c0afa0a commit e933297
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 198 deletions.
14 changes: 0 additions & 14 deletions taier-ui/src/utils/__tests__/enums.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,9 @@ import {
TaskStatus,
taskStatusText,
TaskTimeType,
taskTypeText,
} from '../enums';

describe('enums', () => {
it('Render Task Type Text', () => {
expect(taskTypeText(TASK_TYPE_ENUM.DATA_ACQUISITION)).toBe('实时采集');
expect(taskTypeText(TASK_TYPE_ENUM.HIVE_SQL)).toBe('HiveSQL');
expect(taskTypeText(TASK_TYPE_ENUM.SHELL)).toBe('Shell');
expect(taskTypeText(TASK_TYPE_ENUM.SPARK)).toBe('未知');
expect(taskTypeText(TASK_TYPE_ENUM.SPARK_SQL)).toBe('SparkSQL');
expect(taskTypeText(TASK_TYPE_ENUM.SQL)).toBe('FlinkSQL');
expect(taskTypeText(TASK_TYPE_ENUM.FLINK)).toBe('Flink');
expect(taskTypeText(TASK_TYPE_ENUM.SYNC)).toBe('数据同步');
expect(taskTypeText(TASK_TYPE_ENUM.VIRTUAL)).toBe('虚节点');
expect(taskTypeText(TASK_TYPE_ENUM.WORK_FLOW)).toBe('工作流');
});

it('Render Task Status Text', () => {
expect(taskStatusText(TASK_STATUS.AUTO_CANCEL)).toBe('自动取消');
expect(taskStatusText(TASK_STATUS.COMPUTING)).toBe('计算中');
Expand Down
59 changes: 0 additions & 59 deletions taier-ui/src/utils/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
checkExist,
convertObjToNamePath,
convertParams,
convertToObj,
convertToStr,
copyText,
createSeries,
createSQLProposals,
Expand All @@ -16,7 +14,6 @@ import {
filterComments,
filterSql,
formatDateTime,
formJsonValidator,
getColumnsByColumnsText,
getCookie,
getPlus,
Expand All @@ -28,10 +25,8 @@ import {
isValidFormatType,
pickByTruly,
prettierJSONstring,
queryParse,
randomId,
removePopUpMenu,
removeToolTips,
renderCharacterByCode,
replaceStrFormIndexArr,
splitByKey,
Expand Down Expand Up @@ -101,14 +96,6 @@ describe('utils/index', () => {
expect(checkExist({})).toBeTruthy();
});

it('Should Validate the JSON', async () => {
await expect(formJsonValidator(null, JSON.stringify({}))).resolves.not.toThrowError();
await expect(formJsonValidator(null, 'test')).rejects.toThrow(
'请检查JSON格式,确认无中英文符号混用!',
);
await expect(formJsonValidator(null, 'true')).rejects.toThrow('请填写正确的JSON');
});

it('Should Filter Comments In SQL', () => {
expect(filterComments('show tables;-- name test')).toBe('show tables; ');
expect(filterComments('-- select test from A where id = "taier";')).toBe(' ');
Expand Down Expand Up @@ -144,13 +131,6 @@ describe('utils/index', () => {
expect(filterSql(sql)).toEqual(['show tables']);
});

it('Should Query Hash in URL', () => {
const url = 'http://test.com/?test=task&test2=abc';
expect(queryParse(url)).toEqual({ test: 'task', test2: 'abc' });

expect(queryParse('')).toEqual({});
});

it('Should Get Tenant Id From Cookie', () => {
document.cookie = 'tenantId=1;';
expect(getTenantId()).toBe('1');
Expand All @@ -163,45 +143,6 @@ describe('utils/index', () => {
deleteCookie('userId');
});

it('Should Convert to Object', () => {
const rawObj = { 'a.b.c': 1, 'a.b.a': 2 };
expect(convertToObj(rawObj)).toEqual({
a: {
b: {
c: 1,
a: 2,
},
},
});
});

it('Should Inverse to Object', () => {
const rawObj = {
a: {
b: {
c: 1,
a: 2,
},
},
};
expect(convertToStr(rawObj)).toEqual({ 'a.b.c': 1, 'a.b.a': 2 });
});

it('Should Remove Tooltips from Document', () => {
jest.useFakeTimers();
const dom = document.createElement('div');
dom.classList.add('mxTooltip');
dom.style.visibility = 'visibility';
document.body.appendChild(dom);
expect(dom.style.visibility).toBe('visibility');

removeToolTips();

jest.advanceTimersByTime(500);

expect(dom.style.visibility).toBe('hidden');
});

it('Should Remove Popup Menu from Document', () => {
jest.useFakeTimers();
const dom = document.createElement('div');
Expand Down
27 changes: 0 additions & 27 deletions taier-ui/src/utils/enums.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,6 @@ import {
TASK_TYPE_ENUM,
} from '@/constant';

export function taskTypeText(type?: TASK_TYPE_ENUM) {
switch (type) {
case TASK_TYPE_ENUM.SYNC:
return '数据同步';
case TASK_TYPE_ENUM.VIRTUAL:
return '虚节点';
case TASK_TYPE_ENUM.SQL:
return 'FlinkSQL';
case TASK_TYPE_ENUM.SHELL:
return 'Shell';
case TASK_TYPE_ENUM.HIVE_SQL:
return 'HiveSQL';
case TASK_TYPE_ENUM.SPARK_SQL:
return 'SparkSQL';
case TASK_TYPE_ENUM.DATA_ACQUISITION:
return '实时采集';
case TASK_TYPE_ENUM.FLINK:
return 'Flink';
case TASK_TYPE_ENUM.WORK_FLOW:
return '工作流';
case TASK_TYPE_ENUM.MYSQL:
return 'MySQL';
default:
return '未知';
}
}

export function taskStatusText(type?: TASK_STATUS) {
switch (type) {
case TASK_STATUS.WAIT_SUBMIT:
Expand Down
99 changes: 1 addition & 98 deletions taier-ui/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,6 @@ export function checkExist(prop: any) {
return prop !== undefined && prop !== null && prop !== '';
}

/**
* JSON 格式校验
*/
export function formJsonValidator(_: any, value: string) {
let msg = '';
try {
if (value) {
const t = JSON.parse(value);
if (typeof t !== 'object') {
msg = '请填写正确的JSON';
}
}
} catch (e) {
msg = '请检查JSON格式,确认无中英文符号混用!';
}

if (msg) {
return Promise.reject(new Error(msg));
}
return Promise.resolve();
}

interface FilterParser {
index: number;
queue: string;
Expand Down Expand Up @@ -304,17 +282,6 @@ export function filterSql(sql: string) {
return arr;
}

export const queryParse = (url: string) => {
const search = url.split('?')[1];
if (!search) return {};
return search.split('&').reduce((temp, current) => {
const next = temp;
const [key, value] = current.split('=');
next[key] = value;
return next;
}, {} as Record<string, string>);
};

export const getTenantId = () => {
return getCookie(`tenantId`);
};
Expand All @@ -323,54 +290,6 @@ export const getUserId = () => {
return getCookie('userId');
};

/**
* 由于 antd@3 的嵌套表单 name 可以通过 a.b 实现,但是 antd@4 需要通过 [a,b] 所以需要将最终得到的结果做转化
* @examples
* ```js
* // rawValues = { a.b.c: 1, a.b.a: 2};
* const values = convertToObj(rawValues);
* // values = { a: { b: {c: 1, a: 2}}};
* ```
*/
export const convertToObj = (values: Record<string, any>) => {
const res: Record<string, any> = {};
Object.keys(values).forEach((keyString) => {
const keys = keyString.split('.');
keys.forEach(
function (this: { res: Record<string, any> }, key, index, thisArr) {
if (index === thisArr.length - 1) {
this.res[key] = values[keyString];
} else if (this.res.hasOwnProperty(key)) {
this.res = this.res[key];
} else {
this.res[key] = {};
this.res = this.res[key];
}
},
{ res },
);
});
return res;
};

/**
* 上述方法的逆运算
*/
export const convertToStr = (values: Record<string, any>, prefix = '') => {
let res: Record<string, any> = {};

Object.keys(values).forEach((key) => {
if (typeof values[key] === 'object' && !Array.isArray(values[key])) {
const obj = convertToStr(values[key], `${prefix ? `${prefix}.` : ''}${key}`);
res = { ...res, ...obj };
} else {
res[`${prefix ? `${prefix}.` : ''}${key}`] = values[key];
}
});

return res;
};

function isUtf8(s: string) {
const lastnames = new Array('ä', 'å', 'æ', 'ç', 'è', 'é');
for (let i = 0; i < lastnames.length; i += 1) {
Expand Down Expand Up @@ -461,22 +380,6 @@ export function goToTaskDev(record: { id: string | number; [key: string]: any })
removePopUpMenu();
}

/**
* 从 document.body 隐藏 mxGraph 所产生的 tooltip
*/
export const removeToolTips = () => {
const remove = () => {
const tips = document.querySelectorAll<HTMLDivElement>('.mxTooltip');
if (tips) {
tips.forEach((o) => {
// eslint-disable-next-line no-param-reassign
o.style.visibility = 'hidden';
});
}
};
setTimeout(remove, 500);
};

/**
* 从 document.body 隐藏 mxGraph 所产生的 popup
*/
Expand Down Expand Up @@ -733,7 +636,7 @@ export const convertParams = (params: Record<string, any>, form: Record<string,

if (utils) {
const utilCollection: Record<string, (value: any) => any> = {
toArray: toArray,
toArray,
};
value = utilCollection[utils](value);
}
Expand Down

0 comments on commit e933297

Please sign in to comment.