@@ -573,6 +573,57 @@ export const parseDescriptionEnum = (
573573 return enumMap ;
574574} ;
575575
576+ /**
577+ * 通过自定义正则表达式解析 description 中的枚举翻译
578+ * @param description 描述文本
579+ * @param regex 自定义正则表达式,用于匹配枚举项,例如:(\S+)\s*=\s*(\S+)
580+ * @returns Map<number, string> 枚举值到标签的映射
581+ */
582+ export const parseDescriptionEnumByReg = (
583+ description : string ,
584+ regex : string | RegExp
585+ ) : Map < number , string > => {
586+ const enumMap = new Map < number , string > ( ) ;
587+ if ( ! description ) return enumMap ;
588+
589+ // 将字符串正则转换为 RegExp 对象,确保有全局标志
590+ let regExp : RegExp ;
591+ if ( typeof regex === 'string' ) {
592+ const flagsMatch = regex . match ( / \/ ( [ g i m s u y ] * ) $ / ) ;
593+ if ( flagsMatch ) {
594+ const parts = regex . split ( '/' ) ;
595+ const pattern = parts . slice ( 1 , - 1 ) . join ( '/' ) ;
596+ const flags = flagsMatch [ 1 ] || '' ;
597+ regExp = new RegExp ( pattern , flags . includes ( 'g' ) ? flags : flags + 'g' ) ;
598+ } else {
599+ regExp = new RegExp ( regex , 'g' ) ;
600+ }
601+ } else {
602+ const flags = regex . flags ;
603+ regExp = flags . includes ( 'g' )
604+ ? regex
605+ : new RegExp ( regex . source , flags + 'g' ) ;
606+ }
607+
608+ // 匹配所有结果,然后对每个匹配结果按 = 拆分
609+ let match : RegExpExecArray | null ;
610+ while ( ( match = regExp . exec ( description ) ) !== null ) {
611+ const fullMatch = match [ 0 ] . trim ( ) ;
612+ // 按 = 拆分,左边是标签,右边是值
613+ const parts = fullMatch . split ( / \s * = \s * / ) ;
614+ if ( parts . length >= 2 ) {
615+ const label = parts [ 0 ] . trim ( ) ;
616+ const valueStr = parts [ 1 ] . trim ( ) ;
617+ const numValue = Number ( valueStr ) ;
618+ if ( label && valueStr && ! isNaN ( numValue ) ) {
619+ enumMap . set ( numValue , label ) ;
620+ }
621+ }
622+ }
623+
624+ return enumMap ;
625+ } ;
626+
576627/**
577628 * 获取默认的二进制媒体类型列表
578629 */
0 commit comments