From 6bd5a0f664e6a36a23d2ce3a69c0a7d7eb890df8 Mon Sep 17 00:00:00 2001 From: Izzy Deane Date: Mon, 2 Sep 2024 12:19:37 -0500 Subject: [PATCH 1/2] Array slice variable --- .../variables/builtin/array/array-slice.ts | 35 +++++++++++++++++++ src/backend/variables/builtin/array/index.ts | 6 ++-- 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 src/backend/variables/builtin/array/array-slice.ts diff --git a/src/backend/variables/builtin/array/array-slice.ts b/src/backend/variables/builtin/array/array-slice.ts new file mode 100644 index 000000000..20579fc3d --- /dev/null +++ b/src/backend/variables/builtin/array/array-slice.ts @@ -0,0 +1,35 @@ +import { ReplaceVariable, Trigger } from "../../../../types/variables"; +import { OutputDataType, VariableCategory } from "../../../../shared/variable-constants"; + +const model : ReplaceVariable = { + definition: { + handle: "arraySlice", + description: "Returns a slice of an array", + usage: "arraySlice[array, start, end]", + categories: [VariableCategory.ADVANCED], + possibleDataOutput: [OutputDataType.TEXT] + }, + evaluator: ( + trigger: Trigger, + subject: string | unknown[], + start?: string, + end?: string + ) : unknown[] => { + if (typeof subject === 'string' || subject instanceof String) { + try { + subject = JSON.parse(`${subject}`); + } catch (ignore) { + return []; + } + } + if (!Array.isArray(subject)) { + return []; + } + return [...subject].slice( + start ? Number(start) : 0, + end ? Number(end) : subject.length + ); + } +}; + +export default model; \ No newline at end of file diff --git a/src/backend/variables/builtin/array/index.ts b/src/backend/variables/builtin/array/index.ts index 6841c0838..9ae88a571 100644 --- a/src/backend/variables/builtin/array/index.ts +++ b/src/backend/variables/builtin/array/index.ts @@ -1,5 +1,5 @@ import arrayAdd from './array-add'; -import arrayElemenet from './array-element'; +import arrayElement from './array-element'; import arrayFilter from './array-filter'; import arrayFindIndex from './array-find-index'; import arrayFind from './array-find'; @@ -11,6 +11,7 @@ import arrayRandomItem from './array-random-item'; import arrayRemove from './array-remove'; import arrayReverse from './array-reverse'; import arrayShuffle from './array-shuffle'; +import arraySlice from './array-slice'; // Deprecated import rawArrayAdd from './raw-array-add'; @@ -27,7 +28,7 @@ import rawArrayShuffle from './raw-array-shuffle'; export default [ arrayAdd, - arrayElemenet, + arrayElement, arrayFilter, arrayFindIndex, arrayFind, @@ -39,6 +40,7 @@ export default [ arrayRemove, arrayReverse, arrayShuffle, + arraySlice, // Deprecated rawArrayAdd, From 74d2960747cb4e39d0c50c4540a2f5cdcf89a235 Mon Sep 17 00:00:00 2001 From: Izzy Deane Date: Mon, 2 Sep 2024 12:56:24 -0500 Subject: [PATCH 2/2] Added some safety measures to the start and end parsing --- .../variables/builtin/array/array-slice.ts | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/backend/variables/builtin/array/array-slice.ts b/src/backend/variables/builtin/array/array-slice.ts index 20579fc3d..16c0915b9 100644 --- a/src/backend/variables/builtin/array/array-slice.ts +++ b/src/backend/variables/builtin/array/array-slice.ts @@ -12,8 +12,8 @@ const model : ReplaceVariable = { evaluator: ( trigger: Trigger, subject: string | unknown[], - start?: string, - end?: string + start?: string | number, + end?: string | number ) : unknown[] => { if (typeof subject === 'string' || subject instanceof String) { try { @@ -25,10 +25,18 @@ const model : ReplaceVariable = { if (!Array.isArray(subject)) { return []; } - return [...subject].slice( - start ? Number(start) : 0, - end ? Number(end) : subject.length - ); + + start = start ? Number(start) : 0; + if (Number.isNaN(start)) { + start = 0; + } + + end = end ? Number(end) : subject.length; + if (Number.isNaN(end)) { + end = subject.length; + } + + return [...subject].slice(start, end); } };