import { Md5 } from 'ts-md5'; import { $t } from '@/locales'; /** * 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>(record: T) { return Object.entries(record).map(([value, label]) => ({ value, label })) as CommonType.Option[]; } /** * Transform record to option with keys of number * * @param record * @param reverse */ export function transformRecordToNumberOption>(record: T, reverse: boolean = false) { const options = Object.entries(record).map(([value, label]) => ({ value: Number(value), label })) as CommonType.Option[]; return reverse ? options.sort((a: any, b: any) => b.value - a.value) : options; } /** * Translate options * * @param options */ export function translateOptions(options: CommonType.Option[]) { return options.map(option => ({ ...option, label: $t(option.label as App.I18n.I18nKey) })); } /** * Translate options * * @param options */ export function translateOptions2(options: string[]) { return options.map(option => ({ value: option, label: option })); } /** * tag Color * * @param index */ export function tagColor(index: number) { const tagMap: Record = { 0: 'error', 1: 'info', 2: 'success', 3: 'warning', 4: 'primary' }; if (index === null || index < 0) { return tagMap[1]; } return tagMap[index % 5]; } /** * MD-5 哈希 * * @param text 明文 * @returns md5哈希 */ export function md5(text: string): string { const md5Digest = new Md5(); md5Digest.appendAsciiStr(text); return md5Digest.end() as string; } /** * Toggle html class * * @param className */ export function toggleHtmlClass(className: string) { function add() { document.documentElement.classList.add(className); } function remove() { document.documentElement.classList.remove(className); } return { add, remove }; }