- 在多个工具类中添加私有构造函数,防止实例化 -这种设计模式确保了工具类的静态方法和属性的正确使用 - 受影响的主要类包括: - AlgorithmHelper - AppStorageHelper - LogHelper - PreferencesHelper - SZYXLocalStorageHelper - SZYXLocalStorageKeys - TimeHelper - ToolsHelper - ToolsHelperForTS - ValidatorHelper - WindowHelper - XWebHelper
40 行
1.1 KiB
TypeScript
40 行
1.1 KiB
TypeScript
import { HashMap } from '@kit.ArkTS';
|
|
|
|
/**
|
|
* 常用方法,部分方法,在ets里面不能用
|
|
*/
|
|
export class ToolsHelperForTS {
|
|
private constructor() {
|
|
}
|
|
// Map转为Record
|
|
static mapToRecord(myMap: HashMap<string, string>): Record<string, string> {
|
|
return Object.fromEntries(myMap.entries()) as Record<string, string>;
|
|
}
|
|
|
|
// Record转为Map
|
|
static recordToMap(myRecord: Record<string, string>): HashMap<string, string> {
|
|
let myMap: HashMap<string, string> = new HashMap();
|
|
for (const key in myRecord) {
|
|
myMap.set(key, myRecord[key]);
|
|
}
|
|
return myMap;
|
|
}
|
|
|
|
public static classToRecord(obj: Object): Record<string, string> {
|
|
const record: Record<string, string> = {} as Record<string, string>;
|
|
for (const key in obj) {
|
|
if (obj.hasOwnProperty(key)) {
|
|
record[key] = obj[key];
|
|
}
|
|
}
|
|
return record;
|
|
}
|
|
|
|
static gets(headers?: Record<string, string>) {
|
|
return {
|
|
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
|
// "Accept": "application/json",
|
|
...headers
|
|
}
|
|
}
|
|
} |