16 lines
807 B
TypeScript
16 lines
807 B
TypeScript
import type { Dict, DictItem } from "./types";
|
|
|
|
export const createDict = (items: DictItem[]): Dict => {
|
|
const normalized = items.map((item, idx) => ({ order: idx, ...item }));
|
|
const get = (value?: string | null) => normalized.find((i) => i.value === value);
|
|
const list = () =>
|
|
[...normalized].sort((a, b) => (a.order || 0) - (b.order || 0));
|
|
return { items: normalized, get, list };
|
|
};
|
|
|
|
export const getDictItem = (dict: Dict, value?: string | null) => dict.get(value);
|
|
export const getDictLabel = (dict: Dict, value?: string | null) => dict.get(value)?.label || value || "";
|
|
export const getDictColor = (dict: Dict, value?: string | null) => dict.get(value)?.color;
|
|
export const getDictOptions = (dict: Dict) =>
|
|
dict.list().map((i) => ({ label: i.label, value: i.value, disabled: false }));
|