forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.ts
More file actions
135 lines (117 loc) · 3.68 KB
/
misc.ts
File metadata and controls
135 lines (117 loc) · 3.68 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { isI18NObject } from './is-object';
import { get } from 'lodash';
import { IPublicEnumTransformStage, IPublicModelComponentMeta } from '@alilc/lowcode-types';
import { Logger } from './logger';
const logger = new Logger({ level: 'warn', bizName: 'utils' });
interface Variable {
type: 'variable';
variable: string;
value: any;
}
export function isVariable(obj: any): obj is Variable {
if (!obj || typeof obj !== 'object') {
return false;
}
return obj.type === 'variable';
}
export function isUseI18NSetter(prototype: any, propName: string) {
const configure = prototype?.options?.configure;
if (Array.isArray(configure)) {
return configure.some(c => {
return c.name === propName && c?.setter?.type?.displayName === 'I18nSetter';
});
}
return false;
}
export function convertToI18NObject(v: string | any, locale: string = 'zh-CN') {
if (isI18NObject(v)) return v;
return { type: 'i18n', use: locale, [locale]: v };
}
export function isString(v: any): v is string {
return typeof v === 'string';
}
function _innerWaitForThing(obj: any, path: string): Promise<any> {
const timeGap = 200;
return new Promise((resolve, reject) => {
setTimeout(() => {
const thing = get(obj, path);
if (thing) {
return resolve(thing);
}
reject();
}, timeGap);
}).catch(() => {
return _innerWaitForThing(obj, path);
});
}
export function waitForThing(obj: any, path: string): Promise<any> {
const thing = get(obj, path);
if (thing) {
return Promise.resolve(thing);
}
return _innerWaitForThing(obj, path);
}
export function arrShallowEquals(arr1: any[], arr2: any[]): boolean {
if (!Array.isArray(arr1) || !Array.isArray(arr2)) return false;
if (arr1.length !== arr2.length) return false;
return arr1.every(item => arr2.includes(item));
}
/**
* 判断当前 meta 是否从 vc prototype 转换而来
* @param meta
*/
export function isFromVC(meta: IPublicModelComponentMeta) {
return !!meta?.getMetadata().configure?.advanced;
}
export function executePendingFn(fn: () => void, timeout: number = 2000) {
return setTimeout(fn, timeout);
}
const stageList = [
'render',
'serilize',
'save',
'clone',
'init',
'upgrade',
];
/**
* 兼容原来的数字版本的枚举对象
* @param stage
* @returns
*/
export function compatStage(stage: IPublicEnumTransformStage | number): IPublicEnumTransformStage {
if (typeof stage === 'number') {
console.warn('stage 直接指定为数字的使用方式已经过时,将在下一版本移除,请直接使用 IPublicEnumTransformStage.Render|Serilize|Save|Clone|Init|Upgrade');
return stageList[stage - 1] as IPublicEnumTransformStage;
}
return stage as IPublicEnumTransformStage;
}
export function invariant(check: any, message: string, thing?: any) {
if (!check) {
throw new Error(`Invariant failed: ${ message }${thing ? ` in '${thing}'` : ''}`);
}
}
export function deprecate(fail: any, message: string, alterative?: string) {
if (fail) {
logger.warn(`Deprecation: ${message}` + (alterative ? `, use ${alterative} instead.` : ''));
}
}
export function isRegExp(obj: any): obj is RegExp {
if (!obj || typeof obj !== 'object') {
return false;
}
return 'test' in obj && 'exec' in obj && 'compile' in obj;
}
/**
* The prop supportVariable SHOULD take precedence over default global supportVariable.
* @param propSupportVariable prop supportVariable
* @param globalSupportVariable global supportVariable
* @returns
*/
export function shouldUseVariableSetter(
propSupportVariable: boolean | undefined,
globalSupportVariable: boolean,
) {
if (propSupportVariable === false) return false;
return propSupportVariable || globalSupportVariable;
}