@cloudcome/utils-core
Version:
cloudcome core utils
59 lines (58 loc) • 1.45 kB
JavaScript
function _defineDict(definition) {
const keys = Object.keys(definition);
return {
...[...Object.entries(definition)].reduce((acc, [key, dfn]) => {
if (key[0].toUpperCase() !== key[0]) {
throw new Error(`错误:枚举键名 ${key} 必须以大写字母开头`);
}
if (key.startsWith("$")) {
throw new Error(`错误:枚举键名 ${key} 不能以 $ 符号开头`);
}
acc[key] = dfn.value;
acc[`$${key}`] = { key, ...dfn };
return acc;
}, {}),
definition,
descriptions: keys.map((key) => ({
key,
...definition[key]
})),
keys,
length: keys.length,
values: keys.map((key) => definition[key].value),
kvRecord: keys.reduce((acc, key) => {
acc[key] = definition[key].value;
return acc;
}, {}),
vkRecord: keys.reduce((acc, key) => {
acc[definition[key].value] = key;
return acc;
}, {}),
toKeyRecord(prop) {
return keys.reduce((acc, key) => {
acc[key] = definition[key][prop];
return acc;
}, {});
},
toValRecord(prop) {
return keys.reduce(
(acc, key) => {
acc[definition[key].value] = definition[key][prop];
return acc;
},
{}
);
}
};
}
function declareDict() {
return {
define(definition) {
return _defineDict(definition);
}
};
}
export {
declareDict
};
//# sourceMappingURL=dict.mjs.map