efront 4.0.37 → 4.0.39

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.
@@ -26,7 +26,10 @@ var split = function (s, n) {
26
26
  return r;
27
27
  };
28
28
  var fixme = function (neg, res, nl) {
29
- if (nl > 0) res = res.slice(0, res.length - nl) + "." + res.slice(res.length - nl).replace(/0+$/, '');
29
+ if (nl > 0) {
30
+ if (res.length < nl) res = repeat0(nl - res.length) + res;
31
+ res = res.slice(0, res.length - nl) + "." + res.slice(res.length - nl).replace(/0+$/, '');
32
+ }
30
33
  res = res.replace(/^0+(?!\.)/, '');
31
34
  if (/^\./.test(res)) res = '0' + res;
32
35
  if (neg) res = '-' + res;
@@ -55,15 +58,79 @@ var fixde = function (numstr, fractionDigits, compare) {
55
58
  if (neg) res = '-' + res;
56
59
  return res;
57
60
  };
61
+ var vmap = Object.create(null);
62
+ var vsrc = [];
63
+ var adds = function (s, c) {
64
+ for (var cx = 0, dx = s.length; cx < dx; cx++) {
65
+ vmap[s.charAt(cx)] = c + cx;
66
+ vsrc[c + cx] = s.charAt(cx);
67
+ }
68
+ }
69
+ adds("0123456789", 0);
70
+ adds("abcdefghijklmnopqrstuvwxyz", 10);
58
71
  class BigNumber {
59
- constructor(value) {
72
+ constructor(value, system_scale) {
60
73
  if (!this || this.constructor !== BigNumber) {
61
- return new BigNumber(value);
74
+ return new BigNumber(value, system_scale);
75
+ }
76
+ system_scale = system_scale | 0;
77
+ if (!system_scale) system_scale = 10;
78
+ if (system_scale <= 1 || system_scale > 36) throw new Error("进制错误!");
79
+ var num = '0';
80
+ var dotOccurs;
81
+ var scale = system_scale;
82
+ if (system_scale <= 36) {
83
+ value = value.toLowerCase();
84
+ }
85
+ var sign = false;
86
+ for (var v of value) {
87
+ if (v === "-") {
88
+ sign = !sign;
89
+ continue;
90
+ }
91
+ if (v === '+') continue;
92
+ if (v === '.') {
93
+ dotOccurs = true;
94
+ continue;
95
+ }
96
+ if (v === '_') continue;
97
+ if (v === ',') continue;
98
+ if (dotOccurs) {
99
+ num = BigNumber.add(num, BigNumber.div(vmap[v], scale, BigNumber.DECIMAL_DIGIT))
100
+ scale = BigNumber.prd(scale, system_scale);
101
+ }
102
+ else {
103
+ num = BigNumber.add(BigNumber.prd(num, scale), vmap[v]);
104
+ }
62
105
  }
63
- this.value = value;
106
+ if (sign) num = '-' + num;
107
+ this.value = num;
64
108
  }
65
- toString() {
66
- return this.value;
109
+ static DECIMAL_DIGIT = 120;
110
+ toString(system_scale) {
111
+ system_scale |= 0;
112
+ if (!system_scale || system_scale === 10) return this.value || "0";
113
+ if (system_scale <= 1 || system_scale > 36) throw new Error("进制错误!");
114
+ var [s, n, m] = prepare(this.value);
115
+ var dist = [];
116
+ while (n && n !== "0") {
117
+ var n0 = BigNumber.div(n, system_scale, 0);
118
+ var a = BigNumber.sub(n, BigNumber.prd(n0, system_scale));
119
+ n = n0;
120
+ dist.unshift(vsrc[+a]);
121
+ }
122
+ if (m && m !== "0") {
123
+ var c = 0;
124
+ dist.push('.');
125
+ while (m !== "0") {
126
+ [a, m = '0'] = BigNumber.prd("0." + m, system_scale).split('.');
127
+ dist.push(vsrc[+a]);
128
+ if (++c > BigNumber.DECIMAL_DIGIT || !m) break;
129
+ }
130
+ }
131
+ if (!dist.length) dist.push("0");
132
+ if (s) dist.unshift('-');
133
+ return dist.join('');
67
134
  };
68
135
  add(bignumber) {
69
136
  return new BigNumber(BigNumber.add(this.value, bignumber));
@@ -79,8 +146,8 @@ class BigNumber {
79
146
  }
80
147
  static fix(numstr, fractionDigits) {
81
148
  fractionDigits = +fractionDigits || 0;
82
- if (fractionDigits < 0 || fractionDigits > 100) {
83
- throw new Error("小数位数只能是0和100之间的数字");
149
+ if (fractionDigits < 0 || fractionDigits > BigNumber.DECIMAL_DIGIT) {
150
+ throw new Error(`小数位数只能是0和${BigNumber.DECIMAL_DIGIT}之间的数字`);
84
151
  }
85
152
  return fixde(numstr, fractionDigits, 4);
86
153
  }
@@ -67,4 +67,6 @@ assert(BigNumber.div("9007199254740992.2345678901", 0.001, 6), "9007199254740992
67
67
  assert(BigNumber.div("9007199254740992.2345678901", "9007199254740992.2345678901", 6), "1.000000");
68
68
  assert(BigNumber.div("9007199254740992.2345678901", "90071992547409922345678.901", 6), "0.000000");
69
69
  assert(BigNumber.div("9007199254740992.2345678901", "900719925474099223456.78901", 6), "0.000010");
70
- assert(BigNumber.div("99999", "9", 6), "11111.000000");
70
+ assert(BigNumber.div("99999", "9", 6), "11111.000000");
71
+ assert(new BigNumber("1.23", 8).toString(), "1.296875");
72
+ assert(new BigNumber("1.23", 8).toString(8), "1.23");
@@ -1,32 +1,62 @@
1
- var couple = function (source, marker) {
1
+ var _pinyin = null;
2
+ Promise.resolve(init("pinyin")).then(py => _pinyin = py);
3
+ var isABC = a => /^[a-zA-Z]$/.test(a);
4
+ var couple = function (source, marker, pinyin) {
5
+ var isLike = function () {
6
+ var is = isABC(s), im = isABC(m);
7
+ if (is && im) return s.toLowerCase() === m.toLowerCase();
8
+ if (im) {
9
+ if (pinyin.py(s).indexOf(m.toLowerCase()) < 0) return false;
10
+ var py = pinyin.pinyin(s).split('|');
11
+ var i = 2;
12
+ var t = c2 + ct;
13
+ for (var p of py) {
14
+ while (i + t <= marker.length && p.indexOf(marker.slice(t, i + t).toLowerCase()) === 0) i++;
15
+ }
16
+ c2 += i - 2;
17
+ dt = setDt();
18
+ return true;
19
+ }
20
+ return false;
21
+ };
22
+ var setDt = function () {
23
+ var d1 = len1 - c1;
24
+ var d2 = len2 - c2;
25
+ return d1 > d2 ? d2 : d1;
26
+ };
2
27
  var len1 = source.length;
3
28
  var len2 = marker.length;
4
29
  var match = "", begin1 = len1, begin2 = len2;
30
+ var end2 = begin2;
5
31
  for (var cx = -len1, dx = len2; cx < dx; cx++) {
6
32
  var c1 = cx >= 0 ? 0 : -cx;
7
33
  var c2 = cx >= 0 ? cx : 0;
8
- var d1 = len1 - c1;
9
- var d2 = len2 - c2;
34
+ var cc = c2;
10
35
  var start = 0, end = 0;
11
- for (var ct = 0, dt = d1 > d2 ? d2 : d1; ct < dt; ct++) {
12
- if (source[c1 + ct] === marker[c2 + ct]) {
36
+ for (var ct = 0, dt = setDt(); ct < dt; ct++) {
37
+ var s = source[c1 + ct];
38
+ var m = marker[c2 + ct];
39
+ if (s === m || pinyin && isLike()) {
13
40
  end = ct + 1;
14
41
  if (end === dt && end - start > match.length) {
15
42
  match = source.slice(c1 + start, c1 + end);
16
43
  begin1 = c1 + start;
17
- begin2 = c2 + start;
44
+ begin2 = cc + start;
45
+ end2 = c2 + end;
18
46
  }
19
47
  } else {
20
48
  if (end - start > match.length) {
21
49
  match = source.slice(c1 + start, c1 + end);
22
50
  begin1 = c1 + start;
23
- begin2 = c2 + start;
51
+ begin2 = cc + start;
52
+ end2 = c2 + end;
24
53
  }
54
+ cc = c2;
25
55
  start = ct + 1;
26
56
  }
27
57
  }
28
58
  }
29
- return [match, begin1, begin2];
59
+ return [match, begin1, begin2, end2];
30
60
  };
31
61
  var MARK_PRE1, MARK_PRE2, _PRE1, _PRE2 = _PRE1 = "<b>";
32
62
  var MARK_AFT1, MARK_AFT2, _AFT1, _AFT2 = _AFT1 = "</b>";
@@ -52,31 +82,31 @@ var power = function (source, search) {
52
82
  if (!search || !source) {
53
83
  return [0, source];
54
84
  }
55
- var matchers = couple(source, search);
85
+ var matchers = couple(source, search, _pinyin);
56
86
  var match_text = matchers[0];
57
- var match_start2 = matchers[1];
87
+ var match_start = matchers[1];
58
88
  if (search.length === 1) {
59
89
  var p = 0;
60
- var res = source.replace(new RegExp(search.replace(/[\\\*\?\+\(\)\[]/g, "\\$&"), "g"), (m, i) => {
90
+ var res = source.replace(new RegExp(search.replace(/[\\\*\?\+\(\)\[]/g, "\\$&"), "ig"), (m, i) => {
61
91
  if (!p) p = .1 / (1 + i);
62
- return MARK_PRE1 + search + MARK_AFT1;
92
+ return MARK_PRE1 + m + MARK_AFT1;
63
93
  });
64
94
  return [p, res];
65
95
  }
66
- if (match_text.length > 1) {
67
- var match_text_pre = source.slice(0, match_start2);
68
- var match_text_aft = source.slice(match_start2 + match_text.length);
96
+ if (matchers[3] - matchers[2] > 1) {
97
+ var match_text_pre = source.slice(0, match_start);
98
+ var match_text_aft = source.slice(match_start + match_text.length);
69
99
  var pp = 0, ap = 0;
70
100
  var p = match_text.length;
71
101
  p = match_text.length;
72
- if (match_start2) p += .1 / match_start2 - .2;
102
+ if (match_start) p += .1 / match_start - .2;
73
103
  if (match_text_pre.length > 1) {
74
104
  [pp, match_text_pre] = power(match_text_pre, search);
75
105
  }
76
106
  if (match_text_aft.length > 1) {
77
107
  [ap, match_text_aft] = power(match_text_aft, search);
78
108
  }
79
- if (match_text.length !== search.length) {
109
+ if (matchers[3] - matchers[2] !== search.length) {
80
110
  p += (pp + ap) / source.length / search.length * .01 - .2;
81
111
  }
82
112
  return [p, match_text_pre.concat(MARK_PRE1, match_text, MARK_AFT1, match_text_aft)];
@@ -4,4 +4,10 @@ var test = function (t1, t2) {
4
4
  }
5
5
  test("11234", '1234');
6
6
  test("1234", '1234');
7
-
7
+ await init('pinyin')
8
+ test('我们', 'women');
9
+ test('我们', 'wm');
10
+ test('盛', 'cheng');
11
+ test('盛', 'sheng');
12
+ test('着火', 'zhaohuo');
13
+ test('着陆', 'zhuolu');