统一格式化参数的方法

This commit is contained in:
fofolee 2025-01-06 09:43:48 +08:00
parent d98966a5b0
commit 2dbd6f0c50
16 changed files with 130 additions and 144 deletions

View File

@ -67,8 +67,7 @@ import VariableInput from "components/composer/ui/VariableInput.vue";
import NumberInput from "components/composer/ui/NumberInput.vue"; import NumberInput from "components/composer/ui/NumberInput.vue";
import ArrayEditor from "components/composer/ui/ArrayEditor.vue"; import ArrayEditor from "components/composer/ui/ArrayEditor.vue";
import { import {
stringifyWithType, stringifyArgv,
stringifyObject,
parseToHasType, parseToHasType,
parseFunction, parseFunction,
} from "js/composer/formatString"; } from "js/composer/formatString";
@ -129,11 +128,7 @@ export default defineComponent({
}, },
generateCode(funcName, argvs) { generateCode(funcName, argvs) {
const newArgvs = argvs const newArgvs = argvs
.map((argv) => { .map((argv) => stringifyArgv(argv))
return typeof argv === "string"
? stringifyWithType(argv)
: stringifyObject(argv);
})
.filter((item) => item != null && item !== ""); .filter((item) => item != null && item !== "");
console.log(newArgvs); console.log(newArgvs);
return `${funcName}(${newArgvs.join(",")})`; return `${funcName}(${newArgvs.join(",")})`;

View File

@ -185,7 +185,7 @@
<script> <script>
import { defineComponent } from "vue"; import { defineComponent } from "vue";
import VariableInput from "components/composer/ui/VariableInput.vue"; import VariableInput from "components/composer/ui/VariableInput.vue";
import { stringifyObject, parseFunction } from "js/composer/formatString"; import { stringifyArgv, parseFunction } from "js/composer/formatString";
export default defineComponent({ export default defineComponent({
name: "AsymmetricCryptoEditor", name: "AsymmetricCryptoEditor",
@ -274,7 +274,7 @@ export default defineComponent({
return argvs; return argvs;
}, },
generateCode(argvs = this.argvs) { generateCode(argvs = this.argvs) {
return `${this.modelValue.value}(${stringifyObject({ return `${this.modelValue.value}(${stringifyArgv({
text: argvs.text, text: argvs.text,
algorithm: argvs.algorithm, algorithm: argvs.algorithm,
operation: argvs.operation, operation: argvs.operation,

View File

@ -362,7 +362,7 @@
<script> <script>
import { defineComponent } from "vue"; import { defineComponent } from "vue";
import { stringifyObject, parseFunction } from "js/composer/formatString"; import { stringifyArgv, parseFunction } from "js/composer/formatString";
import VariableInput from "components/composer/ui/VariableInput.vue"; import VariableInput from "components/composer/ui/VariableInput.vue";
import NumberInput from "components/composer/ui/NumberInput.vue"; import NumberInput from "components/composer/ui/NumberInput.vue";
import ArrayEditor from "components/composer/ui/ArrayEditor.vue"; import ArrayEditor from "components/composer/ui/ArrayEditor.vue";
@ -492,63 +492,63 @@ export default defineComponent({
generateCode(argvs = this.argvs) { generateCode(argvs = this.argvs) {
switch (argvs.operation) { switch (argvs.operation) {
case "from": case "from":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.data argvs.data
)}, "${argvs.encoding}")`; )}, "${argvs.encoding}")`;
case "toString": case "toString":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.buffer argvs.buffer
)}, "${argvs.encoding}", ${argvs.start}, ${argvs.end})`; )}, "${argvs.encoding}", ${argvs.start}, ${argvs.end})`;
case "write": case "write":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.buffer argvs.buffer
)}, ${stringifyObject(argvs.string)}, ${argvs.offset}, ${ )}, ${stringifyArgv(argvs.string)}, ${argvs.offset}, ${
argvs.length argvs.length
}, "${argvs.encoding}")`; }, "${argvs.encoding}")`;
case "fill": case "fill":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.buffer argvs.buffer
)}, ${stringifyObject(argvs.value)}, ${argvs.offset}, ${ )}, ${stringifyArgv(argvs.value)}, ${argvs.offset}, ${argvs.end}, "${
argvs.end argvs.encoding
}, "${argvs.encoding}")`; }")`;
case "copy": case "copy":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.source argvs.source
)}, ${stringifyObject(argvs.target)}, ${argvs.targetStart}, ${ )}, ${stringifyArgv(argvs.target)}, ${argvs.targetStart}, ${
argvs.sourceStart argvs.sourceStart
}, ${argvs.sourceEnd})`; }, ${argvs.sourceEnd})`;
case "compare": case "compare":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.buf1 argvs.buf1
)}, ${stringifyObject(argvs.buf2)})`; )}, ${stringifyArgv(argvs.buf2)})`;
case "concat": case "concat":
const buffersStr = argvs.buffers const buffersStr = argvs.buffers
.map((buf) => stringifyObject(buf)) .map((buf) => stringifyArgv(buf))
.join(", "); .join(", ");
return `${this.modelValue.value}.${argvs.operation}([${buffersStr}]${ return `${this.modelValue.value}.${argvs.operation}([${buffersStr}]${
argvs.totalLength !== undefined ? `, ${argvs.totalLength}` : "" argvs.totalLength !== undefined ? `, ${argvs.totalLength}` : ""
})`; })`;
case "indexOf": case "indexOf":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.buffer argvs.buffer
)}, ${stringifyObject(argvs.value)}, ${argvs.byteOffset}, "${ )}, ${stringifyArgv(argvs.value)}, ${argvs.byteOffset}, "${
argvs.encoding argvs.encoding
}")`; }")`;
case "slice": case "slice":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.buffer argvs.buffer
)}, ${argvs.start}, ${argvs.end})`; )}, ${argvs.start}, ${argvs.end})`;
case "swap": case "swap":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.buffer argvs.buffer
)}, ${argvs.size})`; )}, ${argvs.size})`;

View File

@ -185,7 +185,7 @@
<script> <script>
import { defineComponent } from "vue"; import { defineComponent } from "vue";
import VariableInput from "components/composer/ui/VariableInput.vue"; import VariableInput from "components/composer/ui/VariableInput.vue";
import { stringifyObject, parseFunction } from "js/composer/formatString"; import { stringifyArgv, parseFunction } from "js/composer/formatString";
export default defineComponent({ export default defineComponent({
name: "SymmetricCryptoEditor", name: "SymmetricCryptoEditor",
@ -293,7 +293,7 @@ export default defineComponent({
}, },
methods: { methods: {
generateCode(argvs = this.argvs) { generateCode(argvs = this.argvs) {
return `${this.modelValue.value}(${stringifyObject({ return `${this.modelValue.value}(${stringifyArgv({
text: argvs.text, text: argvs.text,
algorithm: argvs.algorithm, algorithm: argvs.algorithm,
mode: argvs.mode, mode: argvs.mode,

View File

@ -141,7 +141,7 @@
<script> <script>
import { defineComponent } from "vue"; import { defineComponent } from "vue";
import { stringifyObject, parseFunction } from "js/composer/formatString"; import { stringifyArgv, parseFunction } from "js/composer/formatString";
import VariableInput from "components/composer/ui/VariableInput.vue"; import VariableInput from "components/composer/ui/VariableInput.vue";
import NumberInput from "components/composer/ui/NumberInput.vue"; import NumberInput from "components/composer/ui/NumberInput.vue";
@ -244,9 +244,9 @@ export default defineComponent({
strategy: argvs.options.strategy, strategy: argvs.options.strategy,
}; };
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.data argvs.data
)}, "${argvs.method}", ${stringifyObject(options)})`; )}, "${argvs.method}", ${stringifyArgv(options)})`;
}, },
parseCodeToArgvs(code) { parseCodeToArgvs(code) {
if (!code) return null; if (!code) return null;

View File

@ -383,7 +383,7 @@
import { defineComponent } from "vue"; import { defineComponent } from "vue";
import VariableInput from "components/composer/ui/VariableInput.vue"; import VariableInput from "components/composer/ui/VariableInput.vue";
import NumberInput from "components/composer/ui/NumberInput.vue"; import NumberInput from "components/composer/ui/NumberInput.vue";
import { stringifyObject, parseFunction } from "js/composer/formatString"; import { stringifyArgv, parseFunction } from "js/composer/formatString";
// //
const ENCODING_OPTIONS = [ const ENCODING_OPTIONS = [
@ -557,7 +557,7 @@ export default defineComponent({
break; break;
} }
return `${this.modelValue.value}(${stringifyObject(params)})`; return `${this.modelValue.value}(${stringifyArgv(params)})`;
}, },
updateArgvs(key, value) { updateArgvs(key, value) {
const argvs = { ...this.argvs }; const argvs = { ...this.argvs };

View File

@ -169,8 +169,7 @@ import VariableInput from "components/composer/ui/VariableInput.vue";
import NumberInput from "components/composer/ui/NumberInput.vue"; import NumberInput from "components/composer/ui/NumberInput.vue";
import DictEditor from "components/composer/ui/DictEditor.vue"; import DictEditor from "components/composer/ui/DictEditor.vue";
import { import {
stringifyObject, stringifyArgv,
stringifyWithType,
parseFunction, parseFunction,
} from "js/composer/formatString"; } from "js/composer/formatString";
import { import {
@ -469,13 +468,13 @@ export default defineComponent({
if (!url) return; if (!url) return;
const configStr = Object.keys(restConfig).length const configStr = Object.keys(restConfig).length
? `, ${stringifyObject(restConfig)}` ? `, ${stringifyArgv(restConfig)}`
: ""; : "";
return `${ return `${
this.modelValue.value this.modelValue.value
}.${method.toLowerCase()}(${stringifyWithType(url)}${ }.${method.toLowerCase()}(${stringifyArgv(url)}${
this.hasRequestData ? `, ${stringifyObject(data)}` : "" this.hasRequestData ? `, ${stringifyArgv(data)}` : ""
}${configStr})`; }${configStr})`;
}, },
updateArgvs(key, value) { updateArgvs(key, value) {

View File

@ -73,7 +73,7 @@
<script> <script>
import { defineComponent } from "vue"; import { defineComponent } from "vue";
import { stringifyObject, parseFunction } from "js/composer/formatString"; import { stringifyArgv, parseFunction } from "js/composer/formatString";
import VariableInput from "components/composer/ui/VariableInput.vue"; import VariableInput from "components/composer/ui/VariableInput.vue";
export default defineComponent({ export default defineComponent({
@ -151,17 +151,17 @@ export default defineComponent({
generateCode(argvs = this.argvs) { generateCode(argvs = this.argvs) {
switch (argvs.operation) { switch (argvs.operation) {
case "lookupHost": case "lookupHost":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.hostname argvs.hostname
)}, ${stringifyObject(argvs.options)})`; )}, ${stringifyArgv(argvs.options)})`;
case "reverseResolve": case "reverseResolve":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.ip argvs.ip
)})`; )})`;
default: default:
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.hostname argvs.hostname
)})`; )})`;
} }

View File

@ -169,7 +169,7 @@
<script> <script>
import { defineComponent } from "vue"; import { defineComponent } from "vue";
import { stringifyObject, parseFunction } from "js/composer/formatString"; import { stringifyArgv, parseFunction } from "js/composer/formatString";
import VariableInput from "components/composer/ui/VariableInput.vue"; import VariableInput from "components/composer/ui/VariableInput.vue";
import DictEditor from "components/composer/ui/DictEditor.vue"; import DictEditor from "components/composer/ui/DictEditor.vue";
@ -305,47 +305,45 @@ export default defineComponent({
case "parse": case "parse":
case "isAbsolute": case "isAbsolute":
case "parseComponents": case "parseComponents":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.url argvs.url
)})`; )})`;
case "format": case "format":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.urlObject argvs.urlObject
)})`; )})`;
case "parseQuery": case "parseQuery":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.query argvs.query
)})`; )})`;
case "formatQuery": case "formatQuery":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.queryParams argvs.queryParams
)})`; )})`;
case "parsePath": case "parsePath":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.path argvs.path
)})`; )})`;
case "parseHost": case "parseHost":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.host argvs.host
)})`; )})`;
case "getQueryParam": case "getQueryParam":
case "removeQueryParam": case "removeQueryParam":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.url argvs.url
)}, ${stringifyObject(argvs.param)})`; )}, ${stringifyArgv(argvs.param)})`;
case "addQueryParam": case "addQueryParam":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.url argvs.url
)}, ${stringifyObject(argvs.param)}, ${stringifyObject( )}, ${stringifyArgv(argvs.param)}, ${stringifyArgv(argvs.value)})`;
argvs.value
)})`;
default: default:
return `${this.modelValue.value}.${argvs.operation}()`; return `${this.modelValue.value}.${argvs.operation}()`;

View File

@ -90,7 +90,7 @@
<script> <script>
import { defineComponent } from "vue"; import { defineComponent } from "vue";
import { stringifyObject, parseFunction } from "js/composer/formatString"; import { stringifyArgv, parseFunction } from "js/composer/formatString";
export default defineComponent({ export default defineComponent({
name: "OsEditor", name: "OsEditor",
@ -248,7 +248,7 @@ export default defineComponent({
return `${this.modelValue.value}.${argvs.operation}()`; return `${this.modelValue.value}.${argvs.operation}()`;
} }
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
params params
)})`; )})`;
}, },

View File

@ -161,7 +161,7 @@
<script> <script>
import { defineComponent } from "vue"; import { defineComponent } from "vue";
import { stringifyObject, parseFunction } from "js/composer/formatString"; import { stringifyArgv, parseFunction } from "js/composer/formatString";
import VariableInput from "components/composer/ui/VariableInput.vue"; import VariableInput from "components/composer/ui/VariableInput.vue";
export default defineComponent({ export default defineComponent({
@ -288,33 +288,33 @@ export default defineComponent({
case "dirname": case "dirname":
case "extname": case "extname":
case "isAbsolute": case "isAbsolute":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.path argvs.path
)})`; )})`;
case "basename": case "basename":
if (argvs.ext && argvs.ext.value) { if (argvs.ext && argvs.ext.value) {
return `${this.modelValue.value}.${ return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.operation argvs.path
}(${stringifyObject(argvs.path)}, ${stringifyObject(argvs.ext)})`; )}, ${stringifyArgv(argvs.ext)})`;
} }
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.path argvs.path
)})`; )})`;
case "join": case "join":
case "resolve": case "resolve":
return `${this.modelValue.value}.${argvs.operation}(${argvs.paths return `${this.modelValue.value}.${argvs.operation}(${argvs.paths
.map((p) => stringifyObject(p)) .map((p) => stringifyArgv(p))
.join(", ")})`; .join(", ")})`;
case "relative": case "relative":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.from argvs.from
)}, ${stringifyObject(argvs.to)})`; )}, ${stringifyArgv(argvs.to)})`;
case "format": case "format":
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject( return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
argvs.pathObject argvs.pathObject
)})`; )})`;

