多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
729 B

export class AliasMapper {
private aliasMap = new Map<string, unknown>()
generateAlias(key: unknown): string {
const alias = Math.random().toString(36).substring(2, 15)
this.aliasMap.set(alias, key)
return alias
}
getAlias(alias: string): unknown {
return this.aliasMap.get(alias)
}
clear() {
this.aliasMap.clear()
}
async process<T extends Record<string, unknown>>(
data: Record<string, T>,
processor: (key: unknown, value: T) => void | Promise<void>,
): Promise<void> {
for (const [alias, value] of Object.entries(data)) {
const originalKey = this.getAlias(alias)
if (originalKey !== undefined) {
await processor(originalKey, value)
}
}
}
}