@@ -494,3 +494,58 @@ export function isAllNumber(arr) {
494494export function capitalizeFirstLetter ( str : string ) {
495495 return str . replace ( / ^ [ a - z ] / , ( match ) => match . toUpperCase ( ) ) ;
496496}
497+
498+ // 解析 description 中的枚举翻译
499+ export const parseDescriptionEnum = (
500+ description : string
501+ ) : Map < number , string > => {
502+ const enumMap = new Map < number , string > ( ) ;
503+ if ( ! description ) return enumMap ;
504+
505+ // 首先处理可能的总体描述,例如 "系统用户角色:User=0,..."
506+ let descToProcess = description ;
507+ const mainDescriptionMatch = description . match ( / ^ ( [ ^ : ] + ) : ( .* ) / ) ;
508+ if ( mainDescriptionMatch ) {
509+ // 如果有总体描述(如 "系统用户角色:"),只处理冒号后面的部分
510+ descToProcess = mainDescriptionMatch [ 2 ] ;
511+ }
512+
513+ // 匹配形如 "User(普通用户)=0" 或 "User=0" 的模式
514+ const enumPattern = / ( [ ^ ( ) : , = ] + ) (?: \( ( [ ^ ) ] + ) \) ) ? = ( \d + ) / g;
515+ let match : RegExpExecArray | null ;
516+
517+ while ( ( match = enumPattern . exec ( descToProcess ) ) !== null ) {
518+ const name = match [ 1 ] ? match [ 1 ] . trim ( ) : '' ;
519+ const valueStr = match [ 3 ] ? match [ 3 ] . trim ( ) : '' ;
520+
521+ if ( valueStr && ! isNaN ( Number ( valueStr ) ) ) {
522+ // 统一使用英文key(如User)
523+ enumMap . set ( Number ( valueStr ) , name ) ;
524+ }
525+ }
526+
527+ // 如果没有匹配到任何枚举,尝试使用简单的分割方法作为后备
528+ if ( enumMap . size === 0 ) {
529+ const pairs = descToProcess . split ( ',' ) ;
530+ pairs . forEach ( ( pair ) => {
531+ const parts = pair . split ( '=' ) ;
532+ if ( parts . length === 2 ) {
533+ let label = parts [ 0 ] . trim ( ) ;
534+ const value = parts [ 1 ] . trim ( ) ;
535+
536+ // 处理可能带有括号的情况
537+ const bracketMatch = label . match ( / ( [ ^ ( ] + ) \( ( [ ^ ) ] + ) \) / ) ;
538+ if ( bracketMatch ) {
539+ // 只使用括号前的英文key
540+ label = bracketMatch [ 1 ] . trim ( ) ;
541+ }
542+
543+ if ( label && value && ! isNaN ( Number ( value ) ) ) {
544+ enumMap . set ( Number ( value ) , label ) ;
545+ }
546+ }
547+ } ) ;
548+ }
549+
550+ return enumMap ;
551+ } ;
0 commit comments