diff --git a/plugin/lib/quickcomposer/data/array.js b/plugin/lib/quickcomposer/data/array.js index 92e4e2e..0a1bdff 100644 --- a/plugin/lib/quickcomposer/data/array.js +++ b/plugin/lib/quickcomposer/data/array.js @@ -160,6 +160,12 @@ const array = { array[index] = value; return array; }, + + // 获取数组长度 + length: function (array) { + if (!Array.isArray(array)) return 0; + return array.length; + }, }; module.exports = array; diff --git a/plugin/lib/quickcomposer/data/index.js b/plugin/lib/quickcomposer/data/index.js index f99859c..671ccf0 100644 --- a/plugin/lib/quickcomposer/data/index.js +++ b/plugin/lib/quickcomposer/data/index.js @@ -3,6 +3,7 @@ const buffer = require("./buffer"); const zlib = require("./zlib"); const { htmlParser } = require("./htmlParser"); const array = require("./array"); +const object = require("./object"); const time = require("./time"); const { regexTransform } = require("./regexTransform"); @@ -12,6 +13,7 @@ module.exports = { buffer, zlib, array, + object, time, regexTransform, }; diff --git a/plugin/lib/quickcomposer/data/object.js b/plugin/lib/quickcomposer/data/object.js new file mode 100644 index 0000000..b1b1fef --- /dev/null +++ b/plugin/lib/quickcomposer/data/object.js @@ -0,0 +1,72 @@ +const object = { + // 获取对象属性值 + get: function (obj, key) { + if (!obj || typeof obj !== "object") return undefined; + return key.split(".").reduce((o, k) => o?.[k], obj); + }, + + // 设置对象属性值 + set: function (obj, key, value) { + if (!obj || typeof obj !== "object") return obj; + const keys = key.split("."); + const lastKey = keys.pop(); + const target = keys.reduce((o, k) => { + if (!o[k] || typeof o[k] !== "object") { + o[k] = {}; + } + return o[k]; + }, obj); + target[lastKey] = value; + return obj; + }, + + // 删除对象属性 + delete: function (obj, key) { + if (!obj || typeof obj !== "object") return obj; + const keys = key.split("."); + const lastKey = keys.pop(); + const target = keys.reduce((o, k) => o?.[k], obj); + if (target && typeof target === "object") { + delete target[lastKey]; + } + return obj; + }, + + // 合并对象 + merge: function (target, ...sources) { + if (!target || typeof target !== "object") return target; + return Object.assign(target, ...sources); + }, + + // 获取对象所有键 + keys: function (obj) { + if (!obj || typeof obj !== "object") return []; + return Object.keys(obj); + }, + + // 获取对象所有值 + values: function (obj) { + if (!obj || typeof obj !== "object") return []; + return Object.values(obj); + }, + + // 获取对象键值对 + entries: function (obj) { + if (!obj || typeof obj !== "object") return []; + return Object.entries(obj); + }, + + // 检查属性是否存在 + has: function (obj, key) { + if (!obj || typeof obj !== "object") return false; + return key.split(".").reduce((o, k) => o?.[k] !== undefined, true); + }, + + // 深拷贝对象 + clone: function (obj) { + if (!obj || typeof obj !== "object") return obj; + return JSON.parse(JSON.stringify(obj)); + }, +}; + +module.exports = object; diff --git a/src/js/composer/commands/dataCommands.js b/src/js/composer/commands/dataCommands.js index 544ffa9..e4ca27d 100644 --- a/src/js/composer/commands/dataCommands.js +++ b/src/js/composer/commands/dataCommands.js @@ -388,6 +388,651 @@ export const dataCommands = { }, ], }, + { + value: "Array.from", + label: "数组处理", + icon: "view_list", + subCommands: [ + { + value: "Array.from", + label: "创建数组", + config: [ + { + component: "ArrayEditor", + icon: "view_list", + width: 12, + }, + ], + outputs: [ + { + label: "创建的数组", + suggestName: "newArray", + }, + ], + }, + { + value: "quickcomposer.data.array.length", + label: "获取长度", + icon: "straighten", + config: [ + { + label: "数组", + component: "VariableInput", + icon: "view_list", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 12, + }, + ], + outputs: { + label: "数组长度", + suggestName: "arrayLength", + typeName: "数字", + }, + }, + { + value: "quickcomposer.data.array.push", + label: "添加元素", + icon: "add", + config: [ + { + label: "原始数组", + component: "VariableInput", + icon: "view_list", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 4, + placeholder: "[1,2,3]", + }, + { + label: "要添加的元素", + component: "VariableInput", + icon: "input", + width: 4, + placeholder: "4", + }, + { + label: "位置(可选)", + component: "VariableInput", + icon: "first_page", + disableToggleType: true, + defaultValue: newVarInputVal("var"), + width: 4, + placeholder: "留空则添加到末尾", + }, + ], + outputs: { + label: "添加后的数组", + suggestName: "addedArray", + typeName: "数组", + }, + }, + { + value: "quickcomposer.data.array.splice", + label: "删除元素", + icon: "delete", + config: [ + { + label: "原始数组", + component: "VariableInput", + icon: "view_list", + width: 12, + placeholder: "[1,2,3,4,5]", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + }, + { + label: "起始位置", + component: "VariableInput", + icon: "first_page", + disableToggleType: true, + defaultValue: newVarInputVal("var"), + width: 6, + placeholder: "1", + }, + { + label: "删除数量", + component: "VariableInput", + disableToggleType: true, + defaultValue: newVarInputVal("var"), + icon: "delete", + width: 6, + placeholder: "1", + }, + ], + outputs: { + label: "删除后的数组", + suggestName: "deletedArray", + typeName: "数组", + }, + }, + { + value: "quickcomposer.data.array.join", + label: "连接数组(join)", + icon: "join_right", + config: [ + { + label: "原始数组", + component: "VariableInput", + icon: "view_list", + width: 8, + placeholder: "[1,2,3,4,5]", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + }, + { + label: "连接符", + component: "VariableInput", + icon: "swap_horiz", + width: 4, + defaultValue: newVarInputVal("str", ","), + }, + ], + outputs: { + label: "连接后的数组", + suggestName: "joinedArray", + typeName: "数组", + }, + }, + { + value: "quickcomposer.data.array.set", + label: "设置元素", + icon: "edit", + config: [ + { + label: "原始数组", + component: "VariableInput", + icon: "view_list", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 4, + }, + { + label: "位置", + component: "VariableInput", + icon: "first_page", + disableToggleType: true, + defaultValue: newVarInputVal("var"), + width: 4, + placeholder: "1", + }, + { + label: "值", + component: "VariableInput", + icon: "input", + width: 4, + placeholder: "4", + }, + ], + outputs: { + label: "设置后的数组", + suggestName: "newArray", + typeName: "数组", + }, + }, + { + value: "quickcomposer.data.array.slice", + label: "切片(slice)", + icon: "content_cut", + config: [ + { + label: "原始数组", + component: "VariableInput", + icon: "view_list", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 12, + placeholder: "[1,2,3,4,5]", + }, + { + label: "起始位置", + component: "VariableInput", + icon: "first_page", + disableToggleType: true, + defaultValue: newVarInputVal("var"), + width: 6, + placeholder: "1", + }, + { + label: "结束位置", + component: "VariableInput", + icon: "last_page", + disableToggleType: true, + defaultValue: newVarInputVal("var"), + width: 6, + placeholder: "3", + }, + ], + outputs: { + label: "切片后的数组", + suggestName: "slicedArray", + typeName: "数组", + }, + }, + { + value: "quickcomposer.data.array.filter", + label: "过滤(filter)", + icon: "filter_alt", + config: [ + { + label: "原始数组", + component: "VariableInput", + icon: "view_list", + width: 12, + defaultValue: newVarInputVal("var"), + disableToggleType: true, + placeholder: + '[{"id":1,"name":"张三","age":20},{"id":2,"name":"李四","age":18}]', + }, + { + label: "过滤条件", + component: "VariableInput", + icon: "code", + width: 12, + placeholder: "age > 18", + }, + ], + outputs: { + label: "过滤后的数组", + suggestName: "filteredArray", + typeName: "数组", + }, + }, + { + value: "quickcomposer.data.array.map", + label: "映射(map)", + icon: "transform", + config: [ + { + label: "原始数组", + component: "VariableInput", + icon: "view_list", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 12, + placeholder: + '[{"id":1,"name":"张三","age":20},{"id":2,"name":"李四","age":18}]', + }, + { + label: "转换函数", + component: "VariableInput", + icon: "code", + placeholder: "name", + width: 12, + }, + ], + outputs: { + label: "映射后的数组", + suggestName: "mappedArray", + typeName: "数组", + }, + }, + { + value: "quickcomposer.data.array.find", + label: "查找元素(find)", + icon: "search", + config: [ + { + label: "原始数组", + component: "VariableInput", + icon: "view_list", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 12, + placeholder: '[{"id":1,"name":"张三"},{"id":2,"name":"李四"}]', + }, + { + label: "查找条件", + component: "VariableInput", + icon: "code", + width: 12, + placeholder: "id === 1", + }, + ], + outputs: { + label: "查找的元素", + suggestName: "foundElement", + typeName: "对象", + }, + }, + { + value: "quickcomposer.data.array.flatten", + label: "扁平化(flat)", + icon: "unfold_less", + config: [ + { + label: "原始数组", + component: "VariableInput", + icon: "view_list", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 8, + placeholder: "[[1,2],[3,4],[[5,6]]]", + }, + { + label: "扁平化深度", + component: "VariableInput", + icon: "format_indent_decrease", + disableToggleType: true, + defaultValue: newVarInputVal("var"), + width: 4, + }, + ], + outputs: { + label: "扁平化后的数组", + suggestName: "flattenedArray", + typeName: "数组", + }, + }, + { + value: "quickcomposer.data.array.unique", + label: "去重", + icon: "filter_1", + config: [ + { + label: "原始数组", + component: "VariableInput", + icon: "view_list", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 12, + placeholder: "[1,2,3,1,2]", + }, + ], + outputs: { + label: "去重后的数组", + suggestName: "uniqueArray", + typeName: "数组", + }, + }, + { + value: "quickcomposer.data.array.sort", + label: "排序", + icon: "sort", + config: [ + { + label: "原始数组", + component: "VariableInput", + icon: "view_list", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 4, + placeholder: "[1,2,3,4,5]", + }, + { + label: "排序方向", + component: "QSelect", + icon: "swap_vert", + width: 4, + options: [ + { label: "升序", value: "asc" }, + { label: "降序", value: "desc" }, + { label: "乱序", value: "shuffle" }, + ], + defaultValue: "asc", + }, + { + label: "排序字段(可选)", + component: "VariableInput", + icon: "key", + width: 4, + placeholder: "dept", + }, + ], + outputs: { + label: "排序后的数组", + suggestName: "sortedArray", + typeName: "数组", + }, + }, + ], + }, + { + value: "new Object", + label: "对象处理", + icon: "data_object", + subCommands: [ + { + value: "new Object", + label: "新建对象", + icon: "key", + config: [ + { + component: "DictEditor", + width: 12, + }, + ], + outputs: { + label: "新建的对象", + suggestName: "newObject", + typeName: "对象", + }, + }, + { + value: "quickcomposer.data.object.get", + label: "获取属性", + icon: "key", + config: [ + { + label: "原始对象", + component: "VariableInput", + icon: "data_object", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 8, + }, + { + label: "属性", + component: "VariableInput", + icon: "key", + width: 4, + placeholder: "name 或 user.name", + }, + ], + outputs: { + label: "获取的属性", + suggestName: "getProperty", + typeName: "字符串", + }, + }, + { + value: "quickcomposer.data.object.set", + label: "设置属性", + icon: "edit", + config: [ + { + label: "原始对象", + component: "VariableInput", + icon: "data_object", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 12, + }, + { + label: "属性", + component: "VariableInput", + icon: "key", + width: 6, + placeholder: "name 或 user.name", + }, + { + label: "值", + component: "VariableInput", + icon: "edit", + width: 6, + }, + ], + outputs: { + label: "设置后的对象", + suggestName: "newObject", + typeName: "对象", + }, + }, + { + value: "quickcomposer.data.object.delete", + label: "删除属性", + icon: "delete", + config: [ + { + label: "原始对象", + component: "VariableInput", + icon: "data_object", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 8, + }, + { + label: "属性", + component: "VariableInput", + icon: "key", + width: 4, + placeholder: "name 或 user.name", + }, + ], + outputs: { + label: "删除后的对象", + suggestName: "newObject", + typeName: "对象", + }, + }, + { + value: "quickcomposer.data.object.merge", + label: "合并对象", + icon: "merge", + config: [ + { + label: "目标对象", + component: "VariableInput", + icon: "data_object", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 12, + }, + { + label: "源对象列表", + component: "ArrayEditor", + icon: "data_object", + width: 12, + defaultValue: [newVarInputVal("var")], + defaultRowValue: newVarInputVal("var"), + disableToggleType: true, + }, + ], + outputs: { + label: "合并后的对象", + suggestName: "newObject", + typeName: "对象", + }, + }, + { + value: "quickcomposer.data.object.has", + label: "检查属性", + icon: "check_circle", + config: [ + { + label: "原始对象", + component: "VariableInput", + icon: "data_object", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 8, + }, + { + label: "属性", + component: "VariableInput", + icon: "key", + width: 4, + placeholder: "name 或 user.name", + }, + ], + outputs: { + label: "检查结果", + suggestName: "checkResult", + typeName: "布尔", + }, + }, + { + value: "quickcomposer.data.object.keys", + label: "获取所有键", + icon: "key", + config: [ + { + label: "原始对象", + component: "VariableInput", + icon: "data_object", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 12, + }, + ], + outputs: { + label: "获取的键", + suggestName: "objectKeys", + typeName: "数组", + }, + }, + { + value: "quickcomposer.data.object.values", + label: "获取所有值", + icon: "view_list", + config: [ + { + label: "原始对象", + component: "VariableInput", + icon: "data_object", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 12, + }, + ], + outputs: { + label: "获取的值", + suggestName: "objectValues", + typeName: "数组", + }, + }, + { + value: "quickcomposer.data.object.entries", + label: "获取键值对", + icon: "view_list", + config: [ + { + label: "原始对象", + component: "VariableInput", + icon: "data_object", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 12, + }, + ], + outputs: { + label: "获取的键值对", + suggestName: "objectEntries", + typeName: "数组", + }, + }, + { + value: "quickcomposer.data.object.clone", + label: "深拷贝对象", + icon: "content_copy", + config: [ + { + label: "原始对象", + component: "VariableInput", + icon: "data_object", + defaultValue: newVarInputVal("var"), + disableToggleType: true, + width: 12, + }, + ], + outputs: { + label: "深拷贝后的对象", + suggestName: "newObject", + typeName: "对象", + }, + }, + ], + }, { value: "quickcomposer.data.regexTransform", label: "正则提取/替换", @@ -883,330 +1528,6 @@ export const dataCommands = { }, ], }, - { - value: "Array.from", - label: "数组处理", - icon: "view_list", - subCommands: [ - { - value: "Array.from", - label: "创建数组", - config: [ - { - component: "ArrayEditor", - icon: "view_list", - width: 12, - }, - ], - outputs: [ - { - label: "创建的数组", - suggestName: "newArray", - }, - ], - }, - { - value: "quickcomposer.data.array.push", - label: "添加元素", - icon: "add", - config: [ - { - label: "原始数组", - component: "VariableInput", - icon: "view_list", - defaultValue: newVarInputVal("var"), - disableToggleType: true, - width: 4, - placeholder: "[1,2,3]", - }, - { - label: "要添加的元素", - component: "VariableInput", - icon: "input", - width: 4, - placeholder: "4", - }, - { - label: "位置(可选)", - component: "VariableInput", - icon: "first_page", - disableToggleType: true, - defaultValue: newVarInputVal("var"), - width: 4, - placeholder: "留空则添加到末尾", - }, - ], - }, - { - value: "quickcomposer.data.array.splice", - label: "删除元素", - icon: "delete", - config: [ - { - label: "原始数组", - component: "VariableInput", - icon: "view_list", - width: 12, - placeholder: "[1,2,3,4,5]", - defaultValue: newVarInputVal("var"), - disableToggleType: true, - }, - { - label: "起始位置", - component: "VariableInput", - icon: "first_page", - disableToggleType: true, - defaultValue: newVarInputVal("var"), - width: 6, - placeholder: "1", - }, - { - label: "删除数量", - component: "VariableInput", - disableToggleType: true, - defaultValue: newVarInputVal("var"), - icon: "delete", - width: 6, - placeholder: "1", - }, - ], - }, - { - value: "quickcomposer.data.array.join", - label: "连接数组(join)", - icon: "join_right", - config: [ - { - label: "原始数组", - component: "VariableInput", - icon: "view_list", - width: 8, - placeholder: "[1,2,3,4,5]", - defaultValue: newVarInputVal("var"), - disableToggleType: true, - }, - { - label: "连接符", - component: "VariableInput", - icon: "swap_horiz", - width: 4, - defaultValue: newVarInputVal("str", ","), - }, - ], - }, - { - value: "quickcomposer.data.array.set", - label: "设置元素", - icon: "edit", - config: [ - { - label: "原始数组", - component: "VariableInput", - icon: "view_list", - defaultValue: newVarInputVal("var"), - disableToggleType: true, - width: 4, - }, - { - label: "位置", - component: "VariableInput", - icon: "first_page", - disableToggleType: true, - defaultValue: newVarInputVal("var"), - width: 4, - placeholder: "1", - }, - { - label: "值", - component: "VariableInput", - icon: "input", - width: 4, - placeholder: "4", - }, - ], - }, - { - value: "quickcomposer.data.array.slice", - label: "切片(slice)", - icon: "content_cut", - config: [ - { - label: "原始数组", - component: "VariableInput", - icon: "view_list", - defaultValue: newVarInputVal("var"), - disableToggleType: true, - width: 12, - placeholder: "[1,2,3,4,5]", - }, - { - label: "起始位置", - component: "VariableInput", - icon: "first_page", - disableToggleType: true, - defaultValue: newVarInputVal("var"), - width: 6, - placeholder: "1", - }, - { - label: "结束位置", - component: "VariableInput", - icon: "last_page", - disableToggleType: true, - defaultValue: newVarInputVal("var"), - width: 6, - placeholder: "3", - }, - ], - }, - { - value: "quickcomposer.data.array.filter", - label: "过滤(filter)", - icon: "filter_alt", - config: [ - { - label: "原始数组", - component: "VariableInput", - icon: "view_list", - width: 12, - defaultValue: newVarInputVal("var"), - disableToggleType: true, - placeholder: - '[{"id":1,"name":"张三","age":20},{"id":2,"name":"李四","age":18}]', - }, - { - label: "过滤条件", - component: "VariableInput", - icon: "code", - width: 12, - placeholder: "age > 18", - }, - ], - }, - { - value: "quickcomposer.data.array.map", - label: "映射(map)", - icon: "transform", - config: [ - { - label: "原始数组", - component: "VariableInput", - icon: "view_list", - defaultValue: newVarInputVal("var"), - disableToggleType: true, - width: 12, - placeholder: - '[{"id":1,"name":"张三","age":20},{"id":2,"name":"李四","age":18}]', - }, - { - label: "转换函数", - component: "VariableInput", - icon: "code", - placeholder: "name", - width: 12, - }, - ], - }, - { - value: "quickcomposer.data.array.find", - label: "查找元素(find)", - icon: "search", - config: [ - { - label: "原始数组", - component: "VariableInput", - icon: "view_list", - defaultValue: newVarInputVal("var"), - disableToggleType: true, - width: 12, - placeholder: '[{"id":1,"name":"张三"},{"id":2,"name":"李四"}]', - }, - { - label: "查找条件", - component: "VariableInput", - icon: "code", - width: 12, - placeholder: "id === 1", - }, - ], - }, - { - value: "quickcomposer.data.array.flatten", - label: "扁平化(flat)", - icon: "unfold_less", - config: [ - { - label: "原始数组", - component: "VariableInput", - icon: "view_list", - defaultValue: newVarInputVal("var"), - disableToggleType: true, - width: 8, - placeholder: "[[1,2],[3,4],[[5,6]]]", - }, - { - label: "扁平化深度", - component: "VariableInput", - icon: "format_indent_decrease", - disableToggleType: true, - defaultValue: newVarInputVal("var"), - width: 4, - }, - ], - }, - { - value: "quickcomposer.data.array.unique", - label: "去重", - icon: "filter_1", - config: [ - { - label: "原始数组", - component: "VariableInput", - icon: "view_list", - defaultValue: newVarInputVal("var"), - disableToggleType: true, - width: 12, - placeholder: "[1,2,3,1,2]", - }, - ], - }, - { - value: "quickcomposer.data.array.sort", - label: "排序", - icon: "sort", - config: [ - { - label: "原始数组", - component: "VariableInput", - icon: "view_list", - defaultValue: newVarInputVal("var"), - disableToggleType: true, - width: 4, - placeholder: "[1,2,3,4,5]", - }, - { - label: "排序方向", - component: "QSelect", - icon: "swap_vert", - width: 4, - options: [ - { label: "升序", value: "asc" }, - { label: "降序", value: "desc" }, - { label: "乱序", value: "shuffle" }, - ], - defaultValue: "asc", - }, - { - label: "排序字段(可选)", - component: "VariableInput", - icon: "key", - width: 4, - placeholder: "dept", - }, - ], - }, - ], - }, { value: "quickcomposer.data.time.format", label: "时间处理",