UNPKG

5.21 kBJavaScriptView Raw
1/**
2 * @description 字符串解析内容
3 */
4import {
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
17import {
18 matchObject,
19 matchArray
20} from 'MATCH/match';
21
22import config, { changeFilterDefaultObject } from 'MATCH/config';
23
24import stack from 'MATCH/stack';
25
26export 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 // re = /(?:(.*?)(\|\|))|(.*)/ig
71 //for (i of strArr) {
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 * @description 把exp解析的内容反装回真是值
108 */
109export 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 //if (exp['defaultParam']) {
133 // result = getData(data, exp['matchParam']) || getParams(exp['defaultParam'], obj);
134 // return result;
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 // 记录此时的空对象是默认产生的 防止被filter过滤
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 // console.log(e);
164 }
165};
166
167const getParams = (str, obj) => {
168 let createFun = function () {
169 return new Function (str);
170 }
171 obj.title = createFun();
172};
173
174export const getData = (
175 data: object,
176 exp: string, // 对应的对象字面量字符串 xx.xxx
177 type: string // 对应的类型
178) => {
179 let par = data;
180 let token = exp.split('.');
181 // 递归获取
182 for (let i = 0; i < token.length; i++) {
183 //for (let i of token) {
184 par = par[token[i]];
185 }
186
187 return type ? typeTrans(par, type) : par;
188};
189
190const 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
205const isIgnore = (
206 key: string
207) => {
208 return config.ignoreTokenKey.indexOf(key) !== -1;
209};