mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-08 22:51:25 +08:00
统一格式化参数的方法
This commit is contained in:
parent
d98966a5b0
commit
2dbd6f0c50
@ -67,8 +67,7 @@ import VariableInput from "components/composer/ui/VariableInput.vue";
|
||||
import NumberInput from "components/composer/ui/NumberInput.vue";
|
||||
import ArrayEditor from "components/composer/ui/ArrayEditor.vue";
|
||||
import {
|
||||
stringifyWithType,
|
||||
stringifyObject,
|
||||
stringifyArgv,
|
||||
parseToHasType,
|
||||
parseFunction,
|
||||
} from "js/composer/formatString";
|
||||
@ -129,11 +128,7 @@ export default defineComponent({
|
||||
},
|
||||
generateCode(funcName, argvs) {
|
||||
const newArgvs = argvs
|
||||
.map((argv) => {
|
||||
return typeof argv === "string"
|
||||
? stringifyWithType(argv)
|
||||
: stringifyObject(argv);
|
||||
})
|
||||
.map((argv) => stringifyArgv(argv))
|
||||
.filter((item) => item != null && item !== "");
|
||||
console.log(newArgvs);
|
||||
return `${funcName}(${newArgvs.join(",")})`;
|
||||
|
@ -185,7 +185,7 @@
|
||||
<script>
|
||||
import { defineComponent } from "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({
|
||||
name: "AsymmetricCryptoEditor",
|
||||
@ -274,7 +274,7 @@ export default defineComponent({
|
||||
return argvs;
|
||||
},
|
||||
generateCode(argvs = this.argvs) {
|
||||
return `${this.modelValue.value}(${stringifyObject({
|
||||
return `${this.modelValue.value}(${stringifyArgv({
|
||||
text: argvs.text,
|
||||
algorithm: argvs.algorithm,
|
||||
operation: argvs.operation,
|
||||
|
@ -362,7 +362,7 @@
|
||||
|
||||
<script>
|
||||
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 NumberInput from "components/composer/ui/NumberInput.vue";
|
||||
import ArrayEditor from "components/composer/ui/ArrayEditor.vue";
|
||||
@ -492,63 +492,63 @@ export default defineComponent({
|
||||
generateCode(argvs = this.argvs) {
|
||||
switch (argvs.operation) {
|
||||
case "from":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.data
|
||||
)}, "${argvs.encoding}")`;
|
||||
|
||||
case "toString":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.buffer
|
||||
)}, "${argvs.encoding}", ${argvs.start}, ${argvs.end})`;
|
||||
|
||||
case "write":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.buffer
|
||||
)}, ${stringifyObject(argvs.string)}, ${argvs.offset}, ${
|
||||
)}, ${stringifyArgv(argvs.string)}, ${argvs.offset}, ${
|
||||
argvs.length
|
||||
}, "${argvs.encoding}")`;
|
||||
|
||||
case "fill":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.buffer
|
||||
)}, ${stringifyObject(argvs.value)}, ${argvs.offset}, ${
|
||||
argvs.end
|
||||
}, "${argvs.encoding}")`;
|
||||
)}, ${stringifyArgv(argvs.value)}, ${argvs.offset}, ${argvs.end}, "${
|
||||
argvs.encoding
|
||||
}")`;
|
||||
|
||||
case "copy":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.source
|
||||
)}, ${stringifyObject(argvs.target)}, ${argvs.targetStart}, ${
|
||||
)}, ${stringifyArgv(argvs.target)}, ${argvs.targetStart}, ${
|
||||
argvs.sourceStart
|
||||
}, ${argvs.sourceEnd})`;
|
||||
|
||||
case "compare":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.buf1
|
||||
)}, ${stringifyObject(argvs.buf2)})`;
|
||||
)}, ${stringifyArgv(argvs.buf2)})`;
|
||||
|
||||
case "concat":
|
||||
const buffersStr = argvs.buffers
|
||||
.map((buf) => stringifyObject(buf))
|
||||
.map((buf) => stringifyArgv(buf))
|
||||
.join(", ");
|
||||
return `${this.modelValue.value}.${argvs.operation}([${buffersStr}]${
|
||||
argvs.totalLength !== undefined ? `, ${argvs.totalLength}` : ""
|
||||
})`;
|
||||
|
||||
case "indexOf":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.buffer
|
||||
)}, ${stringifyObject(argvs.value)}, ${argvs.byteOffset}, "${
|
||||
)}, ${stringifyArgv(argvs.value)}, ${argvs.byteOffset}, "${
|
||||
argvs.encoding
|
||||
}")`;
|
||||
|
||||
case "slice":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.buffer
|
||||
)}, ${argvs.start}, ${argvs.end})`;
|
||||
|
||||
case "swap":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.buffer
|
||||
)}, ${argvs.size})`;
|
||||
|
||||
|
@ -185,7 +185,7 @@
|
||||
<script>
|
||||
import { defineComponent } from "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({
|
||||
name: "SymmetricCryptoEditor",
|
||||
@ -293,7 +293,7 @@ export default defineComponent({
|
||||
},
|
||||
methods: {
|
||||
generateCode(argvs = this.argvs) {
|
||||
return `${this.modelValue.value}(${stringifyObject({
|
||||
return `${this.modelValue.value}(${stringifyArgv({
|
||||
text: argvs.text,
|
||||
algorithm: argvs.algorithm,
|
||||
mode: argvs.mode,
|
||||
|
@ -141,7 +141,7 @@
|
||||
|
||||
<script>
|
||||
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 NumberInput from "components/composer/ui/NumberInput.vue";
|
||||
|
||||
@ -244,9 +244,9 @@ export default defineComponent({
|
||||
strategy: argvs.options.strategy,
|
||||
};
|
||||
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.data
|
||||
)}, "${argvs.method}", ${stringifyObject(options)})`;
|
||||
)}, "${argvs.method}", ${stringifyArgv(options)})`;
|
||||
},
|
||||
parseCodeToArgvs(code) {
|
||||
if (!code) return null;
|
||||
|
@ -383,7 +383,7 @@
|
||||
import { defineComponent } from "vue";
|
||||
import VariableInput from "components/composer/ui/VariableInput.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 = [
|
||||
@ -557,7 +557,7 @@ export default defineComponent({
|
||||
break;
|
||||
}
|
||||
|
||||
return `${this.modelValue.value}(${stringifyObject(params)})`;
|
||||
return `${this.modelValue.value}(${stringifyArgv(params)})`;
|
||||
},
|
||||
updateArgvs(key, value) {
|
||||
const argvs = { ...this.argvs };
|
||||
|
@ -169,8 +169,7 @@ import VariableInput from "components/composer/ui/VariableInput.vue";
|
||||
import NumberInput from "components/composer/ui/NumberInput.vue";
|
||||
import DictEditor from "components/composer/ui/DictEditor.vue";
|
||||
import {
|
||||
stringifyObject,
|
||||
stringifyWithType,
|
||||
stringifyArgv,
|
||||
parseFunction,
|
||||
} from "js/composer/formatString";
|
||||
import {
|
||||
@ -469,13 +468,13 @@ export default defineComponent({
|
||||
if (!url) return;
|
||||
|
||||
const configStr = Object.keys(restConfig).length
|
||||
? `, ${stringifyObject(restConfig)}`
|
||||
? `, ${stringifyArgv(restConfig)}`
|
||||
: "";
|
||||
|
||||
return `${
|
||||
this.modelValue.value
|
||||
}.${method.toLowerCase()}(${stringifyWithType(url)}${
|
||||
this.hasRequestData ? `, ${stringifyObject(data)}` : ""
|
||||
}.${method.toLowerCase()}(${stringifyArgv(url)}${
|
||||
this.hasRequestData ? `, ${stringifyArgv(data)}` : ""
|
||||
}${configStr})`;
|
||||
},
|
||||
updateArgvs(key, value) {
|
||||
|
@ -73,7 +73,7 @@
|
||||
|
||||
<script>
|
||||
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";
|
||||
|
||||
export default defineComponent({
|
||||
@ -151,17 +151,17 @@ export default defineComponent({
|
||||
generateCode(argvs = this.argvs) {
|
||||
switch (argvs.operation) {
|
||||
case "lookupHost":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.hostname
|
||||
)}, ${stringifyObject(argvs.options)})`;
|
||||
)}, ${stringifyArgv(argvs.options)})`;
|
||||
|
||||
case "reverseResolve":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.ip
|
||||
)})`;
|
||||
|
||||
default:
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.hostname
|
||||
)})`;
|
||||
}
|
||||
|
@ -169,7 +169,7 @@
|
||||
|
||||
<script>
|
||||
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 DictEditor from "components/composer/ui/DictEditor.vue";
|
||||
|
||||
@ -305,47 +305,45 @@ export default defineComponent({
|
||||
case "parse":
|
||||
case "isAbsolute":
|
||||
case "parseComponents":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.url
|
||||
)})`;
|
||||
|
||||
case "format":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.urlObject
|
||||
)})`;
|
||||
|
||||
case "parseQuery":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.query
|
||||
)})`;
|
||||
|
||||
case "formatQuery":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.queryParams
|
||||
)})`;
|
||||
|
||||
case "parsePath":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.path
|
||||
)})`;
|
||||
|
||||
case "parseHost":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.host
|
||||
)})`;
|
||||
|
||||
case "getQueryParam":
|
||||
case "removeQueryParam":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.url
|
||||
)}, ${stringifyObject(argvs.param)})`;
|
||||
)}, ${stringifyArgv(argvs.param)})`;
|
||||
|
||||
case "addQueryParam":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.url
|
||||
)}, ${stringifyObject(argvs.param)}, ${stringifyObject(
|
||||
argvs.value
|
||||
)})`;
|
||||
)}, ${stringifyArgv(argvs.param)}, ${stringifyArgv(argvs.value)})`;
|
||||
|
||||
default:
|
||||
return `${this.modelValue.value}.${argvs.operation}()`;
|
||||
|
@ -90,7 +90,7 @@
|
||||
|
||||
<script>
|
||||
import { defineComponent } from "vue";
|
||||
import { stringifyObject, parseFunction } from "js/composer/formatString";
|
||||
import { stringifyArgv, parseFunction } from "js/composer/formatString";
|
||||
|
||||
export default defineComponent({
|
||||
name: "OsEditor",
|
||||
@ -248,7 +248,7 @@ export default defineComponent({
|
||||
return `${this.modelValue.value}.${argvs.operation}()`;
|
||||
}
|
||||
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
params
|
||||
)})`;
|
||||
},
|
||||
|
@ -161,7 +161,7 @@
|
||||
|
||||
<script>
|
||||
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";
|
||||
|
||||
export default defineComponent({
|
||||
@ -288,33 +288,33 @@ export default defineComponent({
|
||||
case "dirname":
|
||||
case "extname":
|
||||
case "isAbsolute":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.path
|
||||
)})`;
|
||||
|
||||
case "basename":
|
||||
if (argvs.ext && argvs.ext.value) {
|
||||
return `${this.modelValue.value}.${
|
||||
argvs.operation
|
||||
}(${stringifyObject(argvs.path)}, ${stringifyObject(argvs.ext)})`;
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.path
|
||||
)}, ${stringifyArgv(argvs.ext)})`;
|
||||
}
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.path
|
||||
)})`;
|
||||
|
||||
case "join":
|
||||
case "resolve":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${argvs.paths
|
||||
.map((p) => stringifyObject(p))
|
||||
.map((p) => stringifyArgv(p))
|
||||
.join(", ")})`;
|
||||
|
||||
case "relative":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.from
|
||||
)}, ${stringifyObject(argvs.to)})`;
|
||||
)}, ${stringifyArgv(argvs.to)})`;
|
||||
|
||||
case "format":
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyObject(
|
||||
return `${this.modelValue.value}.${argvs.operation}(${stringifyArgv(
|
||||
argvs.pathObject
|
||||
)})`;
|
||||
|
||||
|
@ -131,11 +131,7 @@
|
||||
|
||||
<script>
|
||||
import { defineComponent } from "vue";
|
||||
import {
|
||||
parseFunction,
|
||||
stringifyObject,
|
||||
stringifyWithType,
|
||||
} from "js/composer/formatString";
|
||||
import { parseFunction, stringifyArgv } from "js/composer/formatString";
|
||||
import VariableInput from "components/composer/ui/VariableInput.vue";
|
||||
import NumberInput from "components/composer/ui/NumberInput.vue";
|
||||
import DictEditor from "components/composer/ui/DictEditor.vue";
|
||||
@ -239,7 +235,7 @@ export default defineComponent({
|
||||
const args = [];
|
||||
|
||||
// 添加命令
|
||||
args.push(stringifyWithType(argvs.command));
|
||||
args.push(stringifyArgv(argvs.command));
|
||||
|
||||
// 添加选项
|
||||
const options = { ...argvs.options };
|
||||
@ -254,7 +250,7 @@ export default defineComponent({
|
||||
});
|
||||
|
||||
if (Object.keys(options).length > 0) {
|
||||
args.push(stringifyObject(options));
|
||||
args.push(stringifyArgv(options));
|
||||
}
|
||||
|
||||
return `${this.modelValue.value}(${args.join(", ")})`;
|
||||
|
@ -85,7 +85,7 @@ const customComponentGuide = {
|
||||
`,
|
||||
objectCase: `
|
||||
// 对象参数的情况
|
||||
const formattedConfig = stringifyObject(config);
|
||||
const formattedConfig = stringifyArgv(config);
|
||||
return \`functionName(\${formattedConfig})\`;
|
||||
`,
|
||||
},
|
||||
@ -182,7 +182,7 @@ const customComponentGuide = {
|
||||
`,
|
||||
},
|
||||
stringHandling: {
|
||||
stringifyObject: {
|
||||
stringifyArgv: {
|
||||
description: "将对象转换为代码字符串",
|
||||
usage: "用于生成包含对象参数的代码",
|
||||
example: "将 {key: 'value'} 转换为 '{key:\"value\"}'",
|
||||
@ -246,12 +246,6 @@ const customComponentGuide = {
|
||||
args: "解析后的参数数组,根据路径规则处理变量格式",
|
||||
},
|
||||
},
|
||||
stringifyWithType: {
|
||||
description: "将带类型的值转换为字符串",
|
||||
usage: "用于处理 VariableInput 类型的值",
|
||||
example:
|
||||
"将 {value: 'text', isString: true, __varInputVal__: true,} 转换为 '\"text\"'",
|
||||
},
|
||||
parseToHasType: {
|
||||
description: "将字符串解析为带类型的值",
|
||||
usage: "用于解析 VariableInput 类型的值",
|
||||
|
@ -1,26 +1,12 @@
|
||||
import { parse } from "@babel/parser";
|
||||
|
||||
/**
|
||||
* 根据值的类型和属性将其转换为字符串
|
||||
* 1. 对于带有 __varInputVal__ 属性的对象,根据该属性决定是否添加引号
|
||||
* 2. 对于普通字符串,自动添加引号
|
||||
* 3. 对于其他类型(数字、布尔等),直接转换
|
||||
* @param {Object|string|number|boolean} argv 要转换的值
|
||||
* @returns {string} 转换后的字符串
|
||||
* 处理带有 __varInputVal__ 属性的对象
|
||||
* @param {Object} argv 要处理的对象
|
||||
* @returns {string} 处理后的字符串
|
||||
*/
|
||||
export const stringifyWithType = (argv) => {
|
||||
// 处理带有类型标记的对象
|
||||
if (typeof argv === "object" && argv.hasOwnProperty("__varInputVal__")) {
|
||||
return argv.isString ? `"${argv.value}"` : argv.value;
|
||||
}
|
||||
|
||||
// 处理普通字符串
|
||||
if (typeof argv === "string") {
|
||||
return `"${argv}"`;
|
||||
}
|
||||
|
||||
// 处理其他类型
|
||||
return argv;
|
||||
const stringifyVarInputVal = (argv) => {
|
||||
return argv.isString ? `"${argv.value}"` : argv.value;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -51,9 +37,9 @@ const removeEmptyValues = (obj) => {
|
||||
const processObject = (obj, parentPath = "") => {
|
||||
// 移除空值
|
||||
obj = removeEmptyValues(obj);
|
||||
// 如果是 VariableInput 的输出,直接用 stringifyWithType 处理
|
||||
// 如果是 VariableInput 的输出,直接用 stringifyVarInputVal 处理
|
||||
if (obj?.hasOwnProperty("__varInputVal__")) {
|
||||
return stringifyWithType(obj);
|
||||
return stringifyVarInputVal(obj);
|
||||
}
|
||||
|
||||
let result = "{\n";
|
||||
@ -64,16 +50,18 @@ const processObject = (obj, parentPath = "") => {
|
||||
|
||||
// 处理对象类型
|
||||
if (value && typeof value === "object") {
|
||||
// 如果是 VariableInput 的输出,直接用 stringifyWithType 处理
|
||||
// 如果是 VariableInput 的输出,直接用 stringifyVarInputVal 处理
|
||||
if (value.hasOwnProperty("__varInputVal__")) {
|
||||
valueStr = stringifyWithType(value);
|
||||
valueStr = stringifyVarInputVal(value);
|
||||
} else {
|
||||
valueStr = processObject(value, parentPath + " ");
|
||||
}
|
||||
}
|
||||
// 处理其他类型
|
||||
else {
|
||||
valueStr = stringifyWithType(value);
|
||||
else if (value && typeof value === "string") {
|
||||
valueStr = `"${value}"`;
|
||||
} else {
|
||||
valueStr = value;
|
||||
}
|
||||
|
||||
// 添加缩进
|
||||
@ -94,13 +82,13 @@ const processObject = (obj, parentPath = "") => {
|
||||
* @param {Object} jsonObj 要格式化的对象
|
||||
* @returns {string} 格式化后的JSON字符串
|
||||
*/
|
||||
export const stringifyObject = (jsonObj) => {
|
||||
const stringifyObject = (jsonObj) => {
|
||||
if (jsonObj?.hasOwnProperty("__varInputVal__")) {
|
||||
return stringifyVarInputVal(jsonObj);
|
||||
}
|
||||
if (jsonObj instanceof Array) {
|
||||
return `[${jsonObj.map((item) => stringifyObject(item)).join(",")}]`;
|
||||
}
|
||||
if (jsonObj?.hasOwnProperty("__varInputVal__")) {
|
||||
return stringifyWithType(jsonObj);
|
||||
}
|
||||
try {
|
||||
return processObject(jsonObj);
|
||||
} 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对象
|
||||
* @param {string} str 要解析的字符串
|
||||
|
@ -4,14 +4,14 @@
|
||||
* @param {Array} selectedActions 已选择的操作列表
|
||||
* @returns {string} 生成的代码
|
||||
*/
|
||||
import { stringifyObject, stringifyWithType } from "./formatString";
|
||||
import { stringifyArgv } from "./formatString";
|
||||
|
||||
// 生成 goto 参数字符串
|
||||
function generateGotoArgs(goto) {
|
||||
const args = [];
|
||||
|
||||
// URL
|
||||
const urlStr = stringifyWithType(goto.url);
|
||||
const urlStr = stringifyArgv(goto.url);
|
||||
args.push(urlStr);
|
||||
|
||||
// Headers
|
||||
@ -24,7 +24,7 @@ function generateGotoArgs(goto) {
|
||||
headers.userAgent = goto.headers.userAgent;
|
||||
}
|
||||
console.log("Headers:", JSON.stringify(headers, null, 2));
|
||||
args.push(stringifyObject(headers, true));
|
||||
args.push(stringifyArgv(headers, true));
|
||||
}
|
||||
|
||||
// Timeout
|
||||
@ -121,7 +121,7 @@ function generateRunArgs(run) {
|
||||
if (run.proxy) options.proxy = run.proxy;
|
||||
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 "mouseup":
|
||||
case "focus":
|
||||
result = stringifyWithType(config.selector);
|
||||
result = stringifyArgv(config.selector);
|
||||
break;
|
||||
case "css":
|
||||
case "paste":
|
||||
result = stringifyWithType(config.value);
|
||||
result = stringifyArgv(config.value);
|
||||
break;
|
||||
case "press":
|
||||
result = generatePressArgs(config);
|
||||
@ -163,7 +163,7 @@ function generateActionArgs(action, config) {
|
||||
break;
|
||||
case "cookies":
|
||||
case "removeCookies":
|
||||
result = stringifyWithType(config.name);
|
||||
result = stringifyArgv(config.name);
|
||||
break;
|
||||
case "setCookies":
|
||||
result = generateSetCookiesArgs(config);
|
||||
@ -190,7 +190,7 @@ function generateActionArgs(action, config) {
|
||||
result = generateDownloadArgs(config);
|
||||
break;
|
||||
case "devTools":
|
||||
result = stringifyWithType(config.mode);
|
||||
result = stringifyArgv(config.mode);
|
||||
break;
|
||||
default:
|
||||
result = "";
|
||||
@ -208,7 +208,7 @@ function generateActionArgs(action, config) {
|
||||
function generateWaitArgs(config) {
|
||||
switch (config.type) {
|
||||
case "selector":
|
||||
return stringifyWithType(config.selector);
|
||||
return stringifyArgv(config.selector);
|
||||
case "function":
|
||||
return config.function;
|
||||
case "time":
|
||||
@ -220,7 +220,7 @@ function generateWaitArgs(config) {
|
||||
|
||||
// 生成 press 参数字符串
|
||||
function generatePressArgs(config) {
|
||||
const args = [stringifyWithType(config.key)];
|
||||
const args = [stringifyArgv(config.key)];
|
||||
if (config.modifiers?.length) {
|
||||
args.push(JSON.stringify(config.modifiers));
|
||||
}
|
||||
@ -231,12 +231,12 @@ function generatePressArgs(config) {
|
||||
function generateScreenshotArgs(config) {
|
||||
const args = [];
|
||||
if (config.rect) {
|
||||
args.push(stringifyObject(config.rect));
|
||||
args.push(stringifyArgv(config.rect));
|
||||
} else if (config.selector) {
|
||||
args.push(stringifyWithType(config.selector));
|
||||
args.push(stringifyArgv(config.selector));
|
||||
}
|
||||
if (config.savePath) {
|
||||
args.push(stringifyWithType(config.savePath));
|
||||
args.push(stringifyArgv(config.savePath));
|
||||
}
|
||||
return args.join(", ");
|
||||
}
|
||||
@ -245,10 +245,10 @@ function generateScreenshotArgs(config) {
|
||||
function generatePdfArgs(config) {
|
||||
const args = [];
|
||||
if (config.savePath) {
|
||||
args.push(stringifyWithType(config.savePath));
|
||||
args.push(stringifyArgv(config.savePath));
|
||||
}
|
||||
if (config.options) {
|
||||
args.push(stringifyObject(config.options));
|
||||
args.push(stringifyArgv(config.options));
|
||||
}
|
||||
return args.join(", ");
|
||||
}
|
||||
@ -256,26 +256,26 @@ function generatePdfArgs(config) {
|
||||
// 生成 device 参数字符串
|
||||
function generateDeviceArgs(config) {
|
||||
if (config.type === "preset") {
|
||||
return stringifyWithType(config.deviceName);
|
||||
return stringifyArgv(config.deviceName);
|
||||
} else {
|
||||
const options = {};
|
||||
if (config.size) options.size = config.size;
|
||||
if (config.useragent) options.userAgent = config.useragent;
|
||||
return stringifyObject(options);
|
||||
return stringifyArgv(options);
|
||||
}
|
||||
}
|
||||
|
||||
// 生成 setCookies 参数字符串
|
||||
function generateSetCookiesArgs(config) {
|
||||
if (!config.items?.length) return "[]";
|
||||
return stringifyObject(config.items);
|
||||
return stringifyArgv(config.items);
|
||||
}
|
||||
|
||||
// 生成 evaluate 参数字符串
|
||||
function generateEvaluateArgs(config) {
|
||||
const args = [config.function];
|
||||
if (config.args?.length) {
|
||||
args.push(...config.args.map(stringifyWithType));
|
||||
args.push(...config.args.map(stringifyArgv));
|
||||
}
|
||||
return args.join(", ");
|
||||
}
|
||||
@ -285,35 +285,35 @@ function generateWhenArgs(config) {
|
||||
if (config.type === "function") {
|
||||
return config.function;
|
||||
} else {
|
||||
return stringifyWithType(config.selector);
|
||||
return stringifyArgv(config.selector);
|
||||
}
|
||||
}
|
||||
|
||||
// 生成 file 参数字符串
|
||||
function generateFileArgs(config) {
|
||||
const args = [stringifyWithType(config.selector)];
|
||||
const args = [stringifyArgv(config.selector)];
|
||||
if (config.files) {
|
||||
args.push(stringifyObject(config.files));
|
||||
args.push(stringifyArgv(config.files));
|
||||
}
|
||||
return args.join(", ");
|
||||
}
|
||||
|
||||
// 生成 value 参数字符串
|
||||
function generateValueArgs(config) {
|
||||
return `${stringifyWithType(config.selector)}, ${stringifyWithType(
|
||||
return `${stringifyArgv(config.selector)}, ${stringifyArgv(
|
||||
config.value
|
||||
)}`;
|
||||
}
|
||||
|
||||
// 生成 check 参数字符串
|
||||
function generateCheckArgs(config) {
|
||||
return `${stringifyWithType(config.selector)}, ${config.checked}`;
|
||||
return `${stringifyArgv(config.selector)}, ${config.checked}`;
|
||||
}
|
||||
|
||||
// 生成 scroll 参数字符串
|
||||
function generateScrollArgs(config) {
|
||||
if (config.type === "element") {
|
||||
return stringifyWithType(config.selector);
|
||||
return stringifyArgv(config.selector);
|
||||
} else {
|
||||
if (config.x !== undefined) {
|
||||
return `${config.x}, ${config.y}`;
|
||||
@ -325,9 +325,9 @@ function generateScrollArgs(config) {
|
||||
|
||||
// 生成 download 参数字符串
|
||||
function generateDownloadArgs(config) {
|
||||
const args = [stringifyWithType(config.url)];
|
||||
const args = [stringifyArgv(config.url)];
|
||||
if (config.savePath) {
|
||||
args.push(stringifyWithType(config.savePath));
|
||||
args.push(stringifyArgv(config.savePath));
|
||||
}
|
||||
return args.join(", ");
|
||||
}
|
||||
|
@ -54,10 +54,8 @@
|
||||
"formatString.js": {
|
||||
"描述": "处理字符串格式化的工具函数,主要用于处理对象序列化和类型转换",
|
||||
"主要功能": {
|
||||
"stringifyWithType": "根据值的类型和属性将其转换为字符串,支持带类型标记的对象",
|
||||
"removeEmptyValues": "递归移除对象中的空值",
|
||||
"processObject": "递归处理对象的值并格式化成字符串",
|
||||
"stringifyObject": "格式化对象为JSON字符串,根据值的类型决定是否添加引号"
|
||||
"parseFunction": "解析函数字符串,返回函数名和参数",
|
||||
"stringifyArgv": "格式化参数为字符串,根据值的类型决定是否添加引号"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user