forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
41 lines (34 loc) · 1.1 KB
/
common.ts
File metadata and controls
41 lines (34 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import changeCase from 'change-case';
import short from 'short-uuid';
// Doc: https://www.npmjs.com/package/change-case
export function camel2dash(input: string): string {
return changeCase.paramCase(input);
}
/**
* 转为驼峰
*/
export function camelize(str: string): string {
return changeCase.camelCase(str);
}
export function generateID(): string {
return short.generate();
}
export function upperCaseFirst(inputValue: string): string {
return changeCase.upperCaseFirst(inputValue);
}
export function uniqueArray<T>(arr: T[], by: (i: T) => string) {
const map: Record<string, T> = {};
arr.forEach((item) => {
map[by(item)] = item;
});
// FIXME: Babel 编译存在问题,暂时替换实现
// const uniqueKeys = [...new Set<string>(Object.keys(map))];
const uniqueKeys = Array.from(new Set<string>(Object.keys(map)));
const uniqueItems = uniqueKeys.map((key) => map[key]);
return uniqueItems;
}
export function getStaticExprValue<T>(expr: string): T {
// TODO: 需要安全性检查
// eslint-disable-next-line no-new-func
return Function(`"use strict";return (${expr})`)();
}