efront 3.31.2 → 3.32.0

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,14 +1,12 @@
1
- function getIndexFromOrderedArray_test() {
2
- if (getIndexFromOrderedArray([0, 0, 0], 0) !== 2) {
3
- throw new Error("expect getIndexFromOrderedArray([0,0,0],0) to be equal 2");
4
- }
5
- if (getIndexFromOrderedArray([0, 1, 2], 0) !== 0) {
6
- throw new Error("expect getIndexFromOrderedArray([0,1,2],0) to be equal 0");
7
- }
8
- if (getIndexFromOrderedArray([1, 2, 3], 0) !== -1) {
9
- throw new Error("expect getIndexFromOrderedArray([1,2,3],0) to be equal -1");
10
- }
11
- if (getIndexFromOrderedArray([1, 2, 3], 2) !== 1) {
12
- throw new Error("expect getIndexFromOrderedArray([1,2,3],2) to be equal 1");
13
- }
1
+ if (getIndexFromOrderedArray([0, 0, 0], 0) !== 2) {
2
+ throw new Error("expect getIndexFromOrderedArray([0,0,0],0) to be equal 2");
3
+ }
4
+ if (getIndexFromOrderedArray([0, 1, 2], 0) !== 0) {
5
+ throw new Error("expect getIndexFromOrderedArray([0,1,2],0) to be equal 0");
6
+ }
7
+ if (getIndexFromOrderedArray([1, 2, 3], 0) !== -1) {
8
+ throw new Error("expect getIndexFromOrderedArray([1,2,3],0) to be equal -1");
9
+ }
10
+ if (getIndexFromOrderedArray([1, 2, 3], 2) !== 1) {
11
+ throw new Error("expect getIndexFromOrderedArray([1,2,3],2) to be equal 1");
14
12
  }
File without changes
@@ -0,0 +1,25 @@
1
+ var float = function () {
2
+ return Math.random();
3
+ };
4
+ var int = function () {
5
+ return Math.random() * 10 | 0;
6
+ };
7
+ var str = function () {
8
+ return Math.random().toString(36).slice(2, 3);
9
+ }
10
+ var test = function (gen, isle, sort = isle) {
11
+ var array = [];
12
+ for (var cx = 0, dx = 10; cx < dx; cx++)saveToOrderedArray(array, gen(), isle);
13
+ var res = array.slice();
14
+ assert(res.join(','), array.sort(sort).join(','));
15
+ console.log(res)
16
+ }
17
+ test(float, undefined, (a, b) => a - b);
18
+ test(float, (a, b) => a >= b, (a, b) => b - a);
19
+ test(float, (a, b) => a > b, (a, b) => b - a);
20
+ test(int, (a, b) => a <= b, (a, b) => a - b);
21
+ test(int, (a, b) => a > b, (a, b) => b - a);
22
+ test(int, (a, b) => a >= b, (a, b) => b - a);
23
+ test(str, (a, b) => a > b);
24
+ test(str, (a, b) => a <= b);
25
+
File without changes
@@ -2,7 +2,9 @@
2
2
  var colored = Object.create(null);
3
3
  var lazy = require("../basic/lazy");
4
4
  var colors = require("./colors");
5
+ var strings = require("../basic/strings");
5
6
  var lastLogLength = 0;
7
+ var needNextLine = false;
6
8
  var getColor = function (c) {
7
9
  switch (c) {
8
10
  case "red":
@@ -28,14 +30,23 @@ var getColor = function (c) {
28
30
  }
29
31
  return '';
30
32
  };
33
+ var colorReg = /<(\/?)([a-z][\w]*)[^\/\\\>]*\>/ig;
31
34
  var write = function (hasNewLine, str) {
32
- str = String(str).replace(/<([a-z][\w]*)[^\>]*\>([\s\S]*?)<\/\1\>/ig, function (_, c, s) {
33
- var color = getColor(c);
34
- if (color) return color + s + colors.Reset;
35
- return s;
35
+ var colorpath = [];
36
+ str = String(str).replace(colorReg, function (_, e, c) {
37
+ if (e) {
38
+ colorpath.pop();
39
+ c = colorpath[colorpath.length - 1];
40
+ }
41
+ else colorpath.push(c);
42
+ if (c) var color = getColor(c);
43
+ var res = [];
44
+ if (e) res.push(colors.Reset);
45
+ if (color) res.push(color);
46
+ return res.join('');
36
47
  });
37
48
  process.stdout.cork();
38
- var hasNextLine = /[\r\n\u2028\u2029]/.test(str);
49
+ var hasNextLine = /[\r\n\u2028\u2029]$/.test(str);
39
50
  if (process.stdout.isTTY) {
40
51
  if (lastLogLength) {
41
52
  var width = process.stdout.columns;
@@ -48,7 +59,17 @@ var write = function (hasNewLine, str) {
48
59
  else {
49
60
  if (!hasNewLine && !hasNextLine) str = '';
50
61
  }
51
- hasNewLine && !hasNextLine ? process.stdout.write(str + "\r\n") : process.stdout.write("\r" + str);
62
+ if (needNextLine && !/^[\r\n\u2028\u2029]/.test(str)) {
63
+ process.stdout.write("\r\n");
64
+ }
65
+ if (hasNewLine && !hasNextLine) {
66
+ process.stdout.write(str)
67
+ needNextLine = true;
68
+ }
69
+ else {
70
+ process.stdout.write("\r" + str);
71
+ needNextLine = false;
72
+ }
52
73
  if (hasNextLine) hasNewLine = true;
53
74
  if (hasNewLine) {
54
75
  lastLogLength = 0;
@@ -74,44 +95,173 @@ var write = function (hasNewLine, str) {
74
95
  var logger = function (...args) {
75
96
  var label = fgColor + bgColor + info + reset;
76
97
  var time_stamp = '';
77
- var str = [time_stamp, label, ...args].join(" ");
78
- if (queue.length > 1 && !queue[queue.length - 2] && !/[\r\n\u2028\u2029]/.test(queue[queue.length - 1])) {
79
- queue.pop();
80
- queue.pop();
81
- }
98
+ var str = [time_stamp, label, ...args.map(a => format(a))].join(" ");
82
99
  write1(hasNewLine, str);
83
100
  };
84
101
  colored[log] = logger;
85
102
  });
86
- var queue = [];
87
- var flush = function () {
88
- while (queue.length) write(queue.shift(), queue.shift());
89
- };
90
- // var write0 = lazy(flush, -60);
91
103
  var write1 = function (hasNewLine, str) {
92
- writeid++;
93
- // queue.push(hasNewLine, str);
104
+ drop.cancel();
94
105
  write(hasNewLine, str);
95
106
  };
96
- colored.flush = flush;
107
+ var formatRows = function (arg, rows, deep, entry, leave) {
108
+ if (rows.length === 0) return entry + leave;
109
+ var ci = circleobjs.indexOf(arg);
110
+ if (ci >= 0) {
111
+ entry = `<cyan><引用点 *${ci + 1}></cyan> ` + entry;
112
+ }
113
+ if (deepobjs.length === 0) circleobjs.splice(0, circleobjs.length);
114
+ var space = new Array(deep).join(" ");
115
+ var deepspace = new Array(deep + 1).join(" ");
116
+ var lens = rows.map(r => r.replace(colorReg, '').replace(/[\u00ff-\uffff]/g, '00').length);
117
+ var maxLength = Math.max(...lens) + 2;
118
+ var itemcount = (process.stdout.columns - deepspace.length - 10) / maxLength | 0;
119
+ if (itemcount * itemcount > rows.length) itemcount = Math.ceil(Math.sqrt(rows.length));
120
+ if (itemcount < 1) itemcount = 1;
121
+ var hasNextLine = false;
122
+ var isArray = arg instanceof Array;
123
+ for (var r of rows) {
124
+ if (/[\r\n\u2028\u2029]/.test(r)) {
125
+ itemcount = 1;
126
+ hasNextLine = true;
127
+ break;
128
+ }
129
+ }
130
+ if (!hasNextLine && deepspace.length + maxLength * rows.length < process.stdout.columns)
131
+ return `${entry} ${rows.join(", ")} ${leave}`;
132
+ var res;
133
+ if (itemcount <= 1) {
134
+ res = rows;
135
+ }
136
+ else {
137
+ var maxLength = Array(itemcount).fill(0);
138
+ for (var cy = 0, dy = itemcount; cy < dy; cy++) {
139
+ for (var cx = cy, dx = rows.length - 1; cx < dx; cx += itemcount) {
140
+ if (maxLength[cy] < lens[cx]) maxLength[cy] = lens[cx];
141
+ }
142
+ }
143
+ maxLength = maxLength.map(i => i + 2);
144
+ res = [];
145
+ if (isArray) {
146
+ for (var cx = 0, dx = rows.length; cx < dx; cx += itemcount) {
147
+ res.push(rows.slice(cx, cx + itemcount).map((r, i) => {
148
+ return Array(maxLength[i] - lens[cx + i]).join(" ") + r;
149
+ }).join(', '));
150
+ }
151
+ }
152
+ else {
153
+ for (var cx = 0, dx = rows.length; cx < dx; cx += itemcount) {
154
+ res.push([rows[cx], ...rows.slice(cx + 1, cx + itemcount).map((r, i) => {
155
+ return Array(maxLength[i] - lens[cx + i]).join(" ") + r;
156
+ })].join(', '));
157
+ }
158
+ }
159
+ }
160
+ return `${entry}\r\n${deepspace + res.join(",\r\n" + deepspace)}\r\n${space}${leave}`
161
+ };
162
+ var deepobjs = [];
163
+ var circleobjs = [];
164
+ var format = function (arg, deep = 0) {
165
+ deep++;
166
+ if (arg === null) return arg;
167
+ if (typeof arg === 'string') {
168
+ if (deep > 1) return "<green>" + strings.encode(arg) + "</green>";
169
+ return arg;
170
+ }
171
+ if (typeof arg === 'function') return `<cyan>[${arg.__proto__.constructor.name}${arg.name ? ": " + arg.name : " (匿名)"}]</cyan>`;
172
+ if (/^(number|boolean)$/.test(typeof arg)) return '<yellow>' + arg + "</yellow>";
173
+ if (arg === undefined) return "<gray>undefined</gray>";
174
+ if (typeof arg === "object") {
175
+ if (deepobjs.indexOf(arg) >= 0) {
176
+ var ci = circleobjs.indexOf(arg);
177
+ if (ci < 0) ci = circleobjs.length, circleobjs.push(arg);
178
+ return `<cyan>[循环点 *${ci + 1}]</cyan>`;
179
+ }
180
+ if (arg instanceof Array) {
181
+ if (arg.length === 0) return '[]';
182
+ if (deep > 3) return `${arg.__proto__.constructor.name}(${arg.length})[ ... ]`;
183
+ deepobjs.push(arg);
184
+ var res = arg.slice(0, 100).map(a => format(a, deep));
185
+ deepobjs.pop();
186
+ if (arg.length > res.length) res.push(`<gray>.. 其他 ${arg.length - res.length} 项</gray>`);
187
+ return formatRows(arg, res, deep, '[', ']');
188
+ }
189
+ if (arg.constructor === Date) {
190
+ return '<purple>' + formatDate.call(arg) + "</purple>";
191
+ }
192
+ if (arg.constructor === RegExp) {
193
+ return `<red2>/${arg.source}/</red2><cyan>${arg.flags}</cyan>`;
194
+ }
195
+ var keys = Object.keys(arg);
196
+ var ks = keys.slice(0, 100);
197
+ if (deep > 3) {
198
+ var kvs = [];
199
+ if (keys.length > 0) kvs.push(`<gray>.. 共 ${keys.length} 个属性</gray>`);
200
+ }
201
+ else {
202
+ deepobjs.push(arg);
203
+ var kvs = ks.map(k => `${/[\:'"`\[\{\(\r\n\u2028\u2029]|^\s|\s$/.test(k) ? format(k, deep) : k}: ${format(arg[k], deep)}`);
204
+ deepobjs.pop();
205
+ if (keys.length > ks.length) kvs.push(`<gray>.. 其他 ${keys.length - ks.length} 个属性</gray>`);
206
+ }
207
+ var entry = '{';
208
+ if (arg.constructor && arg.constructor !== Object) entry = arg.constructor.name + entry;
209
+ return formatRows(arg, kvs, deep, entry, '}');
210
+ }
211
+ return String(arg);
212
+ };
213
+ var toLength = function (n, a = -1) {
214
+ n = String(n);
215
+ if (n.length < 2) {
216
+ n = '0' + n;
217
+ }
218
+ if (a === -1 && n.length < 3) {
219
+ n = '0' + n;
220
+ }
221
+ return n;
222
+ };
223
+
224
+ var formatDate = function () {
225
+ var year = this.getFullYear();
226
+ var month = this.getMonth() + 1;
227
+ var date = this.getDate();
228
+ var hours = this.getHours();
229
+ var minutes = this.getMinutes();
230
+ var seconds = this.getSeconds();
231
+ var milli = this.getMilliseconds();
232
+ milli = toLength(milli);
233
+ var offset = -this.getTimezoneOffset();
234
+ if (offset >= 0) {
235
+ offset = '+' + toLength(offset / 60 | 0, 0) + toLength(offset % 60, 0);
236
+ } else {
237
+ offset = '-' + toLength(-offset / 60 | 0, 0) + toLength(-offset % 60, 0);
238
+ }
239
+ return `${[year, month, date].map(toLength).join('-')} ${[hours, minutes, seconds].map(toLength).join(':')}.${milli} ${offset}`;
240
+ };
241
+
242
+ colored.time = function (date = new Date, str) {
243
+ write1(true, colors.BgGray + colors.FgWhite2 + formatDate.call(date) + str + colors.Reset);
244
+ };
245
+
97
246
  colored.type = function (...args) {
98
- write1(false, args.join(' '));
247
+ write1(false, args.map(a => format(a)).join(' '));
248
+ };
249
+ colored.line = function (...args) {
250
+ write1(true, args.map(a => format(a)).join(' '));
99
251
  };
100
252
  var _log = console.log;
101
253
  colored.log = function () {
102
- flush();
254
+ if (lastLogLength > 0) write1(false, '');
255
+ if (needNextLine) needNextLine = false;
103
256
  _log.apply(console, arguments);
104
257
  };
105
258
  colored.begin = function (c) {
106
259
  return write1(false, getColor(c));
107
260
  };
108
- var writeid = 0;
109
- var drop = lazy(function (dropid) {
110
- if (dropid === writeid) write1(false, "");
261
+ var drop = lazy(function () {
262
+ write(false, "");
111
263
  }, 160);
112
- colored.drop = function () {
113
- drop(++writeid);
114
- };
264
+ colored.drop = drop;
115
265
  colored.end = function () {
116
266
  return write1(false, colors.Reset);
117
267
  };
@@ -0,0 +1,55 @@
1
+ var console = require("./colored_console");
2
+ var data = [
3
+ { a: 1 },
4
+ { b: [2] },
5
+ { c: null },
6
+ class c { },
7
+ { 匿名函数: function () { } },
8
+ { "arraw function": e => { } },
9
+ { "function a(){}": function a() { } },
10
+ { "astar function": function* b() { } },
11
+ { "async function": async function* d() { } },
12
+ { "二层对象": { c: 2, "三层对象": { d: 3 } } },
13
+ {
14
+ "多属性节点": {
15
+ a: 1,
16
+ b: 2,
17
+ c: 3,
18
+ }
19
+ },
20
+ {
21
+ "多属性节点": {
22
+ "属性1": 1,
23
+ "属性2": 1,
24
+ "属性3": 1,
25
+ "属性4": 1,
26
+ "属性5": 1,
27
+ "属性6": 1,
28
+ "属性7": 1,
29
+ "属性8": 1,
30
+ "属性10": 1,
31
+ "属性11": 1,
32
+ "属性12": 1,
33
+ "属性13": 1,
34
+ "属性14": 1,
35
+ "属性15": 1,
36
+ "属性16": 1,
37
+ "属性17": 1,
38
+ }
39
+ },
40
+ {
41
+ "多属性节点": function () {
42
+ var a = {};
43
+ for (var cx = 1; cx <= 101; cx++) {
44
+ a["属性" + cx] = cx;
45
+ }
46
+ return a;
47
+ }()
48
+ },
49
+ /Abcdefg/gimuy,
50
+ new Date,
51
+ { "循环对象": null },
52
+ ];
53
+ data[data.length - 1].循环对象 = data[data.length - 1];
54
+ console.log(data);
55
+ console.line(data);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "efront",
3
- "version": "3.31.2",
3
+ "version": "3.32.0",
4
4
  "description": "简化前端开发,优化web性能",
5
5
  "main": "public/efront.js",
6
6
  "directories": {