Просмотр исходного кода

Feature: map Object to possible actual name

吴迪 3 лет назад
Родитель
Сommit
5a5076af8e
1 измененных файлов с 5 добавлено и 4 удалено
  1. 5 4
      ocean-engine-doc-maker.user.js

+ 5 - 4
ocean-engine-doc-maker.user.js

@@ -11,7 +11,8 @@
 
 (function() {
     'use strict';
-    const convertType = (type) => {
+    const makeClassName = s => s?.replace(/(?:_|^)([a-z])/g, g => g.substring(g.length - 1).toUpperCase()) || ''
+    const convertType = (type, name) => {
         const isList = type.endsWith('[]')
         if (isList) type = type.substring(0, type.length - 2)
         switch (type.toLowerCase()) {
@@ -24,7 +25,7 @@
             case 'boolean': type = 'Boolean'; break
             case 'string': type = 'String'; break
             case 'json':
-            case 'object': type = 'Object'; break
+            case 'object': type = makeClassName(name) || 'Object'; break
         }
         if (isList) type = `List<${type}>`
         return type
@@ -34,7 +35,7 @@
         for (const node of childNodes) {
             if (node.nodeType === Node.TEXT_NODE) {
                 const text = node.textContent.trim()
-                if (text) return text
+                if (text) return text.replace(/\n/g, '')
             }
         }
     }
@@ -44,7 +45,7 @@
         const optional = tds[0].querySelector('span.tag')?.innerText?.trim() !== '必填'
         const type = tds[1].innerText.trim()
         const comment = tds[2].innerText.trim()
-        return `  val ${name.replace(/\n/g, '')}: ${convertType(type)}${optional ? '? = null' : ''}, // ${comment.replace(/\n/g, ' ')}\n`
+        return `  val ${name}: ${convertType(type, name)}${optional ? '? = null' : ''}, // ${comment.replace(/\n/g, ' ')}\n`
     }
     const parseEnumRow = (tds) => {
         const name = parseName(tds[0].querySelector('div')?.childNodes)