1 |
|
2 |
|
3 |
|
4 | import {
|
5 | isFun,
|
6 | isNum,
|
7 | isObj,
|
8 | isStr,
|
9 | isArray,
|
10 | isEmptyObj,
|
11 | typeCharge,
|
12 | objToArray,
|
13 | hasReg,
|
14 | typeTrans
|
15 | } from 'LIB/util';
|
16 |
|
17 | import {
|
18 | matchObject,
|
19 | matchArray
|
20 | } from 'MATCH/match';
|
21 |
|
22 | import config, { changeFilterDefaultObject } from 'MATCH/config';
|
23 |
|
24 | import stack from 'MATCH/stack';
|
25 |
|
26 | export const parse = function (
|
27 | str: any,
|
28 | key: any
|
29 | ) {
|
30 | let strArr;
|
31 | let i;
|
32 | let token;
|
33 | let parseResult = {
|
34 | };
|
35 | const objTokenReg = /\$\$\{\{(.*)\}\}/;
|
36 | const arrTokenReg = /\$\{(.*)\}/;
|
37 | const typeTokenReg = /\((boolean|Boolean|int|Int|string|float)\)\$/;
|
38 |
|
39 | if (isObj(str)) {
|
40 |
|
41 | parseResult['matchObject'] = str;
|
42 | return parseResult;
|
43 | }
|
44 |
|
45 | if (isArray(str)) {
|
46 |
|
47 | parseResult['matchArray'] = str;
|
48 | return parseResult;
|
49 | }
|
50 |
|
51 | if (isFun(str)) {
|
52 |
|
53 | parseResult['matchFun'] = str;
|
54 | return parseResult;
|
55 | }
|
56 |
|
57 | if (!isStr(str) ||
|
58 | isIgnore(key) ||
|
59 | (!objTokenReg.test(str) &&
|
60 | !arrTokenReg.test(str))
|
61 | ) {
|
62 |
|
63 | parseResult['noMatch'] = str;
|
64 | return parseResult;
|
65 | }
|
66 |
|
67 |
|
68 | strArr = str.split('||');
|
69 |
|
70 |
|
71 |
|
72 | for (let j = 0; j < strArr.length; j++) {
|
73 | let i = strArr[j];
|
74 | token = i.trim().match(typeTokenReg);
|
75 | if (hasReg(token)) {
|
76 |
|
77 | parseResult['matchType'] = token[1];
|
78 | }
|
79 |
|
80 | token = i.trim().match(objTokenReg);
|
81 |
|
82 | if (token && token.length && token.length >= 1) {
|
83 |
|
84 | parseResult['matchParam'] = token[1];
|
85 | continue;
|
86 | }
|
87 |
|
88 | token = i.trim().match(arrTokenReg);
|
89 |
|
90 | if (token && token.length && token.length >= 1) {
|
91 |
|
92 | parseResult['matchArrParam'] = token[1];
|
93 | continue;
|
94 | }
|
95 |
|
96 | if (!token) {
|
97 |
|
98 | parseResult['default'] = i.trim();
|
99 | continue;
|
100 | }
|
101 | }
|
102 |
|
103 | return parseResult;
|
104 | };
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | export const parseToData = function (
|
110 | exp: object, // parse 返回值
|
111 | data: object, // 映射的params数组
|
112 | that: object // 返回对象指针
|
113 | ) {
|
114 | let result;
|
115 |
|
116 | try {
|
117 | if (exp['matchObject']) {
|
118 | result = matchObject(data, exp['matchObject']);
|
119 | return result;
|
120 | }
|
121 |
|
122 | if (exp['matchArray']) {
|
123 | result = matchArray(data, exp['matchArray']);
|
124 | return result;
|
125 | }
|
126 |
|
127 | if (exp['noMatch'] !== undefined) {
|
128 | result = exp['noMatch'];
|
129 | return result;
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | if (exp['matchParam']) {
|
138 | result = getData(data, exp['matchParam'], exp['matchType']);
|
139 | result =
|
140 | (result === undefined) ? typeCharge(exp['default']) : result;
|
141 |
|
142 |
|
143 | if (isEmptyObj(result)) changeFilterDefaultObject(true);
|
144 | return result;
|
145 | }
|
146 |
|
147 | if(exp['matchArrParam']) {
|
148 | result = getArrData(data, exp['matchArrParam'], exp['matchType']);
|
149 | result =
|
150 | (result === undefined) ? typeCharge(exp['default']) : result;
|
151 | return result;
|
152 | }
|
153 |
|
154 | if (exp['matchFun']) {
|
155 | result = exp['matchFun'].apply(that, [data].concat(objToArray(stack, 'value')));
|
156 | return result;
|
157 | }
|
158 | } catch (e) {
|
159 | if (exp['default']) {
|
160 | result = typeCharge(exp['default']);
|
161 | return (config.filterDefaultObject && isEmptyObj(result)) ? undefined : result;
|
162 | }
|
163 |
|
164 | }
|
165 | };
|
166 |
|
167 | const getParams = (str, obj) => {
|
168 | let createFun = function () {
|
169 | return new Function (str);
|
170 | }
|
171 | obj.title = createFun();
|
172 | };
|
173 |
|
174 | export const getData = (
|
175 | data: object,
|
176 | exp: string,
|
177 | type: string
|
178 | ) => {
|
179 | let par = data;
|
180 | let token = exp.split('.');
|
181 |
|
182 | for (let i = 0; i < token.length; i++) {
|
183 |
|
184 | par = par[token[i]];
|
185 | }
|
186 |
|
187 | return type ? typeTrans(par, type) : par;
|
188 | };
|
189 |
|
190 | const getArrData = (
|
191 | data: object,
|
192 | exp: string,
|
193 | type: string
|
194 | ) => {
|
195 | let token = exp.split('.');
|
196 | let index = parseInt(token.shift(), 10);
|
197 | if (!isNum(index) || data[index] === undefined) {
|
198 | console.log('error: the Array index is not exist!');
|
199 | return undefined;
|
200 | }
|
201 |
|
202 | return getData(data[index], token.join('.'), type);
|
203 | };
|
204 |
|
205 | const isIgnore = (
|
206 | key: string
|
207 | ) => {
|
208 | return config.ignoreTokenKey.indexOf(key) !== -1;
|
209 | };
|