forked from drone/command-center-dashboard

shuishen
2025-04-02 e14358f85a7d32a753225f253ff223d17cf36c25
src/utils/formatter.js
@@ -1,4 +1,4 @@
import {validatenull} from "@/utils/validate";
import { validatenull } from '@/utils/validate'
/**
 * 格式化工具类
@@ -8,32 +8,35 @@
    try {
      // 为空则返回空
      if (validatenull(str)) {
        return '';
            return ''
      }
      // 解析并格式化JSON字符串
      str = JSON.stringify(JSON.parse(str), null, 2);
         str = JSON.stringify(JSON.parse(str), null, 2)
      // 使用HTML实体进行替换(不改变&符号)
      str = str.replace(/</g, '&lt;').replace(/>/g, '&gt;');
         str = str.replace(/</g, '&lt;').replace(/>/g, '&gt;')
      // 返回格式化的字符串,并添加样式类
      return str.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
        let cls = 'number';
         return str.replace(
            /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
            function (match) {
               let cls = 'number'
        if (/^"/.test(match)) {
          if (/:$/.test(match)) {
            cls = 'key';
                     cls = 'key'
          } else {
            cls = 'string';
                     cls = 'string'
          }
        } else if (/true|false/.test(match)) {
          cls = 'boolean';
                  cls = 'boolean'
        } else if (/null/.test(match)) {
          cls = 'null';
                  cls = 'null'
        }
        return match;
      });
               return match
            }
         )
    } catch (e) {
      return str;
         return str
    }
  }
}