Browse Source

refactor: reference data

Ahmad Kholid 3 years ago
parent
commit
0c4fbcbeb7

+ 12 - 15
src/utils/reference-data/index.js

@@ -1,4 +1,3 @@
-import { set as setObjectPath } from 'object-path-immutable';
 import dayjs from '@/lib/dayjs';
 import { objectHasKey } from '@/utils/helper';
 import mustacheReplacer from './mustache-replacer';
@@ -35,24 +34,22 @@ export const funcs = {
 export default function ({ block, refKeys, data: refData }) {
   if (!refKeys || refKeys.length === 0) return block;
 
-  let replacedBlock = { ...block };
-  const data = Object.assign(refData, { funcs });
+  const data = { ...refData, funcs };
+  const copyBlock = JSON.parse(JSON.stringify(block));
 
   refKeys.forEach((blockDataKey) => {
     if (!objectHasKey(block.data, blockDataKey)) return;
 
-    const newDataValue = mustacheReplacer({
-      data,
-      block,
-      str: replacedBlock.data[blockDataKey],
-    });
-
-    replacedBlock = setObjectPath(
-      replacedBlock,
-      `data.${blockDataKey}`,
-      newDataValue
-    );
+    const currentData = copyBlock.data[blockDataKey];
+
+    if (Array.isArray(currentData)) {
+      currentData.forEach((str, index) => {
+        currentData[index] = mustacheReplacer(str, data);
+      });
+    } else {
+      copyBlock.data[blockDataKey] = mustacheReplacer(currentData, data);
+    }
   });
 
-  return replacedBlock;
+  return copyBlock;
 }

+ 10 - 12
src/utils/reference-data/key-parser.js

@@ -1,15 +1,14 @@
-import { objectHasKey } from '../helper';
-
 const refKeys = {
   dataColumn: 'dataColumns',
   dataColumns: 'dataColumns',
 };
 
 export default function (key) {
-  /* eslint-disable-next-line */
-  let [dataKey, path] = key.split('@');
+  let [dataKey, path] = key.split(/@(.+)/);
+
+  dataKey = refKeys[dataKey] ?? dataKey;
 
-  dataKey = objectHasKey(refKeys, dataKey) ? refKeys[dataKey] : dataKey;
+  if (!path) return { dataKey, path: '' };
 
   if (dataKey !== 'dataColumns') {
     if (dataKey === 'loopData' && !path.endsWith('.$index')) {
@@ -19,19 +18,18 @@ export default function (key) {
       path = pathArr.join('.');
     }
 
-    return { dataKey, path: path || '' };
+    return { dataKey, path };
   }
 
-  const pathArr = path?.split('.') ?? [];
-  let dataPath = path;
+  const pathArr = path.split('.');
 
   if (pathArr.length === 1) {
-    dataPath = `0.${pathArr[0]}`;
+    path = `0.${pathArr[0]}`;
   } else if (typeof +pathArr[0] !== 'number' || Number.isNaN(+pathArr[0])) {
-    dataPath = `0.${pathArr.join('.')}`;
+    path = `0.${pathArr.join('.')}`;
   }
 
-  if (dataPath.endsWith('.')) dataPath = dataPath.slice(0, -1);
+  if (path.endsWith('.')) path = path.slice(0, -1);
 
-  return { dataKey: 'dataColumns', path: dataPath };
+  return { dataKey: 'dataColumns', path };
 }

+ 3 - 7
src/utils/reference-data/mustache-replacer.js

@@ -1,5 +1,4 @@
 import { get as getObjectPath } from 'object-path-immutable';
-import { replaceMustache } from '../helper';
 import keyParser from './key-parser';
 
 export function extractStrFunction(str) {
@@ -8,7 +7,6 @@ export function extractStrFunction(str) {
   if (!extractedStr) return null;
 
   const { 1: name, 2: funcParams } = extractedStr;
-
   const params = funcParams
     .split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/)
     .map((param) => param?.trim().replace(/^['"]|['"]$/g, '') || '');
@@ -19,8 +17,8 @@ export function extractStrFunction(str) {
   };
 }
 
-export default function ({ str, data, block }) {
-  const replacedStr = replaceMustache(str, (match) => {
+export default function (str, data) {
+  const replacedStr = str.replace(/\{\{(.*?)\}\}/g, (match) => {
     const key = match.slice(2, -2).trim();
 
     if (!key) return '';
@@ -37,9 +35,7 @@ export default function ({ str, data, block }) {
       const { dataKey, path } = keyParser(key);
       result = getObjectPath(data[dataKey], path) ?? match;
     }
-    if (block && block.name === 'webhook') {
-      return JSON.stringify(result);
-    }
+
     return typeof result === 'string' ? result : JSON.stringify(result);
   });