Browse Source

fix: can't call function in conditions block

Ahmad Kholid 3 years ago
parent
commit
884f1925d8
2 changed files with 35 additions and 35 deletions
  1. 1 32
      src/utils/reference-data/index.js
  2. 34 3
      src/utils/reference-data/mustache-replacer.js

+ 1 - 32
src/utils/reference-data/index.js

@@ -1,40 +1,9 @@
-import dayjs from '@/lib/dayjs';
 import { objectHasKey } from '@/utils/helper';
 import mustacheReplacer from './mustache-replacer';
 
-/* eslint-disable prefer-destructuring */
-export const funcs = {
-  date(...args) {
-    let date = new Date();
-    let dateFormat = 'DD-MM-YYYY';
-
-    if (args.length === 1) {
-      dateFormat = args[0];
-    } else if (args.length >= 2) {
-      date = new Date(args[0]);
-      dateFormat = args[1];
-    }
-
-    /* eslint-disable-next-line */
-    const isValidDate = date instanceof Date && !isNaN(date);
-    const dayjsDate = dayjs(isValidDate ? date : Date.now());
-
-    const result =
-      dateFormat === 'relative'
-        ? dayjsDate.fromNow()
-        : dayjsDate.format(dateFormat);
-
-    return result;
-  },
-  randint(min = 0, max = 100) {
-    return Math.round(Math.random() * (+max - +min) + +min);
-  },
-};
-
-export default function ({ block, refKeys, data: refData }) {
+export default function ({ block, refKeys, data }) {
   if (!refKeys || refKeys.length === 0) return block;
 
-  const data = { ...refData, funcs };
   const copyBlock = JSON.parse(JSON.stringify(block));
 
   refKeys.forEach((blockDataKey) => {

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

@@ -1,4 +1,5 @@
 import objectPath from 'object-path';
+import dayjs from '@/lib/dayjs';
 
 const refKeys = {
   table: 'table',
@@ -6,6 +7,35 @@ const refKeys = {
   dataColumns: 'table',
 };
 
+/* eslint-disable prefer-destructuring */
+export const functions = {
+  date(...args) {
+    let date = new Date();
+    let dateFormat = 'DD-MM-YYYY';
+
+    if (args.length === 1) {
+      dateFormat = args[0];
+    } else if (args.length >= 2) {
+      date = new Date(args[0]);
+      dateFormat = args[1];
+    }
+
+    /* eslint-disable-next-line */
+    const isValidDate = date instanceof Date && !isNaN(date);
+    const dayjsDate = dayjs(isValidDate ? date : Date.now());
+
+    const result =
+      dateFormat === 'relative'
+        ? dayjsDate.fromNow()
+        : dayjsDate.format(dateFormat);
+
+    return result;
+  },
+  randint(min = 0, max = 100) {
+    return Math.round(Math.random() * (+max - +min) + +min);
+  },
+};
+
 export function extractStrFunction(str) {
   const extractedStr = /^\$\s*(\w+)\s*\((.*)\)/.exec(str.trim());
 
@@ -69,8 +99,8 @@ function replacer(str, { regex, tagLen, modifyPath, data }) {
     let result = '';
     const funcRef = extractStrFunction(key);
 
-    if (funcRef && data.funcs[funcRef.name]) {
-      result = data.funcs[funcRef.name]?.apply(
+    if (funcRef && data.functions[funcRef.name]) {
+      result = data.functions[funcRef.name]?.apply(
         { refData: data },
         funcRef.params
       );
@@ -84,9 +114,10 @@ function replacer(str, { regex, tagLen, modifyPath, data }) {
   });
 }
 
-export default function (str, data) {
+export default function (str, refData) {
   if (!str || typeof str !== 'string') return '';
 
+  const data = { ...refData, functions };
   const replacedStr = replacer(str, {
     data,
     tagLen: 2,