View File

@ -131,11 +131,7 @@
<script> <script>
import { defineComponent } from "vue"; import { defineComponent } from "vue";
import { import { parseFunction, stringifyArgv } from "js/composer/formatString";
parseFunction,
stringifyObject,
stringifyWithType,
} from "js/composer/formatString";
import VariableInput from "components/composer/ui/VariableInput.vue"; import VariableInput from "components/composer/ui/VariableInput.vue";
import NumberInput from "components/composer/ui/NumberInput.vue"; import NumberInput from "components/composer/ui/NumberInput.vue";
import DictEditor from "components/composer/ui/DictEditor.vue"; import DictEditor from "components/composer/ui/DictEditor.vue";
@ -239,7 +235,7 @@ export default defineComponent({
const args = []; const args = [];
// //
args.push(stringifyWithType(argvs.command)); args.push(stringifyArgv(argvs.command));
// //
const options = { ...argvs.options }; const options = { ...argvs.options };
@ -254,7 +250,7 @@ export default defineComponent({
}); });
if (Object.keys(options).length > 0) { if (Object.keys(options).length > 0) {
args.push(stringifyObject(options)); args.push(stringifyArgv(options));
} }
return `${this.modelValue.value}(${args.join(", ")})`; return `${this.modelValue.value}(${args.join(", ")})`;

