|
@@ -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;
|
|
|
}
|
|
|
|