diff --git a/plugin/lib/quickcomposer/data/array.js b/plugin/lib/quickcomposer/data/array.js index 42a3c66..92e4e2e 100644 --- a/plugin/lib/quickcomposer/data/array.js +++ b/plugin/lib/quickcomposer/data/array.js @@ -81,8 +81,9 @@ const array = { }, // 数组排序 - sort: function (array, key, order = "asc") { + sort: function (array, order = "asc", key) { if (!Array.isArray(array)) return []; + if (order === "shuffle") return this.shuffle(array); return [...array].sort((a, b) => { const valueA = key ? a[key] : a; const valueB = key ? b[key] : b; @@ -97,63 +98,10 @@ const array = { }); }, - // 数组分组 - group: function (array, key) { - if (!Array.isArray(array) || !key) return {}; - return array.reduce((groups, item) => { - const value = key.split(".").reduce((obj, k) => obj?.[k], item); - if (value !== undefined) { - if (!groups[value]) { - groups[value] = []; - } - groups[value].push(item); - } - return groups; - }, {}); - }, - // 数组去重 - unique: function (array, key) { + unique: function (array) { if (!Array.isArray(array)) return []; - if (!key) { - return [...new Set(array)]; - } - const seen = new Set(); - return array.filter((item) => { - const value = key.split(".").reduce((obj, k) => obj?.[k], item); - if (value === undefined || seen.has(value)) { - return false; - } - seen.add(value); - return true; - }); - }, - - // 数组聚合 - aggregate: function (array, operation, key) { - if (!Array.isArray(array)) return null; - const values = key - ? array.map((item) => key.split(".").reduce((obj, k) => obj?.[k], item)) - : array; - const validNumbers = values.filter((v) => !isNaN(v)); - - switch (operation) { - case "sum": - return validNumbers.reduce((sum, val) => sum + val, 0); - case "avg": - return validNumbers.length - ? validNumbers.reduce((sum, val) => sum + val, 0) / - validNumbers.length - : 0; - case "max": - return validNumbers.length ? Math.max(...validNumbers) : null; - case "min": - return validNumbers.length ? Math.min(...validNumbers) : null; - case "count": - return array.length; - default: - return null; - } + return [...new Set(array)]; }, // 数组切片 @@ -168,61 +116,6 @@ const array = { return array.flat(depth); }, - // 数组差集 - diff: function (array1, array2, key) { - if (!Array.isArray(array1) || !Array.isArray(array2)) return []; - if (!key) { - return array1.filter((item) => !array2.includes(item)); - } - const set2 = new Set( - array2.map((item) => key.split(".").reduce((obj, k) => obj?.[k], item)) - ); - return array1.filter( - (item) => !set2.has(key.split(".").reduce((obj, k) => obj?.[k], item)) - ); - }, - - // 数组交集 - intersect: function (array1, array2, key) { - if (!Array.isArray(array1) || !Array.isArray(array2)) return []; - if (!key) { - return array1.filter((item) => array2.includes(item)); - } - const set2 = new Set( - array2.map((item) => key.split(".").reduce((obj, k) => obj?.[k], item)) - ); - return array1.filter((item) => - set2.has(key.split(".").reduce((obj, k) => obj?.[k], item)) - ); - }, - - // 数组并集 - union: function (array1, array2, key) { - if (!Array.isArray(array1) || !Array.isArray(array2)) return []; - if (!key) { - return [...new Set([...array1, ...array2])]; - } - const seen = new Set(); - return [...array1, ...array2].filter((item) => { - const value = key.split(".").reduce((obj, k) => obj?.[k], item); - if (value === undefined || seen.has(value)) { - return false; - } - seen.add(value); - return true; - }); - }, - - // 数组分块 - chunk: function (array, size = 1) { - if (!Array.isArray(array) || size < 1) return []; - const chunks = []; - for (let i = 0; i < array.length; i += size) { - chunks.push(array.slice(i, i + size)); - } - return chunks; - }, - // 数组随机排序 shuffle: function (array) { if (!Array.isArray(array)) return []; @@ -233,6 +126,40 @@ const array = { } return result; }, + + // 数组添加元素 + push: function (array, element, index) { + if (!Array.isArray(array)) return []; + if (index === undefined) { + array.push(element); + } else { + if (typeof index !== "number" || index < 0 || index > array.length) { + throw new Error("位置参数错误"); + } + array.splice(index, 0, element); + } + return array; + }, + + // 数组删除元素 + splice: function (array, start, deleteCount) { + if (!Array.isArray(array)) return []; + array.splice(start, deleteCount); + return array; + }, + + // 数组连接 + join: function (array, separator) { + if (!Array.isArray(array)) return ""; + return array.join(separator); + }, + + // 数组设置元素 + set: function (array, index, value) { + if (!Array.isArray(array)) return []; + array[index] = value; + return array; + }, }; module.exports = array; diff --git a/plugin/lib/quickcomposer/data/string.js b/plugin/lib/quickcomposer/data/string.js index d285314..8ac4c44 100644 --- a/plugin/lib/quickcomposer/data/string.js +++ b/plugin/lib/quickcomposer/data/string.js @@ -93,11 +93,6 @@ const string = { return text.split(separator); }, - // 数组合并 - join: function (array, separator = ",") { - return Array.isArray(array) ? array.join(separator) : String(array); - }, - // 字符串重复 repeat: function (text, count = 1) { return text.repeat(Math.max(0, count)); diff --git a/src/js/composer/commands/dataCommands.js b/src/js/composer/commands/dataCommands.js index b8c7afc..544ffa9 100644 --- a/src/js/composer/commands/dataCommands.js +++ b/src/js/composer/commands/dataCommands.js @@ -226,31 +226,6 @@ export const dataCommands = { typeName: "字符串", }, }, - { - value: "quickcomposer.data.string.join", - label: "数组合并", - icon: "merge", - config: [ - { - label: "字符串数组", - component: "VariableInput", - icon: "view_list", - width: 8, - }, - { - label: "连接符", - component: "VariableInput", - icon: "space_bar", - width: 4, - defaultValue: newVarInputVal("str", ","), - }, - ], - outputs: { - label: "合并结果", - suggestName: "joinedString", - typeName: "字符串", - }, - }, { value: "quickcomposer.data.string.repeat", label: "字符串重复", @@ -909,197 +884,158 @@ export const dataCommands = { ], }, { - value: "quickcomposer.data.array.filter", + value: "Array.from", label: "数组处理", icon: "view_list", subCommands: [ { - value: "quickcomposer.data.array.filter", - label: "数组过滤", - icon: "filter_alt", + 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", - width: 12, defaultValue: newVarInputVal("var"), disableToggleType: true, - placeholder: - '[{"id":1,"name":"张三","age":20},{"id":2,"name":"李四","age":18}]', + width: 4, + placeholder: "[1,2,3]", }, { - label: "过滤条件", + label: "要添加的元素", component: "VariableInput", - icon: "code", - width: 12, - placeholder: "age > 18", + icon: "input", + width: 4, + placeholder: "4", + }, + { + label: "位置(可选)", + component: "VariableInput", + icon: "first_page", + disableToggleType: true, + defaultValue: newVarInputVal("var"), + width: 4, + placeholder: "留空则添加到末尾", }, ], }, { - value: "quickcomposer.data.array.map", - label: "数组映射", - icon: "transform", + value: "quickcomposer.data.array.splice", + label: "删除元素", + icon: "delete", config: [ { label: "原始数组", component: "VariableInput", icon: "view_list", width: 12, - placeholder: - '[{"id":1,"name":"张三","age":20},{"id":2,"name":"李四","age":18}]', + placeholder: "[1,2,3,4,5]", + defaultValue: newVarInputVal("var"), + disableToggleType: true, }, { - label: "转换函数", + label: "起始位置", component: "VariableInput", - icon: "code", - placeholder: "name", - width: 12, + 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.sort", - label: "数组排序", - icon: "sort", - config: [ - { - label: "原始数组", - component: "VariableInput", - icon: "view_list", - width: 12, - placeholder: - '[{"id":1,"name":"张三","age":20},{"id":2,"name":"李四","age":18}]', - }, - { - label: "排序字段", - component: "VariableInput", - icon: "key", - width: 6, - placeholder: "age", - }, - { - label: "排序方式", - component: "QSelect", - icon: "sort", - options: [ - { label: "升序", value: "asc" }, - { label: "降序", value: "desc" }, - ], - defaultValue: "asc", - width: 6, - }, - ], - }, - { - value: "quickcomposer.data.array.group", - label: "数组分组", - icon: "group_work", + value: "quickcomposer.data.array.join", + label: "连接数组(join)", + icon: "join_right", config: [ { label: "原始数组", component: "VariableInput", icon: "view_list", width: 8, - placeholder: - '[{"id":1,"name":"张三","dept":"技术"},{"id":2,"name":"李四","dept":"技术"}]', + placeholder: "[1,2,3,4,5]", + defaultValue: newVarInputVal("var"), + disableToggleType: true, }, { - label: "分组字段", + label: "连接符", component: "VariableInput", - icon: "key", + icon: "swap_horiz", width: 4, - placeholder: "dept", + defaultValue: newVarInputVal("str", ","), }, ], }, { - value: "quickcomposer.data.array.unique", - label: "数组去重", - icon: "filter_1", + value: "quickcomposer.data.array.set", + label: "设置元素", + icon: "edit", config: [ { label: "原始数组", component: "VariableInput", icon: "view_list", - width: 8, - placeholder: '[{"id":1,"dept":"技术"},{"id":2,"dept":"技术"}]', - }, - { - label: "去重字段", - component: "VariableInput", - icon: "key", + defaultValue: newVarInputVal("var"), + disableToggleType: true, width: 4, - placeholder: "dept", }, - ], - }, - { - value: "quickcomposer.data.array.find", - label: "查找元素", - icon: "search", - config: [ { - label: "原始数组", + label: "位置", component: "VariableInput", - icon: "view_list", - width: 12, - placeholder: '[{"id":1,"name":"张三"},{"id":2,"name":"李四"}]', + icon: "first_page", + disableToggleType: true, + defaultValue: newVarInputVal("var"), + width: 4, + placeholder: "1", }, { - label: "查找条件", + label: "值", component: "VariableInput", - icon: "code", - width: 12, - placeholder: "id === 1", - }, - ], - }, - { - value: "quickcomposer.data.array.aggregate", - label: "数组聚合", - icon: "functions", - config: [ - { - label: "原始数组", - component: "VariableInput", - icon: "view_list", - width: 12, - placeholder: '[{"id":1,"amount":100},{"id":2,"amount":200}]', - }, - { - label: "聚合操作", - component: "QSelect", - icon: "functions", - options: [ - { label: "求和", value: "sum" }, - { label: "平均值", value: "avg" }, - { label: "最大值", value: "max" }, - { label: "最小值", value: "min" }, - { label: "计数", value: "count" }, - ], - defaultValue: "sum", - width: 6, - }, - { - label: "聚合字段", - component: "VariableInput", - icon: "key", - width: 6, - placeholder: "amount", + icon: "input", + width: 4, + placeholder: "4", }, ], }, { value: "quickcomposer.data.array.slice", - label: "数组切片", + 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]", }, @@ -1123,15 +1059,88 @@ export const dataCommands = { }, ], }, + { + 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: "数组扁平化", + 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]]]", }, @@ -1146,123 +1155,54 @@ export const dataCommands = { ], }, { - value: "quickcomposer.data.array.diff", - label: "数组差集", - icon: "difference", - config: [ - { - label: "数组1", - component: "VariableInput", - icon: "view_list", - width: 6, - placeholder: "[1,2,3,4]", - }, - { - label: "数组2", - component: "VariableInput", - icon: "view_list", - width: 6, - placeholder: "[3,4,5,6]", - }, - { - label: "比较字段", - component: "VariableInput", - icon: "key", - width: 12, - placeholder: "对象数组时使用,如: id", - }, - ], - }, - { - value: "quickcomposer.data.array.intersect", - label: "数组交集", - icon: "join_full", - config: [ - { - label: "数组1", - component: "VariableInput", - icon: "view_list", - width: 6, - placeholder: "[1,2,3,4]", - }, - { - label: "数组2", - component: "VariableInput", - icon: "view_list", - width: 6, - placeholder: "[3,4,5,6]", - }, - { - label: "比较字段", - component: "VariableInput", - icon: "key", - width: 12, - placeholder: "对象数组时使用,如: id", - }, - ], - }, - { - value: "quickcomposer.data.array.union", - label: "数组并集", - icon: "join_inner", - config: [ - { - label: "数组1", - component: "VariableInput", - icon: "view_list", - width: 6, - placeholder: "[1,2,3,4]", - }, - { - label: "数组2", - component: "VariableInput", - icon: "view_list", - width: 6, - placeholder: "[3,4,5,6]", - }, - { - label: "比较字段", - component: "VariableInput", - icon: "key", - width: 12, - placeholder: "对象数组时使用,如: id", - }, - ], - }, - { - value: "quickcomposer.data.array.chunk", - label: "数组分块", - icon: "grid_view", + value: "quickcomposer.data.array.unique", + label: "去重", + icon: "filter_1", config: [ { label: "原始数组", component: "VariableInput", icon: "view_list", - width: 8, - placeholder: "[1,2,3,4,5,6]", - }, - { - label: "块大小", - component: "VariableInput", - icon: "straighten", - disableToggleType: true, defaultValue: newVarInputVal("var"), - width: 4, + disableToggleType: true, + width: 12, + placeholder: "[1,2,3,1,2]", }, ], }, { - value: "quickcomposer.data.array.shuffle", - label: "数组随机排序", - icon: "shuffle", + value: "quickcomposer.data.array.sort", + label: "排序", + icon: "sort", config: [ { label: "原始数组", component: "VariableInput", icon: "view_list", - width: 12, + 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", + }, ], }, ],