Kaynağa Gözat

feat: add $last property to table

Ahmad Kholid 3 yıl önce
ebeveyn
işleme
b26c268aa3

+ 1 - 1
src/newtab/pages/Workflows.vue

@@ -369,7 +369,7 @@ function addHostWorkflow() {
         });
 
         if (!store.state.user && length >= 3) {
-          toast.error(r('message.rateExceeded'));
+          toast.error(t('message.rateExceeded'));
           return false;
         }
 

+ 0 - 36
src/utils/reference-data/key-parser.js

@@ -1,36 +0,0 @@
-const refKeys = {
-  table: 'table',
-  dataColumn: 'table',
-  dataColumns: 'table',
-};
-
-export default function (key) {
-  let [dataKey, path] = key.split(/@(.+)/);
-
-  dataKey = refKeys[dataKey] ?? dataKey;
-
-  if (!path) return { dataKey, path: '' };
-
-  if (dataKey !== 'table') {
-    if (dataKey === 'loopData' && !path.endsWith('.$index')) {
-      const pathArr = path.split('.');
-      pathArr.splice(1, 0, 'data');
-
-      path = pathArr.join('.');
-    }
-
-    return { dataKey, path };
-  }
-
-  const pathArr = path.split('.');
-
-  if (pathArr.length === 1) {
-    path = `0.${pathArr[0]}`;
-  } else if (typeof +pathArr[0] !== 'number' || Number.isNaN(+pathArr[0])) {
-    path = `0.${pathArr.join('.')}`;
-  }
-
-  if (path.endsWith('.')) path = path.slice(0, -1);
-
-  return { dataKey: 'table', path };
-}

+ 41 - 2
src/utils/reference-data/mustache-replacer.js

@@ -1,5 +1,10 @@
 import objectPath from 'object-path';
-import keyParser from './key-parser';
+
+const refKeys = {
+  table: 'table',
+  dataColumn: 'table',
+  dataColumns: 'table',
+};
 
 export function extractStrFunction(str) {
   const extractedStr = /^\$\s*(\w+)\s*\((.*)\)/.exec(str.trim());
@@ -17,6 +22,39 @@ export function extractStrFunction(str) {
   };
 }
 
+export function keyParser(key, data) {
+  let [dataKey, path] = key.split(/@(.+)/);
+
+  dataKey = refKeys[dataKey] ?? dataKey;
+
+  if (!path) return { dataKey, path: '' };
+
+  if (dataKey !== 'table') {
+    if (dataKey === 'loopData' && !path.endsWith('.$index')) {
+      const pathArr = path.split('.');
+      pathArr.splice(1, 0, 'data');
+
+      path = pathArr.join('.');
+    }
+
+    return { dataKey, path };
+  }
+
+  const [firstPath, restPath] = path.split(/\.(.+)/);
+
+  if (firstPath === '$last') {
+    const lastIndex = data.table.length - 1;
+
+    path = `${lastIndex}.${restPath}`;
+  } else if (!restPath) {
+    path = `0.${firstPath}`;
+  } else if (typeof +firstPath !== 'number' || Number.isNaN(+firstPath)) {
+    path = `0.${firstPath}.${restPath}`;
+  }
+
+  return { dataKey: 'table', path };
+}
+
 export default function (str, data) {
   if (!str || typeof str !== 'string') return '';
 
@@ -34,7 +72,8 @@ export default function (str, data) {
         funcRef.params
       );
     } else {
-      const { dataKey, path } = keyParser(key);
+      const { dataKey, path } = keyParser(key, data);
+
       result = objectPath.get(data[dataKey], path) ?? match;
     }