gtsoft-snail-job-admin/src/utils/common.ts

51 lines
1000 B
TypeScript
Raw Normal View History

2024-03-21 10:57:53 +08:00
import { $t } from '@/locales';
2024-03-08 17:59:45 +08:00
/**
* Transform record to option
*
* @example
* ```ts
* const record = {
* key1: 'label1',
* key2: 'label2'
* };
* const options = transformRecordToOption(record);
* // [
* // { value: 'key1', label: 'label1' },
* // { value: 'key2', label: 'label2' }
* // ]
* ```;
*
* @param record
*/
export function transformRecordToOption<T extends Record<string, string>>(record: T) {
return Object.entries(record).map(([value, label]) => ({
value,
label
2024-03-21 10:57:53 +08:00
})) as CommonType.Option<keyof T>[];
}
/**
* Translate options
*
* @param options
*/
2024-04-19 17:42:11 +08:00
export function translateOptions(options: CommonType.Option<string | number>[]) {
2024-03-21 10:57:53 +08:00
return options.map(option => ({
...option,
label: $t(option.label as App.I18n.I18nKey)
}));
2024-03-08 17:59:45 +08:00
}
/**
* Translate options
*
* @param options
*/
export function translateOptions2(options: string[]) {
return options.map(option => ({
value: option,
label: option
}));
}