efront 4.4.8 → 4.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,18 @@
1
1
  function splitSeek(express) {
2
2
  var dist = [];
3
3
  var x = 0;
4
+ var called = false;
4
5
  express.forEach((e, i) => {
5
- if (i > x && /\?/.test(e)) {
6
+ if (isArray(e)) called = true;
7
+ if (i > x && /^\?/.test(e)) {
6
8
  var exp = express.slice(x, x = i);
9
+ exp.called = called;
7
10
  dist.push(exp);
11
+ called = false;
8
12
  }
9
13
  });
10
14
  if (x < express.length) dist.push(express.slice(x, express.length));
15
+ dist[dist.length - 1].called = called;
11
16
  dist.forEach(d => d[0] = d[0].replace(/^\?/, ''))
12
17
  return dist;
13
18
  }
@@ -24,31 +29,114 @@ function getTmpvar(explist) {
24
29
  return tmpvar;
25
30
  }
26
31
  var autoDefine = false;
27
- function createSeek(express, split = true) {
28
- var tmpvar = 'a', undef = '_', _null = '$';
32
+ function joinsharp(sharped, ctx) {
33
+ var willcall = [], call = false;
34
+ for (var cx = 1, dx = sharped.length; cx < dx; cx++) {
35
+ if (/^\(/.test(sharped[cx])) {
36
+ var p = sharped[cx - 1];
37
+ if (/\)$/.test(p)) continue;
38
+ if (cx > 1 || /\./.test(p)) willcall[cx] = true;
39
+ }
40
+ }
41
+ var result = [];
42
+ var will = false, call = false;
43
+ if (sharped[sharped.length - 1] === '.') sharped[sharped.length - 1] = "";
44
+ for (var cx = 0, dx = sharped.length; cx < dx; cx++) {
45
+ var s = sharped[cx];
46
+ var plus = /^\//.test(s);
47
+ if (plus) s = s.slice(1);
48
+ call = will;
49
+ will = willcall[cx + 1];
50
+ if (will) {
51
+ if (/\]/.test(s)) {
52
+ s = [s];
53
+ s.unshift('');
54
+ }
55
+ else {
56
+ s = s.split('.');
57
+ if (s.length > 1) {
58
+ s.unshift(s.splice(0, s.length - 1).join('.'));
59
+ s[1] = '.' + s[1];
60
+ }
61
+ else {
62
+ s.unshift("");
63
+ }
64
+ }
65
+ if (plus) {
66
+ s[0] = result[result.length - 1] + s[0];
67
+ result[result.length - 1] = s;
68
+ }
69
+ else result.push(s);
70
+ }
71
+ else if (call) {
72
+ var params = s.replace(/^\(\s*([\s\S]*)\s*\)$/, '$1');
73
+ result.push(`["call"](${ctx}${params ? ',' + params : ''})`);
74
+ }
75
+ else {
76
+ if (plus) {
77
+ result[result.length - 1] += s;
78
+ }
79
+ else result.push(s);
80
+ }
81
+ }
82
+ return result;
83
+ }
84
+ var wrapquote = false;
85
+ function createSeek(sharped, splited = true) {
86
+ if (splited) wrapquote = false;
87
+ if (sharped.length <= 1) return sharped[0];
88
+ var tmpvar = 'a', undef = '_', tmpctx = 'c';
89
+ var express = splited ? joinsharp(sharped, tmpctx) : sharped;
29
90
  if (express.length <= 1) return express[0];
30
- var notmp = !/[\.\[\(\{]/.test(express[0]);
91
+ if (splited) wrapquote = true;
92
+ var [exp0, exp1] = express;
93
+ var notmp = typeof exp0 === 'string' && !/[\.\[\(\{]/.test(exp0);
31
94
  if (express.length === 2) {
32
- if (split && /^\?/.test(express[1])) {
33
- if (notmp) return `typeof ${tmpvar}!=='undefined'&&${tmpvar}!==null?${tmpvar}:${express[1].slice(1)}`;
34
- return `function(${tmpvar},${undef}){return ${tmpvar}===${undef}||${tmpvar}===null?${express[1].slice(1)}:${tmpvar}}(${express[0]})`;
95
+ if (splited && /^\?/.test(exp1)) {
96
+ if (notmp) return `typeof ${exp0}!=='undefined'&&${exp0}!==null?${exp0}:${exp1.slice(1)}`;
97
+ return `function(${tmpvar},${undef}){return ${tmpvar}==${undef}?${exp1.slice(1)}:${tmpvar}}(${exp0})`;
35
98
  }
36
99
  if (notmp) {
37
- tmpvar = express[0];
38
100
  if (autoDefine) {
39
- return `typeof ${tmpvar}==='undefined'||${tmpvar}===null?void 0:${tmpvar}${express[1]}`;
101
+ return `typeof ${exp0}==='undefined'||${exp0}===null?void 0:${exp0}${exp1}`;
40
102
  }
41
- return `${tmpvar}===void 0||${tmpvar}===null?void 0:${tmpvar}${express[1]}`;
103
+ return `${exp0}==null?void 0:${exp0}${exp1}`;
42
104
  };
43
- return `function(${tmpvar},${undef}){return ${tmpvar}===${undef}||${tmpvar}===null?${undef}:${tmpvar}${express[1]}}(${express[0]})`;
105
+ if (isArray(exp0)) {
106
+ return `function(${tmpctx},${tmpvar},${undef}){${tmpvar}=${tmpctx}${exp0[exp0.length - 1]};return ${tmpvar}==${undef}?${undef}:${tmpvar}${exp1}}(${exp0.slice(0, exp0.length - 1)})`;
107
+ }
108
+ return `function(${tmpvar},${undef}){return ${tmpvar}==${undef}?${undef}:${tmpvar}${exp1}}(${exp0})`;
44
109
  }
45
- if (!split) {
46
- var dist = express.slice(1).map((search, i, a) => {
47
- return `if(${tmpvar}===${undef}||${tmpvar}===${_null})return;${tmpvar}=${tmpvar}${search};`;
110
+ if (!splited) {
111
+ var called = express.called;
112
+ var prop = isArray(exp0) ? exp0[exp0.length - 1] : '';
113
+ var prop0 = prop;
114
+ var dist = express.slice(1).map(search => {
115
+ var res = prop ? `${tmpvar}=${tmpctx}${prop}` : '';
116
+ res += `if(${tmpvar}==${undef})return;`;
117
+ if (isArray(search)) {
118
+ prop = search[search.length - 1];
119
+ res += `${tmpctx}=${tmpvar}${search.slice(0, search.length - 1)},${tmpvar}=${tmpctx}${prop};`;
120
+ prop = '';
121
+ }
122
+ else {
123
+ res += `${tmpvar}=${tmpvar}${search};`;
124
+ }
125
+ return res;
48
126
  }).join('');
49
- dist = `function(${tmpvar},${_null},${undef}){${dist}return ${tmpvar}}(${express[0]},null)`;
127
+ if (called) {
128
+ if (prop0) {
129
+ dist = `function(${tmpctx},${tmpvar},${undef}){${dist}return ${tmpvar}}(${exp0.slice(0, exp0.length - 1)})`;
130
+ }
131
+ else {
132
+ dist = `function(${tmpvar},${tmpctx},${undef}){${dist}return ${tmpvar}}(${exp0})`;
133
+ }
134
+ }
135
+ else {
136
+ dist = `function(${tmpvar},${undef}){${dist}return ${tmpvar}}(${exp0})`;
137
+ }
50
138
  if (notmp && autoDefine) {
51
- dist = `typeof ${express[0]}==='undefined'&&${express[0]}===null?void 0:${dist}`;
139
+ dist = `typeof ${exp0}==='undefined'||${exp0}===null?void 0:${dist}`;
52
140
  }
53
141
  return dist;
54
142
  }
@@ -59,14 +147,14 @@ function createSeek(express, split = true) {
59
147
  tmpvar = getTmpvar(explist);
60
148
  var dist = explist.slice(0, explist.length - 1).map(express => {
61
149
  var dist = createSeek(express, false);
62
- return `${tmpvar}=${dist};if(${tmpvar}!==void 0&&${tmpvar}!==null)return ${tmpvar};`;
150
+ return `${tmpvar}=${dist};if(${tmpvar}!=null)return ${tmpvar};`;
63
151
  }).join("") + `return ${createSeek(explist[explist.length - 1], false)}`;
64
152
  return `function(${tmpvar}){${dist}}()`;
65
153
  }
66
154
  function main(express, autodef = true) {
67
155
  autoDefine = autodef;
68
- if (!/\?\s*\.(?=[^\d])|\?\s*[\?\]\}\)\:\,=|%&;\>\<]|\?\s*$/.test(express)) return express;
69
- var reg = /\\[\s\S]|\?\s*([\.](?!\d)|\?|$|(?=[\?\]\}\)\:\,=|%&;\>\<\*\/]))|[\:\,\+\=\-\!%\^\|\/\&\*\!\;\?\>\<~\{\}\[\]\(\)'"`\s]/g;
156
+ if (!/\?\s*\.(?=[^\d])|\?\s*[\?\]\}\)\:\,=|%&;\>\<]|\?\.?\s*$/.test(express)) return express;
157
+ var reg = /\\[\s\S]|\?\s*([\.](?!\d)|\?|$|(?=[\?\]\}\)\:\,\=\|%&;\>\<\*\/]))|[\:\,\+\=\-\!%\^\|\/\&\*\!\;\?\>\<\~\{\}\[\]\(\)'"`\s]/g;
70
158
  var cache = [], queue = [];
71
159
  var exp = [];
72
160
  var instr = false;
@@ -80,8 +168,11 @@ function main(express, autodef = true) {
80
168
  exp.splice(0, exp.length, '');
81
169
  e = '';
82
170
  }
83
- if (/\.$/.test(e) && /^[\[\("'`]/.test(s)) {
84
- e = e.replace(/\.$/, '');
171
+ if (/\.$/.test(e)) {
172
+ if (/^[\[\("'`]/.test(s)) e = e.replace(/\.$/, '');
173
+ }
174
+ else if (!cache.length) {
175
+ return exp.push('/' + s);
85
176
  }
86
177
  e += s;
87
178
  exp[exp.length - 1] = e;
@@ -103,7 +194,7 @@ function main(express, autodef = true) {
103
194
  var add_punc = function (m) {
104
195
  if (exp.length) {
105
196
  var temp = createSeek(exp);
106
- queue.push(exp.length > 1 ? `(${temp})` : temp, m);
197
+ queue.push(wrapquote ? `(${temp})` : temp, m);
107
198
  exp = [];
108
199
  } else if (queue.length) {
109
200
  queue[queue.length - 1] += m;
@@ -129,7 +220,6 @@ function main(express, autodef = true) {
129
220
  isstr = true;
130
221
  index++;
131
222
  }
132
-
133
223
  var str = express.slice(lastIndex, index);
134
224
  if (str) {
135
225
  add_exp(str);
@@ -1,11 +1,19 @@
1
1
  assert(renderExpress("[].a"), "[].a")
2
2
  assert(renderExpress("a?b:c"), "a?b:c")
3
3
  assert(renderExpress("a?"), "(typeof a==='undefined'||a===null?void 0:a)")
4
- assert(renderExpress("[].a?.()"), "(function(a,_){return a===_||a===null?_:a()}([].a))")// 暂不支持在?.()的函数中访问this,此为临时方案
5
- assert(renderExpress("[].a?.b?.c?.()"), "(function(a,$,_){if(a===_||a===$)return;a=a.b;if(a===_||a===$)return;a=a.c;if(a===_||a===$)return;a=a();return a}([].a,null))")
4
+ assert(renderExpress("b+a?."), "b+(typeof a==='undefined'||a===null?void 0:a)")
5
+ assert(renderExpress("a?.+b"), "(typeof a==='undefined'||a===null?void 0:a)+b")
6
+ assert(renderExpress("a?.()"), "(typeof a==='undefined'||a===null?void 0:a())")
7
+ assert(renderExpress("a.b?.()"), `(function(c,a,_){a=c.b;return a==_?_:a["call"](c)}(a))`)
8
+ assert(renderExpress("a[['a[']][]?.()"), `(function(c,a,_){a=c[];return a==_?_:a["call"](c)}(a[['a[']]))`)
9
+ assert(renderExpress("a?.b?.()"), `(typeof a==='undefined'||a===null?void 0:function(a,c,_){if(a==_)return;c=a,a=c.b;if(a==_)return;a=a["call"](c);return a}(a))`)
10
+ assert(renderExpress("[].a?.()"), `(function(c,a,_){a=c.a;return a==_?_:a["call"](c)}([]))`)// 已支持在?.()的函数中访问this
11
+ assert(renderExpress("[].a?.b?.c?.()"), `(function(a,c,_){if(a==_)return;a=a.b;if(a==_)return;c=a,a=c.c;if(a==_)return;a=a["call"](c);return a}([].a))`)
6
12
  assert(renderExpress("a??b"), "(typeof a!=='undefined'&&a!==null?a:b)")
7
- assert(renderExpress("a?.b??b?.c", false), "(function(b0){b0=a===void 0||a===null?void 0:a.b;if(b0!==void 0&&b0!==null)return b0;return b===void 0||b===null?void 0:b.c}())")
13
+ assert(renderExpress("a?.b??b?.c", false), "(function(b0){b0=a==null?void 0:a.b;if(b0!=null)return b0;return b==null?void 0:b.c}())")
8
14
 
9
- assert(renderExpress("[]?.a"), "(function(a,_){return a===_||a===null?_:a.a}([]))")
10
- assert(renderExpress("[]?.()"), "(function(a,_){return a===_||a===null?_:a()}([]))")
11
- assert(renderExpress("[]?.[]"), "(function(a,_){return a===_||a===null?_:a[]}([]))")
15
+ assert(renderExpress("[]?.a"), "(function(a,_){return a==_?_:a.a}([]))")
16
+ assert(renderExpress("[]?.()"), "(function(a,_){return a==_?_:a()}([]))")
17
+ assert(renderExpress("[]?.[]"), "(function(a,_){return a==_?_:a[]}([]))")
18
+ assert(renderExpress("a['a?.a']"), "a['a?.a']")
19
+ assert(renderExpress("a?.['a?.a']?.b"), "(typeof a==='undefined'||a===null?void 0:function(a,_){if(a==_)return;a=a['a?.a'];if(a==_)return;a=a.b;return a}(a))")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "efront",
3
- "version": "4.4.8",
3
+ "version": "4.4.9",
4
4
  "description": "简化前端开发,优化web性能",
5
5
  "main": "public/efront.js",
6
6
  "directories": {