Browse Source

fix: function parameter extractor

Ahmad Kholid 3 years ago
parent
commit
5642fe8045
2 changed files with 12 additions and 8 deletions
  1. 3 5
      src/utils/reference-data/index.js
  2. 9 3
      src/utils/reference-data/mustache-replacer.js

+ 3 - 5
src/utils/reference-data/index.js

@@ -3,19 +3,17 @@ 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';
 
-    const getDateFormat = (value) =>
-      value ? value?.replace(/['"]/g, '') : dateFormat;
-
     if (args.length === 1) {
-      dateFormat = getDateFormat(args[0]);
+      dateFormat = args[0];
     } else if (args.length >= 2) {
       date = new Date(args[0]);
-      dateFormat = getDateFormat(args[1]);
+      dateFormat = args[1];
     }
 
     /* eslint-disable-next-line */

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

@@ -7,15 +7,21 @@ export function extractStrFunction(str) {
 
   if (!extractedStr) return null;
 
+  const { 1: name, 2: funcParams } = extractedStr;
+
+  const params = funcParams
+    .split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/)
+    .map((param) => param?.trim().replace(/^['"]|['"]$/g, '') || '');
+
   return {
-    name: extractedStr[1],
-    params: extractedStr[2].split(','),
+    name,
+    params,
   };
 }
 
 export default function ({ str, data, block }) {
   const replacedStr = replaceMustache(str, (match) => {
-    const key = match.slice(2, -2).replace(/\s/g, '');
+    const key = match.slice(2, -2).trim();
 
     if (!key) return '';