View File

@ -85,7 +85,7 @@ const customComponentGuide = {
`, `,
objectCase: ` objectCase: `
// 对象参数的情况 // 对象参数的情况
const formattedConfig = stringifyObject(config); const formattedConfig = stringifyArgv(config);
return \`functionName(\${formattedConfig})\`; return \`functionName(\${formattedConfig})\`;
`, `,
}, },
@ -182,7 +182,7 @@ const customComponentGuide = {
`, `,
}, },
stringHandling: { stringHandling: {
stringifyObject: { stringifyArgv: {
description: "将对象转换为代码字符串", description: "将对象转换为代码字符串",
usage: "用于生成包含对象参数的代码", usage: "用于生成包含对象参数的代码",
example: "将 {key: 'value'} 转换为 '{key:\"value\"}'", example: "将 {key: 'value'} 转换为 '{key:\"value\"}'",
@ -246,12 +246,6 @@ const customComponentGuide = {
args: "解析后的参数数组,根据路径规则处理变量格式", args: "解析后的参数数组,根据路径规则处理变量格式",
}, },
}, },
stringifyWithType: {
description: "将带类型的值转换为字符串",
usage: "用于处理 VariableInput 类型的值",
example:
"将 {value: 'text', isString: true, __varInputVal__: true,} 转换为 '\"text\"'",
},
parseToHasType: { parseToHasType: {
description: "将字符串解析为带类型的值", description: "将字符串解析为带类型的值",
usage: "用于解析 VariableInput 类型的值", usage: "用于解析 VariableInput 类型的值",

View File

@ -1,26 +1,12 @@
import { parse } from "@babel/parser"; import { parse } from "@babel/parser";
/** /**
* 根据值的类型和属性将其转换为字符串 * 处理带有 __varInputVal__ 属性的对象
* 1. 对于带有 __varInputVal__ 属性的对象根据该属性决定是否添加引号 * @param {Object} argv 要处理的对象
* 2. 对于普通字符串自动添加引号 * @returns {string} 处理后的字符串
* 3. 对于其他类型(数字布尔等)直接转换
* @param {Object|string|number|boolean} argv 要转换的值
* @returns {string} 转换后的字符串
*/ */
export const stringifyWithType = (argv) => { const stringifyVarInputVal = (argv) => {
// 处理带有类型标记的对象 return argv.isString ? `"${argv.value}"` : argv.value;
if (typeof argv === "object" && argv.hasOwnProperty("__varInputVal__")) {
return argv.isString ? `"${argv.value}"` : argv.value;
}
// 处理普通字符串
if (typeof argv === "string") {
return `"${argv}"`;
}
// 处理其他类型
return argv;
}; };
/** /**
@ -51,9 +37,9 @@ const removeEmptyValues = (obj) => {
const processObject = (obj, parentPath = "") => { const processObject = (obj, parentPath = "") => {
// 移除空值 // 移除空值
obj = removeEmptyValues(obj); obj = removeEmptyValues(obj);
// 如果是 VariableInput 的输出,直接用 stringifyWithType 处理 // 如果是 VariableInput 的输出,直接用 stringifyVarInputVal 处理
if (obj?.hasOwnProperty("__varInputVal__")) { if (obj?.hasOwnProperty("__varInputVal__")) {
return stringifyWithType(obj); return stringifyVarInputVal(obj);
} }
let result = "{\n"; let result = "{\n";
@ -64,16 +50,18 @@ const processObject = (obj, parentPath = "") => {
// 处理对象类型 // 处理对象类型
if (value && typeof value === "object") { if (value && typeof value === "object") {
// 如果是 VariableInput 的输出,直接用 stringifyWithType 处理 // 如果是 VariableInput 的输出,直接用 stringifyVarInputVal 处理
if (value.hasOwnProperty("__varInputVal__")) { if (value.hasOwnProperty("__varInputVal__")) {
valueStr = stringifyWithType(value); valueStr = stringifyVarInputVal(value);
} else { } else {
valueStr = processObject(value, parentPath + " "); valueStr = processObject(value, parentPath + " ");
} }
} }
// 处理其他类型 // 处理其他类型
else { else if (value && typeof value === "string") {
valueStr = stringifyWithType(value); valueStr = `"${value}"`;
} else {
valueStr = value;
} }
// 添加缩进 // 添加缩进
@ -94,13 +82,13 @@ const processObject = (obj, parentPath = "") => {
* @param {Object} jsonObj 要格式化的对象 * @param {Object} jsonObj 要格式化的对象
* @returns {string} 格式化后的JSON字符串 * @returns {string} 格式化后的JSON字符串
*/ */
export const stringifyObject = (jsonObj) => { const stringifyObject = (jsonObj) => {
if (jsonObj?.hasOwnProperty("__varInputVal__")) {
return stringifyVarInputVal(jsonObj);
}
if (jsonObj instanceof Array) { if (jsonObj instanceof Array) {
return `[${jsonObj.map((item) => stringifyObject(item)).join(",")}]`; return `[${jsonObj.map((item) => stringifyObject(item)).join(",")}]`;
} }
if (jsonObj?.hasOwnProperty("__varInputVal__")) {
return stringifyWithType(jsonObj);
}
try { try {
return processObject(jsonObj); return processObject(jsonObj);
} catch (e) { } catch (e) {
@ -109,6 +97,24 @@ export const stringifyObject = (jsonObj) => {
} }
}; };
/**
* 格式化参数为字符串
* @param {Object|string|number|boolean} argv 要格式化的参数
* @returns {string} 格式化后的字符串
*/
export const stringifyArgv = (argv) => {
// 处理普通字符串
if (typeof argv === "string") {
return `"${argv}"`;
}
// 处理对象
if (typeof argv === "object") {
return stringifyObject(argv);
}
// 处理其他类型
return argv;
};
/** /**
* 解析字符串为variableInput对象 * 解析字符串为variableInput对象
* @param {string} str 要解析的字符串 * @param {string} str 要解析的字符串

View File

@ -4,14 +4,14 @@
* @param {Array} selectedActions 已选择的操作列表 * @param {Array} selectedActions 已选择的操作列表
* @returns {string} 生成的代码 * @returns {string} 生成的代码
*/ */
import { stringifyObject, stringifyWithType } from "./formatString"; import { stringifyArgv } from "./formatString";
// 生成 goto 参数字符串 // 生成 goto 参数字符串
function generateGotoArgs(goto) { function generateGotoArgs(goto) {
const args = []; const args = [];
// URL // URL
const urlStr = stringifyWithType(goto.url); const urlStr = stringifyArgv(goto.url);
args.push(urlStr); args.push(urlStr);
// Headers // Headers
@ -24,7 +24,7 @@ function generateGotoArgs(goto) {
headers.userAgent = goto.headers.userAgent; headers.userAgent = goto.headers.userAgent;
} }
console.log("Headers:", JSON.stringify(headers, null, 2)); console.log("Headers:", JSON.stringify(headers, null, 2));
args.push(stringifyObject(headers, true)); args.push(stringifyArgv(headers, true));
} }
// Timeout // Timeout
@ -121,7 +121,7 @@ function generateRunArgs(run) {
if (run.proxy) options.proxy = run.proxy; if (run.proxy) options.proxy = run.proxy;
if (run.viewport) options.viewport = run.viewport; if (run.viewport) options.viewport = run.viewport;
return Object.keys(options).length ? stringifyObject(options) : ""; return Object.keys(options).length ? stringifyArgv(options) : "";
} }
// 生成操作参数字符串 // 生成操作参数字符串
@ -143,11 +143,11 @@ function generateActionArgs(action, config) {
case "mousedown": case "mousedown":
case "mouseup": case "mouseup":
case "focus": case "focus":
result = stringifyWithType(config.selector); result = stringifyArgv(config.selector);
break; break;
case "css": case "css":
case "paste": case "paste":
result = stringifyWithType(config.value); result = stringifyArgv(config.value);
break; break;
case "press": case "press":
result = generatePressArgs(config); result = generatePressArgs(config);
@ -163,7 +163,7 @@ function generateActionArgs(action, config) {
break; break;
case "cookies": case "cookies":
case "removeCookies": case "removeCookies":
result = stringifyWithType(config.name); result = stringifyArgv(config.name);
break; break;
case "setCookies": case "setCookies":
result = generateSetCookiesArgs(config); result = generateSetCookiesArgs(config);
@ -190,7 +190,7 @@ function generateActionArgs(action, config) {
result = generateDownloadArgs(config); result = generateDownloadArgs(config);
break; break;
case "devTools": case "devTools":
result = stringifyWithType(config.mode); result = stringifyArgv(config.mode);
break; break;
default: default:
result = ""; result = "";
@ -208,7 +208,7 @@ function generateActionArgs(action, config) {
function generateWaitArgs(config) { function generateWaitArgs(config) {
switch (config.type) { switch (config.type) {
case "selector": case "selector":
return stringifyWithType(config.selector); return stringifyArgv(config.selector);
case "function": case "function":
return config.function; return config.function;
case "time": case "time":
@ -220,7 +220,7 @@ function generateWaitArgs(config) {
// 生成 press 参数字符串 // 生成 press 参数字符串
function generatePressArgs(config) { function generatePressArgs(config) {
const args = [stringifyWithType(config.key)]; const args = [stringifyArgv(config.key)];
if (config.modifiers?.length) { if (config.modifiers?.length) {
args.push(JSON.stringify(config.modifiers)); args.push(JSON.stringify(config.modifiers));
} }
@ -231,12 +231,12 @@ function generatePressArgs(config) {
function generateScreenshotArgs(config) { function generateScreenshotArgs(config) {
const args = []; const args = [];
if (config.rect) { if (config.rect) {
args.push(stringifyObject(config.rect)); args.push(stringifyArgv(config.rect));
} else if (config.selector) { } else if (config.selector) {
args.push(stringifyWithType(config.selector)); args.push(stringifyArgv(config.selector));
} }
if (config.savePath) { if (config.savePath) {
args.push(stringifyWithType(config.savePath)); args.push(stringifyArgv(config.savePath));
} }
return args.join(", "); return args.join(", ");
} }
@ -245,10 +245,10 @@ function generateScreenshotArgs(config) {
function generatePdfArgs(config) { function generatePdfArgs(config) {
const args = []; const args = [];
if (config.savePath) { if (config.savePath) {
args.push(stringifyWithType(config.savePath)); args.push(stringifyArgv(config.savePath));
} }
if (config.options) { if (config.options) {
args.push(stringifyObject(config.options)); args.push(stringifyArgv(config.options));
} }
return args.join(", "); return args.join(", ");
} }
@ -256,26 +256,26 @@ function generatePdfArgs(config) {
// 生成 device 参数字符串 // 生成 device 参数字符串
function generateDeviceArgs(config) { function generateDeviceArgs(config) {
if (config.type === "preset") { if (config.type === "preset") {
return stringifyWithType(config.deviceName); return stringifyArgv(config.deviceName);
} else { } else {
const options = {}; const options = {};
if (config.size) options.size = config.size; if (config.size) options.size = config.size;
if (config.useragent) options.userAgent = config.useragent; if (config.useragent) options.userAgent = config.useragent;
return stringifyObject(options); return stringifyArgv(options);
} }
} }
// 生成 setCookies 参数字符串 // 生成 setCookies 参数字符串
function generateSetCookiesArgs(config) { function generateSetCookiesArgs(config) {
if (!config.items?.length) return "[]"; if (!config.items?.length) return "[]";
return stringifyObject(config.items); return stringifyArgv(config.items);
} }
// 生成 evaluate 参数字符串 // 生成 evaluate 参数字符串
function generateEvaluateArgs(config) { function generateEvaluateArgs(config) {
const args = [config.function]; const args = [config.function];
if (config.args?.length) { if (config.args?.length) {
args.push(...config.args.map(stringifyWithType)); args.push(...config.args.map(stringifyArgv));
} }
return args.join(", "); return args.join(", ");
} }
@ -285,35 +285,35 @@ function generateWhenArgs(config) {
if (config.type === "function") { if (config.type === "function") {
return config.function; return config.function;
} else { } else {
return stringifyWithType(config.selector); return stringifyArgv(config.selector);
} }
} }
// 生成 file 参数字符串 // 生成 file 参数字符串
function generateFileArgs(config) { function generateFileArgs(config) {
const args = [stringifyWithType(config.selector)]; const args = [stringifyArgv(config.selector)];
if (config.files) { if (config.files) {
args.push(stringifyObject(config.files)); args.push(stringifyArgv(config.files));
} }
return args.join(", "); return args.join(", ");
} }
// 生成 value 参数字符串 // 生成 value 参数字符串
function generateValueArgs(config) { function generateValueArgs(config) {
return `${stringifyWithType(config.selector)}, ${stringifyWithType( return `${stringifyArgv(config.selector)}, ${stringifyArgv(
config.value config.value
)}`; )}`;
} }
// 生成 check 参数字符串 // 生成 check 参数字符串
function generateCheckArgs(config) { function generateCheckArgs(config) {
return `${stringifyWithType(config.selector)}, ${config.checked}`; return `${stringifyArgv(config.selector)}, ${config.checked}`;
} }
// 生成 scroll 参数字符串 // 生成 scroll 参数字符串
function generateScrollArgs(config) { function generateScrollArgs(config) {
if (config.type === "element") { if (config.type === "element") {
return stringifyWithType(config.selector); return stringifyArgv(config.selector);
} else { } else {
if (config.x !== undefined) { if (config.x !== undefined) {
return `${config.x}, ${config.y}`; return `${config.x}, ${config.y}`;
@ -325,9 +325,9 @@ function generateScrollArgs(config) {
// 生成 download 参数字符串 // 生成 download 参数字符串
function generateDownloadArgs(config) { function generateDownloadArgs(config) {
const args = [stringifyWithType(config.url)]; const args = [stringifyArgv(config.url)];
if (config.savePath) { if (config.savePath) {
args.push(stringifyWithType(config.savePath)); args.push(stringifyArgv(config.savePath));
} }
return args.join(", "); return args.join(", ");
} }

View File

@ -54,10 +54,8 @@
"formatString.js": { "formatString.js": {
"描述": "处理字符串格式化的工具函数,主要用于处理对象序列化和类型转换", "描述": "处理字符串格式化的工具函数,主要用于处理对象序列化和类型转换",
"主要功能": { "主要功能": {
"stringifyWithType": "根据值的类型和属性将其转换为字符串,支持带类型标记的对象", "parseFunction": "解析函数字符串,返回函数名和参数",
"removeEmptyValues": "递归移除对象中的空值", "stringifyArgv": "格式化参数为字符串,根据值的类型决定是否添加引号"
"processObject": "递归处理对象的值并格式化成字符串",
"stringifyObject": "格式化对象为JSON字符串根据值的类型决定是否添加引号"
} }
} }
} }