From 84140f4e4f6d6d631285b63135344c7eee0b160f Mon Sep 17 00:00:00 2001 From: InspectorCaracal <51038201+InspectorCaracal@users.noreply.github.com> Date: Wed, 9 Oct 2024 13:49:03 -0600 Subject: [PATCH 1/3] add `/sort` slash command --- public/scripts/variables.js | 71 +++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/public/scripts/variables.js b/public/scripts/variables.js index c768c4f5e..343af9b16 100644 --- a/public/scripts/variables.js +++ b/public/scripts/variables.js @@ -836,6 +836,32 @@ function randValuesCallback(from, to, args) { return value; } +function sortArrayObjectCallback(args, value) { + let parsedValue; + if (typeof value == 'string') { + try { + parsedValue = JSON.parse(value); + } catch { + // return the original input if it was invalid + return value; + } + } else { + parsedValue = value + } + if (Array.isArray(parsedValue)) { + // always sort lists by value + parsedValue.sort() + } else if (typeof parsedValue == 'object') { + let keysort = args.keysort; + if (isFalseBoolean(keysort)) { + parsedValue = Object.keys(parsedValue).sort(function(a,b){return parsedValue[a]-parsedValue[b]}); + } else { + parsedValue = Object.keys(parsedValue).sort(); + } + } + return JSON.stringify(parsedValue); +} + /** * Declare a new variable in the current scope. * @param {NamedArguments} args Named arguments. @@ -2109,6 +2135,51 @@ export function registerVariableCommands() { `, })); + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'sort', + callback: sortArrayObjectCallback, + returns: 'the sorted list or dictionary keys', + namedArgumentList: [ + SlashCommandNamedArgument.fromProps({ name: 'keysort', + description: 'whether to sort by key or value; ignored for lists', + typeList: [ARGUMENT_TYPE.BOOLEAN], + enumList: ['true', 'false'], + defaultValue: 'true', + }), + ], + unnamedArgumentList: [ + SlashCommandArgument.fromProps({ + description: 'value', + typeList: [ARGUMENT_TYPE.STRING, ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.LIST, ARGUMENT_TYPE.DICTIONARY], + isRequired: true, + forceEnum: false, + }), + ], + helpString: ` +
+ Sorts a list or dictionary in ascending order and passes the result down the pipe. + +
+
+ Examples: + +
+ `, + })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'rand', callback: (args, value) => String(randValuesCallback(Number(args.from ?? 0), Number(args.to ?? (value ? value : 1)), args)), From 7a598a439a9393f8f8f1054b562492f3d8d5f187 Mon Sep 17 00:00:00 2001 From: Cal Date: Wed, 9 Oct 2024 15:09:30 -0600 Subject: [PATCH 2/3] switch to custom sort comparitor --- public/scripts/variables.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/public/scripts/variables.js b/public/scripts/variables.js index 343af9b16..c971ef830 100644 --- a/public/scripts/variables.js +++ b/public/scripts/variables.js @@ -836,6 +836,14 @@ function randValuesCallback(from, to, args) { return value; } +function customSortComparitor(a, b) { + if (typeof a != typeof b) { + a = typeof a; + b = typeof b; + } + return a > b ? 1 : a < b ? -1 : 0; +} + function sortArrayObjectCallback(args, value) { let parsedValue; if (typeof value == 'string') { @@ -850,13 +858,13 @@ function sortArrayObjectCallback(args, value) { } if (Array.isArray(parsedValue)) { // always sort lists by value - parsedValue.sort() + parsedValue.sort(customSortComparitor) } else if (typeof parsedValue == 'object') { let keysort = args.keysort; if (isFalseBoolean(keysort)) { - parsedValue = Object.keys(parsedValue).sort(function(a,b){return parsedValue[a]-parsedValue[b]}); + parsedValue = Object.keys(parsedValue).sort(function(a,b){return customSortComparitor(parsedValue[a], parsedValue[b])}); } else { - parsedValue = Object.keys(parsedValue).sort(); + parsedValue = Object.keys(parsedValue).sort(customSortComparitor); } } return JSON.stringify(parsedValue); From 8333f44e2b5566acc9db5d1e8754ea6e76627ffc Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 10 Oct 2024 00:41:43 +0300 Subject: [PATCH 3/3] [chore] Reformat callback --- public/scripts/variables.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/public/scripts/variables.js b/public/scripts/variables.js index c971ef830..f2c340541 100644 --- a/public/scripts/variables.js +++ b/public/scripts/variables.js @@ -849,25 +849,25 @@ function sortArrayObjectCallback(args, value) { if (typeof value == 'string') { try { parsedValue = JSON.parse(value); - } catch { - // return the original input if it was invalid - return value; + } catch { + // return the original input if it was invalid + return value; } } else { - parsedValue = value + parsedValue = value; } if (Array.isArray(parsedValue)) { // always sort lists by value - parsedValue.sort(customSortComparitor) + parsedValue.sort(customSortComparitor); } else if (typeof parsedValue == 'object') { let keysort = args.keysort; if (isFalseBoolean(keysort)) { - parsedValue = Object.keys(parsedValue).sort(function(a,b){return customSortComparitor(parsedValue[a], parsedValue[b])}); + parsedValue = Object.keys(parsedValue).sort(function (a, b) { return customSortComparitor(parsedValue[a], parsedValue[b]); }); } else { parsedValue = Object.keys(parsedValue).sort(customSortComparitor); } } - return JSON.stringify(parsedValue); + return JSON.stringify(parsedValue); } /**