@zwa73/utils 1.0.2 → 1.0.4
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.
- package/build.js +16 -0
- package/dist/UtilClass.d.ts +18 -10
- package/dist/UtilClass.js +40 -20
- package/dist/UtilCom.d.ts +10 -0
- package/dist/UtilCom.js +75 -0
- package/index.d.ts +1 -0
- package/index.js +1 -16
- package/package.json +3 -4
- package/release.bat +3 -0
- package/src/UtilClass.ts +63 -40
- package/src/UtilCom.ts +83 -0
- package/test.js +8 -2
- package/tsCompile.bat +2 -2
- package/tsCompileWatch.bat +2 -3
package/build.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
// 读取并更新包配置文件中的版本号
|
|
5
|
+
function updateVersion() {
|
|
6
|
+
const packagePath = path.join(__dirname, 'package.json');
|
|
7
|
+
const packageData = JSON.parse(fs.readFileSync(packagePath));
|
|
8
|
+
const version = packageData.version.split('.');
|
|
9
|
+
version[2] = parseInt(version[2]) + 1;
|
|
10
|
+
packageData.version = version.join('.');
|
|
11
|
+
fs.writeFileSync(packagePath, JSON.stringify(packageData, null, 2));
|
|
12
|
+
return packageData.version;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
updateVersion();
|
package/dist/UtilClass.d.ts
CHANGED
|
@@ -43,7 +43,7 @@ export declare class SList<T> {
|
|
|
43
43
|
*/
|
|
44
44
|
reverse(): SList<T>;
|
|
45
45
|
/**将SList转换为数组 */
|
|
46
|
-
toArray(): T
|
|
46
|
+
toArray(): Array<T>;
|
|
47
47
|
/**返回指定元素在数组中首次出现的位置
|
|
48
48
|
* @param value 目标元素
|
|
49
49
|
*/
|
|
@@ -155,8 +155,8 @@ export declare class SList<T> {
|
|
|
155
155
|
};
|
|
156
156
|
}
|
|
157
157
|
export declare class SIterator<T> {
|
|
158
|
-
_index
|
|
159
|
-
_list
|
|
158
|
+
private _index;
|
|
159
|
+
private _list;
|
|
160
160
|
constructor(list: SList<T>);
|
|
161
161
|
/**判断还有没有下一个元素
|
|
162
162
|
*/
|
|
@@ -212,15 +212,17 @@ export declare class SIterator<T> {
|
|
|
212
212
|
*/
|
|
213
213
|
addNext(obj: T): void;
|
|
214
214
|
}
|
|
215
|
-
export declare class SEntry<K
|
|
216
|
-
_key
|
|
217
|
-
_value
|
|
215
|
+
export declare class SEntry<K, V> {
|
|
216
|
+
private _key;
|
|
217
|
+
private _value;
|
|
218
218
|
constructor(key: K, value: V);
|
|
219
219
|
getKey(): K;
|
|
220
220
|
getValue(): V;
|
|
221
|
+
get key(): K;
|
|
222
|
+
get value(): V;
|
|
221
223
|
}
|
|
222
|
-
export declare class SHashMap<K
|
|
223
|
-
|
|
224
|
+
export declare class SHashMap<K, V> {
|
|
225
|
+
private _map;
|
|
224
226
|
constructor();
|
|
225
227
|
/**添加一个键值对
|
|
226
228
|
* @param entry 键值对
|
|
@@ -231,7 +233,7 @@ export declare class SHashMap<K extends struct, V> {
|
|
|
231
233
|
* @param key 键
|
|
232
234
|
* @returns 值
|
|
233
235
|
*/
|
|
234
|
-
get(key: K): V |
|
|
236
|
+
get(key: K): V | undefined;
|
|
235
237
|
/**添加一个键值对
|
|
236
238
|
* @param key 键
|
|
237
239
|
* @param value 值
|
|
@@ -263,7 +265,7 @@ export declare class SHashMap<K extends struct, V> {
|
|
|
263
265
|
* @param key 键
|
|
264
266
|
* @returns 删除的值
|
|
265
267
|
*/
|
|
266
|
-
remove(key: K): V;
|
|
268
|
+
remove(key: K): V | undefined;
|
|
267
269
|
/**清空哈希表
|
|
268
270
|
* @returns 自身
|
|
269
271
|
*/
|
|
@@ -320,5 +322,11 @@ export declare class SHashMap<K extends struct, V> {
|
|
|
320
322
|
* @returns 结果
|
|
321
323
|
*/
|
|
322
324
|
stat<O>(init: O, func: (accumulator: O, entry: SEntry<K, V>) => O): O;
|
|
325
|
+
[Symbol.iterator](): {
|
|
326
|
+
next(): {
|
|
327
|
+
value: SEntry<K, V> | null;
|
|
328
|
+
done: boolean;
|
|
329
|
+
};
|
|
330
|
+
};
|
|
323
331
|
}
|
|
324
332
|
export {};
|
package/dist/UtilClass.js
CHANGED
|
@@ -278,7 +278,7 @@ class SList {
|
|
|
278
278
|
}
|
|
279
279
|
exports.SList = SList;
|
|
280
280
|
class SIterator {
|
|
281
|
-
_index =
|
|
281
|
+
_index = -1;
|
|
282
282
|
_list;
|
|
283
283
|
constructor(list) {
|
|
284
284
|
this._list = list;
|
|
@@ -392,17 +392,24 @@ class SEntry {
|
|
|
392
392
|
getValue() {
|
|
393
393
|
return this._value;
|
|
394
394
|
}
|
|
395
|
+
//重载TypeScript操作符
|
|
396
|
+
get key() {
|
|
397
|
+
return this.getKey();
|
|
398
|
+
}
|
|
399
|
+
get value() {
|
|
400
|
+
return this._value;
|
|
401
|
+
}
|
|
395
402
|
}
|
|
396
403
|
exports.SEntry = SEntry;
|
|
397
404
|
class SHashMap {
|
|
398
|
-
|
|
405
|
+
_map = new Map();
|
|
399
406
|
constructor() { }
|
|
400
407
|
/**添加一个键值对
|
|
401
408
|
* @param entry 键值对
|
|
402
409
|
* @returns 自身
|
|
403
410
|
*/
|
|
404
411
|
putEntry(entry) {
|
|
405
|
-
this.
|
|
412
|
+
this._map.set(entry.getKey(), entry.getValue());
|
|
406
413
|
return this;
|
|
407
414
|
}
|
|
408
415
|
/**获取指定键的值
|
|
@@ -410,7 +417,7 @@ class SHashMap {
|
|
|
410
417
|
* @returns 值
|
|
411
418
|
*/
|
|
412
419
|
get(key) {
|
|
413
|
-
return this.
|
|
420
|
+
return this._map.get(key);
|
|
414
421
|
}
|
|
415
422
|
/**添加一个键值对
|
|
416
423
|
* @param key 键
|
|
@@ -418,7 +425,7 @@ class SHashMap {
|
|
|
418
425
|
* @returns 自身
|
|
419
426
|
*/
|
|
420
427
|
put(key, value) {
|
|
421
|
-
this.
|
|
428
|
+
this._map.set(key, value);
|
|
422
429
|
return this;
|
|
423
430
|
}
|
|
424
431
|
/**判断是否存在指定键
|
|
@@ -426,16 +433,15 @@ class SHashMap {
|
|
|
426
433
|
* @returns 是否存在
|
|
427
434
|
*/
|
|
428
435
|
has(key) {
|
|
429
|
-
return
|
|
436
|
+
return this._map.has(key);
|
|
430
437
|
}
|
|
431
438
|
/**获取所有键值对
|
|
432
439
|
* @returns 键值对列表
|
|
433
440
|
*/
|
|
434
441
|
entrys() {
|
|
435
442
|
let list = new SList();
|
|
436
|
-
for (
|
|
437
|
-
list.push(new SEntry(
|
|
438
|
-
}
|
|
443
|
+
for (const [key, value] of this._map)
|
|
444
|
+
list.push(new SEntry(key, value));
|
|
439
445
|
return list;
|
|
440
446
|
}
|
|
441
447
|
/**获取所有键
|
|
@@ -443,8 +449,9 @@ class SHashMap {
|
|
|
443
449
|
*/
|
|
444
450
|
keys() {
|
|
445
451
|
let list = new SList();
|
|
446
|
-
|
|
447
|
-
|
|
452
|
+
let it = this._map.keys();
|
|
453
|
+
for (let key of it)
|
|
454
|
+
list.push(key);
|
|
448
455
|
return list;
|
|
449
456
|
}
|
|
450
457
|
/**获取所有值
|
|
@@ -452,10 +459,9 @@ class SHashMap {
|
|
|
452
459
|
*/
|
|
453
460
|
values() {
|
|
454
461
|
let list = new SList();
|
|
455
|
-
|
|
456
|
-
|
|
462
|
+
let it = this._map.values();
|
|
463
|
+
for (let val of it)
|
|
457
464
|
list.push(val);
|
|
458
|
-
}
|
|
459
465
|
return list;
|
|
460
466
|
}
|
|
461
467
|
/**获取迭代器
|
|
@@ -469,22 +475,22 @@ class SHashMap {
|
|
|
469
475
|
* @returns 删除的值
|
|
470
476
|
*/
|
|
471
477
|
remove(key) {
|
|
472
|
-
let out = this.
|
|
473
|
-
|
|
478
|
+
let out = this._map.get(key);
|
|
479
|
+
this._map.delete(key);
|
|
474
480
|
return out;
|
|
475
481
|
}
|
|
476
482
|
/**清空哈希表
|
|
477
483
|
* @returns 自身
|
|
478
484
|
*/
|
|
479
485
|
clear() {
|
|
480
|
-
this.
|
|
486
|
+
this._map.clear();
|
|
481
487
|
return this;
|
|
482
488
|
}
|
|
483
489
|
/**判断哈希表是否为空
|
|
484
490
|
* @returns 是否为空
|
|
485
491
|
*/
|
|
486
492
|
isEmpty() {
|
|
487
|
-
return this.
|
|
493
|
+
return this.isEmpty();
|
|
488
494
|
}
|
|
489
495
|
/**判断是否存在指定值
|
|
490
496
|
* @param val 值
|
|
@@ -498,7 +504,7 @@ class SHashMap {
|
|
|
498
504
|
* @returns 是否存在
|
|
499
505
|
*/
|
|
500
506
|
containsKey(key) {
|
|
501
|
-
return this.
|
|
507
|
+
return this._map.has(key);
|
|
502
508
|
}
|
|
503
509
|
/**加载指定键的值,若不存在则添加默认值并返回默认值
|
|
504
510
|
* @param key 键
|
|
@@ -533,7 +539,7 @@ class SHashMap {
|
|
|
533
539
|
* @returns 大小(键的数量)
|
|
534
540
|
*/
|
|
535
541
|
size() {
|
|
536
|
-
return this.
|
|
542
|
+
return this._map.size;
|
|
537
543
|
}
|
|
538
544
|
/**对哈希表的每一个键值对进行加工,返回加工完成的键值对组成的新哈希表
|
|
539
545
|
* @param func 加工函数
|
|
@@ -574,6 +580,20 @@ class SHashMap {
|
|
|
574
580
|
stat(init, func) {
|
|
575
581
|
return this.entrys().stat(init, func);
|
|
576
582
|
}
|
|
583
|
+
//重载TypeScript操作符
|
|
584
|
+
[Symbol.iterator]() {
|
|
585
|
+
let it = this.iterator();
|
|
586
|
+
return {
|
|
587
|
+
next() {
|
|
588
|
+
if (it.hasNext()) {
|
|
589
|
+
return { value: it.next(), done: false };
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
return { value: null, done: true };
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
};
|
|
596
|
+
}
|
|
577
597
|
}
|
|
578
598
|
exports.SHashMap = SHashMap;
|
|
579
599
|
class SKVC {
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { JObject } from "./UtilInterfaces";
|
|
2
|
+
/**发送一个POST请求并接受数据
|
|
3
|
+
* Object ()
|
|
4
|
+
* @async
|
|
5
|
+
* @param {JObject} json - 数据对象
|
|
6
|
+
* @param {Object} options - 参数对象
|
|
7
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
8
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
9
|
+
*/
|
|
10
|
+
export declare function spost(json: JObject, options: Object, timeLimit?: number): Promise<JObject | null>;
|
package/dist/UtilCom.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.spost = void 0;
|
|
4
|
+
const https = require("https");
|
|
5
|
+
/**发送一个POST请求并接受数据
|
|
6
|
+
* Object ()
|
|
7
|
+
* @async
|
|
8
|
+
* @param {JObject} json - 数据对象
|
|
9
|
+
* @param {Object} options - 参数对象
|
|
10
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
11
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
12
|
+
*/
|
|
13
|
+
function spost(json, options, timeLimit = -1) {
|
|
14
|
+
//转换为毫秒
|
|
15
|
+
let hasTimeLimit = (timeLimit >= 10);
|
|
16
|
+
if (hasTimeLimit)
|
|
17
|
+
timeLimit *= 1000;
|
|
18
|
+
let jsonStr = JSON.stringify(json);
|
|
19
|
+
return new Promise(function (resolve, rejecte) {
|
|
20
|
+
let req = https.request(options, function (res) {
|
|
21
|
+
//请求超时
|
|
22
|
+
if (hasTimeLimit) {
|
|
23
|
+
res.setTimeout(timeLimit, function () {
|
|
24
|
+
//res.abort();
|
|
25
|
+
console.log("spost 接收反馈超时: " + timeLimit + " ms");
|
|
26
|
+
resolve(null);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
let resdata = "";
|
|
30
|
+
res.setEncoding('utf8');
|
|
31
|
+
res.on('data', function (chunk) {
|
|
32
|
+
//console.log(chunk);
|
|
33
|
+
resdata += chunk;
|
|
34
|
+
});
|
|
35
|
+
res.on('error', function (e) {
|
|
36
|
+
console.log("spost 接收反馈错误:" + e);
|
|
37
|
+
resolve(null);
|
|
38
|
+
});
|
|
39
|
+
res.on('end', function () {
|
|
40
|
+
if (resdata != "") {
|
|
41
|
+
try {
|
|
42
|
+
let obj = JSON.parse(resdata);
|
|
43
|
+
console.log("spost 接受信息:\r\n" + JSON.stringify(obj));
|
|
44
|
+
//console.log(obj);
|
|
45
|
+
resolve(obj);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
console.log("spost 接收反馈错误:" + e);
|
|
50
|
+
console.log("原始字符串:" + resdata);
|
|
51
|
+
resolve(null);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
console.log("spost 接收反馈错误: resdata 为空");
|
|
56
|
+
resolve(null);
|
|
57
|
+
return;
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
//请求超时
|
|
61
|
+
if (hasTimeLimit) {
|
|
62
|
+
req.setTimeout(timeLimit, function () {
|
|
63
|
+
console.log("spost 发送请求超时: " + timeLimit + " ms");
|
|
64
|
+
req.destroy();
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
req.on('error', function (e) {
|
|
68
|
+
console.log("spost 发送请求错误:" + e);
|
|
69
|
+
resolve(null);
|
|
70
|
+
});
|
|
71
|
+
req.write(jsonStr);
|
|
72
|
+
req.end();
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
exports.spost = spost;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist';
|
package/index.js
CHANGED
|
@@ -1,16 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const baseUrl = "."; // 项目根目录
|
|
4
|
-
const paths = {
|
|
5
|
-
"@/*" : ["./*"] ,
|
|
6
|
-
"@/src/*" : ["./dist/*"] , // 将 @ 映射到 dist 目录
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
tsconfigPaths.register({
|
|
11
|
-
baseUrl,
|
|
12
|
-
paths,
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
const dist = require("./dist");
|
|
16
|
-
module.exports = dist;
|
|
1
|
+
module.exports = require("./dist");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwa73/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "my utils",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,7 +18,6 @@
|
|
|
18
18
|
"esm-resolve": "^1.0.8",
|
|
19
19
|
"html-entities": "^2.3.3",
|
|
20
20
|
"http-proxy-agent": "^5.0.0",
|
|
21
|
-
"https-proxy-agent": "^5.0.1"
|
|
22
|
-
"tsconfig-paths": "^4.2.0"
|
|
21
|
+
"https-proxy-agent": "^5.0.1"
|
|
23
22
|
}
|
|
24
|
-
}
|
|
23
|
+
}
|
package/release.bat
CHANGED
package/src/UtilClass.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { deepClone } from "./UtilFunctions";
|
|
2
|
-
|
|
3
1
|
type struct = number|string;
|
|
4
2
|
|
|
5
|
-
export class SList<T>{
|
|
3
|
+
export class SList<T> {
|
|
6
4
|
private _arr:Array<T>;
|
|
7
5
|
constructor(obj?:Array<T>|number){
|
|
8
6
|
if(typeof obj == 'number')
|
|
@@ -81,7 +79,7 @@ export class SList<T>{
|
|
|
81
79
|
}
|
|
82
80
|
|
|
83
81
|
/**将SList转换为数组 */
|
|
84
|
-
toArray():T
|
|
82
|
+
toArray():Array<T>{
|
|
85
83
|
return ([] as T[]).concat(this._arr);
|
|
86
84
|
}
|
|
87
85
|
|
|
@@ -309,32 +307,32 @@ export class SList<T>{
|
|
|
309
307
|
}
|
|
310
308
|
|
|
311
309
|
export class SIterator<T>{
|
|
312
|
-
_index
|
|
313
|
-
_list:SList<T>;
|
|
310
|
+
private _index=-1;
|
|
311
|
+
private _list:SList<T>;
|
|
314
312
|
constructor(list:SList<T>){
|
|
315
313
|
this._list = list;
|
|
316
314
|
}
|
|
317
315
|
/**判断还有没有下一个元素
|
|
318
316
|
*/
|
|
319
|
-
|
|
317
|
+
hasNext(): boolean {
|
|
320
318
|
return this._index < this.size() - 1 && this._index >= -1;
|
|
321
319
|
}
|
|
322
320
|
|
|
323
321
|
/**判断当前下标有无元素
|
|
324
322
|
*/
|
|
325
|
-
|
|
323
|
+
hasCurr(): boolean {
|
|
326
324
|
return this._index < this.size() && this._index >= 0;
|
|
327
325
|
}
|
|
328
326
|
|
|
329
327
|
/**判断还有没有上一个元素
|
|
330
328
|
*/
|
|
331
|
-
|
|
329
|
+
hasPre(): boolean {
|
|
332
330
|
return this._index < this.size() + 1 && this._index >= 1;
|
|
333
331
|
}
|
|
334
332
|
|
|
335
333
|
/**返回下一个下标指向的数组内成员,然后下标自加1
|
|
336
334
|
*/
|
|
337
|
-
|
|
335
|
+
next(): T {
|
|
338
336
|
if (this.hasNext())
|
|
339
337
|
return this._list.get(++this._index);
|
|
340
338
|
else
|
|
@@ -343,7 +341,7 @@ export class SIterator<T>{
|
|
|
343
341
|
|
|
344
342
|
/**返回上一个下标指向的数组内成员,然后下标自减1
|
|
345
343
|
*/
|
|
346
|
-
|
|
344
|
+
pre(): T {
|
|
347
345
|
if (this.hasPre())
|
|
348
346
|
return this._list.get(--this._index);
|
|
349
347
|
else
|
|
@@ -352,7 +350,7 @@ export class SIterator<T>{
|
|
|
352
350
|
|
|
353
351
|
/**返回当前下标指向的数组内成员
|
|
354
352
|
*/
|
|
355
|
-
|
|
353
|
+
curr(): T {
|
|
356
354
|
if (this.hasCurr())
|
|
357
355
|
return this._list.get(this._index);
|
|
358
356
|
else
|
|
@@ -424,9 +422,10 @@ export class SIterator<T>{
|
|
|
424
422
|
}
|
|
425
423
|
}
|
|
426
424
|
|
|
427
|
-
export class SEntry<K
|
|
428
|
-
_key:K;
|
|
429
|
-
_value:V;
|
|
425
|
+
export class SEntry<K,V>{
|
|
426
|
+
private _key:K;
|
|
427
|
+
private _value:V;
|
|
428
|
+
|
|
430
429
|
constructor(key:K,value:V){
|
|
431
430
|
this._key = key;
|
|
432
431
|
this._value = value;
|
|
@@ -437,16 +436,26 @@ export class SEntry<K extends struct,V>{
|
|
|
437
436
|
getValue(){
|
|
438
437
|
return this._value;
|
|
439
438
|
}
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
//重载TypeScript操作符
|
|
442
|
+
get key(): K {
|
|
443
|
+
return this.getKey();
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
get value(): V {
|
|
447
|
+
return this._value;
|
|
448
|
+
}
|
|
440
449
|
}
|
|
441
|
-
export class SHashMap<K
|
|
442
|
-
|
|
450
|
+
export class SHashMap<K,V>{
|
|
451
|
+
private _map:Map<K,V> = new Map();
|
|
443
452
|
constructor(){}
|
|
444
453
|
/**添加一个键值对
|
|
445
454
|
* @param entry 键值对
|
|
446
455
|
* @returns 自身
|
|
447
456
|
*/
|
|
448
457
|
putEntry(entry: SEntry<K, V>): SHashMap<K,V> {
|
|
449
|
-
this.
|
|
458
|
+
this._map.set(entry.getKey(),entry.getValue());
|
|
450
459
|
return this;
|
|
451
460
|
}
|
|
452
461
|
|
|
@@ -454,8 +463,8 @@ export class SHashMap<K extends struct,V>{
|
|
|
454
463
|
* @param key 键
|
|
455
464
|
* @returns 值
|
|
456
465
|
*/
|
|
457
|
-
get(key: K): V |
|
|
458
|
-
return this.
|
|
466
|
+
get(key: K): V | undefined {
|
|
467
|
+
return this._map.get(key);
|
|
459
468
|
}
|
|
460
469
|
|
|
461
470
|
/**添加一个键值对
|
|
@@ -464,7 +473,7 @@ export class SHashMap<K extends struct,V>{
|
|
|
464
473
|
* @returns 自身
|
|
465
474
|
*/
|
|
466
475
|
put(key: K, value: V): SHashMap<K,V> {
|
|
467
|
-
this.
|
|
476
|
+
this._map.set(key, value);
|
|
468
477
|
return this;
|
|
469
478
|
}
|
|
470
479
|
|
|
@@ -473,7 +482,7 @@ export class SHashMap<K extends struct,V>{
|
|
|
473
482
|
* @returns 是否存在
|
|
474
483
|
*/
|
|
475
484
|
has(key: K): boolean {
|
|
476
|
-
return
|
|
485
|
+
return this._map.has(key);
|
|
477
486
|
}
|
|
478
487
|
|
|
479
488
|
/**获取所有键值对
|
|
@@ -481,9 +490,8 @@ export class SHashMap<K extends struct,V>{
|
|
|
481
490
|
*/
|
|
482
491
|
entrys(): SList<SEntry<K, V>> {
|
|
483
492
|
let list = new SList<SEntry<K, V>>();
|
|
484
|
-
for (
|
|
485
|
-
list.push(new SEntry(
|
|
486
|
-
}
|
|
493
|
+
for (const [key, value] of this._map)
|
|
494
|
+
list.push(new SEntry(key, value));
|
|
487
495
|
return list;
|
|
488
496
|
}
|
|
489
497
|
|
|
@@ -492,8 +500,9 @@ export class SHashMap<K extends struct,V>{
|
|
|
492
500
|
*/
|
|
493
501
|
keys(): SList<K> {
|
|
494
502
|
let list = new SList<K>();
|
|
495
|
-
|
|
496
|
-
|
|
503
|
+
let it = this._map.keys();
|
|
504
|
+
for(let key of it)
|
|
505
|
+
list.push(key);
|
|
497
506
|
return list;
|
|
498
507
|
}
|
|
499
508
|
|
|
@@ -502,11 +511,10 @@ export class SHashMap<K extends struct,V>{
|
|
|
502
511
|
*/
|
|
503
512
|
values(): SList<V> {
|
|
504
513
|
let list = new SList<V>();
|
|
505
|
-
|
|
506
|
-
let val
|
|
514
|
+
let it = this._map.values();
|
|
515
|
+
for(let val of it)
|
|
507
516
|
list.push(val);
|
|
508
|
-
|
|
509
|
-
return list;
|
|
517
|
+
return list;
|
|
510
518
|
}
|
|
511
519
|
|
|
512
520
|
/**获取迭代器
|
|
@@ -520,9 +528,9 @@ export class SHashMap<K extends struct,V>{
|
|
|
520
528
|
* @param key 键
|
|
521
529
|
* @returns 删除的值
|
|
522
530
|
*/
|
|
523
|
-
remove(key: K): V {
|
|
524
|
-
let out = this.
|
|
525
|
-
|
|
531
|
+
remove(key: K): V|undefined {
|
|
532
|
+
let out = this._map.get(key);
|
|
533
|
+
this._map.delete(key);
|
|
526
534
|
return out;
|
|
527
535
|
}
|
|
528
536
|
|
|
@@ -530,7 +538,7 @@ export class SHashMap<K extends struct,V>{
|
|
|
530
538
|
* @returns 自身
|
|
531
539
|
*/
|
|
532
540
|
clear(): SHashMap<K, V> {
|
|
533
|
-
this.
|
|
541
|
+
this._map.clear();
|
|
534
542
|
return this;
|
|
535
543
|
}
|
|
536
544
|
|
|
@@ -538,7 +546,7 @@ export class SHashMap<K extends struct,V>{
|
|
|
538
546
|
* @returns 是否为空
|
|
539
547
|
*/
|
|
540
548
|
isEmpty(): boolean {
|
|
541
|
-
return this.
|
|
549
|
+
return this.isEmpty();
|
|
542
550
|
}
|
|
543
551
|
|
|
544
552
|
/**判断是否存在指定值
|
|
@@ -554,7 +562,7 @@ export class SHashMap<K extends struct,V>{
|
|
|
554
562
|
* @returns 是否存在
|
|
555
563
|
*/
|
|
556
564
|
containsKey(key: K): boolean {
|
|
557
|
-
return this.
|
|
565
|
+
return this._map.has(key);
|
|
558
566
|
}
|
|
559
567
|
|
|
560
568
|
/**加载指定键的值,若不存在则添加默认值并返回默认值
|
|
@@ -564,7 +572,7 @@ export class SHashMap<K extends struct,V>{
|
|
|
564
572
|
*/
|
|
565
573
|
load(key: K, def: V): V {
|
|
566
574
|
if (this.containsKey(key))
|
|
567
|
-
return this.get(key)
|
|
575
|
+
return this.get(key)!;
|
|
568
576
|
this.put(key, def);
|
|
569
577
|
return def;
|
|
570
578
|
}
|
|
@@ -592,7 +600,7 @@ export class SHashMap<K extends struct,V>{
|
|
|
592
600
|
* @returns 大小(键的数量)
|
|
593
601
|
*/
|
|
594
602
|
size(): number {
|
|
595
|
-
return this.
|
|
603
|
+
return this._map.size;
|
|
596
604
|
}
|
|
597
605
|
/**对哈希表的每一个键值对进行加工,返回加工完成的键值对组成的新哈希表
|
|
598
606
|
* @param func 加工函数
|
|
@@ -637,10 +645,25 @@ export class SHashMap<K extends struct,V>{
|
|
|
637
645
|
return this.entrys().stat(init, func);
|
|
638
646
|
}
|
|
639
647
|
|
|
648
|
+
|
|
649
|
+
//重载TypeScript操作符
|
|
650
|
+
[Symbol.iterator]() {
|
|
651
|
+
let it = this.iterator();
|
|
652
|
+
|
|
653
|
+
return {
|
|
654
|
+
next(): { value: SEntry<K,V>|null; done: boolean } {
|
|
655
|
+
if (it.hasNext()) {
|
|
656
|
+
return { value: it.next(), done: false };
|
|
657
|
+
} else {
|
|
658
|
+
return { value: null, done: true };
|
|
659
|
+
}
|
|
660
|
+
},
|
|
661
|
+
};
|
|
662
|
+
}
|
|
640
663
|
}
|
|
641
664
|
|
|
642
665
|
class SKVC{
|
|
643
|
-
stringMap:SHashMap<string,string> = new SHashMap();
|
|
666
|
+
private stringMap:SHashMap<string,string> = new SHashMap();
|
|
644
667
|
|
|
645
668
|
constructor(){}
|
|
646
669
|
}
|
package/src/UtilCom.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { JObject } from "./UtilInterfaces";
|
|
2
|
+
import * as https from 'https';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/**发送一个POST请求并接受数据
|
|
6
|
+
* Object ()
|
|
7
|
+
* @async
|
|
8
|
+
* @param {JObject} json - 数据对象
|
|
9
|
+
* @param {Object} options - 参数对象
|
|
10
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
11
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
12
|
+
*/
|
|
13
|
+
export function spost(json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
|
|
14
|
+
//转换为毫秒
|
|
15
|
+
let hasTimeLimit = (timeLimit>=10);
|
|
16
|
+
if(hasTimeLimit)
|
|
17
|
+
timeLimit*=1000
|
|
18
|
+
|
|
19
|
+
let jsonStr = JSON.stringify(json);
|
|
20
|
+
|
|
21
|
+
return new Promise(function(resolve, rejecte){
|
|
22
|
+
let req = https.request(options, function(res){
|
|
23
|
+
//请求超时
|
|
24
|
+
if(hasTimeLimit){
|
|
25
|
+
res.setTimeout(timeLimit, function() {
|
|
26
|
+
//res.abort();
|
|
27
|
+
console.log("spost 接收反馈超时: "+timeLimit+" ms");
|
|
28
|
+
resolve(null);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let resdata = "";
|
|
33
|
+
res.setEncoding('utf8');
|
|
34
|
+
res.on('data',function(chunk){
|
|
35
|
+
//console.log(chunk);
|
|
36
|
+
resdata+=chunk;
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
res.on('error',function(e){
|
|
40
|
+
console.log("spost 接收反馈错误:"+e);
|
|
41
|
+
resolve(null);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
res.on('end',function(){
|
|
45
|
+
if(resdata!=""){
|
|
46
|
+
try{
|
|
47
|
+
let obj = JSON.parse(resdata);
|
|
48
|
+
console.log("spost 接受信息:\r\n"+JSON.stringify(obj));
|
|
49
|
+
//console.log(obj);
|
|
50
|
+
resolve(obj);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
catch(e){
|
|
54
|
+
console.log("spost 接收反馈错误:"+e);
|
|
55
|
+
console.log("原始字符串:"+resdata);
|
|
56
|
+
resolve(null);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
console.log("spost 接收反馈错误: resdata 为空");
|
|
61
|
+
resolve(null);
|
|
62
|
+
return;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
//请求超时
|
|
68
|
+
if(hasTimeLimit){
|
|
69
|
+
req.setTimeout(timeLimit, function() {
|
|
70
|
+
console.log("spost 发送请求超时: "+timeLimit+" ms");
|
|
71
|
+
req.destroy();
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
req.on('error', function(e) {
|
|
76
|
+
console.log("spost 发送请求错误:"+e);
|
|
77
|
+
resolve(null);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
req.write(jsonStr);
|
|
81
|
+
req.end();
|
|
82
|
+
});
|
|
83
|
+
}
|
package/test.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
let {SList} = require('./dist');
|
|
1
|
+
let {SList,SHashMap,SEntry} = require('./dist');
|
|
2
2
|
|
|
3
3
|
let slist = new SList([1,2,3,3,4,5,6]);
|
|
4
4
|
slist.each(val => console.log(val));
|
|
5
5
|
for(let v of slist)
|
|
6
|
-
console.log(v)
|
|
6
|
+
console.log(v)
|
|
7
|
+
|
|
8
|
+
let map = new SHashMap();
|
|
9
|
+
map.put("123",456)
|
|
10
|
+
map.put("789",111)
|
|
11
|
+
for(let {key,value} of map)
|
|
12
|
+
console.log(key,value)
|
package/tsCompile.bat
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
tsc
|
|
1
|
+
call tsc
|
|
2
|
+
call tsc-alias
|
|
3
3
|
pause
|
package/tsCompileWatch.bat
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
tsc -w
|
|
3
|
-
pause
|
|
1
|
+
start tsc-alias -w
|
|
2
|
+
start tsc -w
|