@zwa73/utils 1.0.2 → 1.0.3

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 ADDED
@@ -0,0 +1,19 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const tscAlias = require('tsc-alias');
4
+
5
+
6
+ // 读取并更新包配置文件中的版本号
7
+ function updateVersion() {
8
+ const packagePath = path.join(__dirname, 'package.json');
9
+ const packageData = JSON.parse(fs.readFileSync(packagePath));
10
+ const version = packageData.version.split('.');
11
+ version[2] = parseInt(version[2]) + 1;
12
+ packageData.version = version.join('.');
13
+ fs.writeFileSync(packagePath, JSON.stringify(packageData, null, 2));
14
+ return packageData.version;
15
+ }
16
+
17
+
18
+ tscAlias.replaceTscAliasPaths();
19
+ updateVersion();
@@ -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: number;
159
- _list: SList<T>;
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 extends struct, V> {
216
- _key: K;
217
- _value: V;
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 extends struct, V> {
223
- _obj: Record<K, V>;
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 | null;
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 = 0;
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
- _obj = {};
405
+ _map = new Map();
399
406
  constructor() { }
400
407
  /**添加一个键值对
401
408
  * @param entry 键值对
402
409
  * @returns 自身
403
410
  */
404
411
  putEntry(entry) {
405
- this._obj[entry.getKey()] = entry.getValue();
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._obj[key];
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._obj[key] = value;
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 key in this._obj;
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 (let k in this._obj) {
437
- list.push(new SEntry(k, this._obj[k]));
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
- for (let k in this._obj)
447
- list.push(k);
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
- for (let k in this._obj) {
456
- let val = this._obj[k];
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._obj[key];
473
- delete this._obj[key];
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._obj = {};
486
+ this._map.clear();
481
487
  return this;
482
488
  }
483
489
  /**判断哈希表是否为空
484
490
  * @returns 是否为空
485
491
  */
486
492
  isEmpty() {
487
- return this.keys().isEmpty();
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.keys().contains(key);
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.keys().size();
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 {
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist';
package/index.js CHANGED
@@ -1,16 +1 @@
1
- const tsconfigPaths = require("tsconfig-paths");
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.2",
3
+ "version": "1.0.3",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,4 +21,4 @@
21
21
  "https-proxy-agent": "^5.0.1",
22
22
  "tsconfig-paths": "^4.2.0"
23
23
  }
24
- }
24
+ }
package/release.bat CHANGED
@@ -1,2 +1,4 @@
1
+ call tsc
2
+ node build.js
1
3
  npm publish --access public
2
4
  pause
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=0;
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
- public hasNext(): boolean {
317
+ hasNext(): boolean {
320
318
  return this._index < this.size() - 1 && this._index >= -1;
321
319
  }
322
320
 
323
321
  /**判断当前下标有无元素
324
322
  */
325
- public hasCurr(): boolean {
323
+ hasCurr(): boolean {
326
324
  return this._index < this.size() && this._index >= 0;
327
325
  }
328
326
 
329
327
  /**判断还有没有上一个元素
330
328
  */
331
- public hasPre(): boolean {
329
+ hasPre(): boolean {
332
330
  return this._index < this.size() + 1 && this._index >= 1;
333
331
  }
334
332
 
335
333
  /**返回下一个下标指向的数组内成员,然后下标自加1
336
334
  */
337
- public next(): T {
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
- public pre(): T {
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
- public curr(): T {
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 extends struct,V>{
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 extends struct,V>{
442
- _obj:Record<K,V> = {} as Record<K,V>;
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._obj[entry.getKey()] = entry.getValue();
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 | null {
458
- return this._obj[key];
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._obj[key] = value;
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 key in this._obj;
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 (let k in this._obj) {
485
- list.push(new SEntry(k, this._obj[k]));
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
- for (let k in this._obj)
496
- list.push(k);
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
- for (let k in this._obj) {
506
- let val = this._obj[k];
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._obj[key];
525
- delete this._obj[key];
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._obj = {} as Record<K, V>;
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.keys().isEmpty();
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.keys().contains(key);
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) as V;
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.keys().size();
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/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)