@btc-vision/transaction 1.7.3 → 1.7.5

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,10 +1,8 @@
1
- import { i32 } from '../utils/types.js';
2
-
3
1
  export class Map<K, V> {
4
2
  protected _keys: K[] = [];
5
- protected _values: V[] = [];
3
+ protected _values: Record<string, V> = {};
6
4
 
7
- public get size(): i32 {
5
+ public get size(): number {
8
6
  return this._keys.length;
9
7
  }
10
8
 
@@ -13,66 +11,77 @@ export class Map<K, V> {
13
11
  }
14
12
 
15
13
  public *values(): IterableIterator<V> {
16
- yield* this._values;
14
+ for (const key of this._keys) {
15
+ yield this._values[this.keyToString(key)];
16
+ }
17
17
  }
18
18
 
19
19
  public *entries(): IterableIterator<[K, V]> {
20
- for (let i: i32 = 0; i < this._keys.length; i++) {
21
- yield [this._keys[i], this._values[i]];
20
+ for (const key of this._keys) {
21
+ yield [key, this._values[this.keyToString(key)]];
22
22
  }
23
23
  }
24
24
 
25
25
  public set(key: K, value: V): void {
26
- const index: i32 = this.indexOf(key);
27
- if (index == -1) {
26
+ const keyStr = this.keyToString(key);
27
+ if (!this.has(key)) {
28
28
  this._keys.push(key);
29
- this._values.push(value);
30
- } else {
31
- this._values[index] = value;
32
29
  }
30
+
31
+ this._values[keyStr] = value;
33
32
  }
34
33
 
35
- public indexOf(key: K): i32 {
36
- for (let i: i32 = 0; i < this._keys.length; i++) {
37
- if (this._keys[i] == key) {
34
+ public indexOf(key: K): number {
35
+ for (let i = 0; i < this._keys.length; i++) {
36
+ if (this._keys[i] === key) {
38
37
  return i;
39
38
  }
40
39
  }
41
-
42
40
  return -1;
43
41
  }
44
42
 
45
43
  public get(key: K): V | undefined {
46
- const index: i32 = this.indexOf(key);
47
- if (index == -1) {
48
- return undefined;
49
- }
50
- return this._values[index];
44
+ return this._values[this.keyToString(key)];
51
45
  }
52
46
 
53
47
  public has(key: K): boolean {
54
- return this.indexOf(key) != -1;
48
+ return Object.prototype.hasOwnProperty.call(this._values, this.keyToString(key));
55
49
  }
56
50
 
57
51
  public delete(key: K): boolean {
58
- const index: i32 = this.indexOf(key);
59
- if (index == -1) {
52
+ const index = this.indexOf(key);
53
+ if (index === -1) {
60
54
  return false;
61
55
  }
62
56
 
57
+ const keyStr = this.keyToString(key);
63
58
  this._keys.splice(index, 1);
64
- this._values.splice(index, 1);
59
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
60
+ delete this._values[keyStr];
65
61
  return true;
66
62
  }
67
63
 
68
64
  public clear(): void {
69
65
  this._keys = [];
70
- this._values = [];
66
+ this._values = {};
71
67
  }
72
68
 
73
69
  *[Symbol.iterator](): IterableIterator<[K, V]> {
74
- for (let i: i32 = 0; i < this._keys.length; i++) {
75
- yield [this._keys[i], this._values[i]];
70
+ for (const key of this._keys) {
71
+ yield [key, this._values[this.keyToString(key)]];
72
+ }
73
+ }
74
+
75
+ private keyToString(key: K): string {
76
+ if (typeof key === 'string') {
77
+ return key;
78
+ }
79
+ if (typeof key === 'number' || typeof key === 'boolean') {
80
+ return String(key);
81
+ }
82
+ if (typeof key === 'object' && key !== null) {
83
+ return JSON.stringify(key);
76
84
  }
85
+ return String(key);
77
86
  }
78
87
  }
package/src/opnet.ts CHANGED
@@ -126,6 +126,7 @@ export * from './keypair/Secp256k1PointDeriver.js';
126
126
  export * from './transaction/ContractAddress.js';
127
127
 
128
128
  export * from './deterministic/Map.js';
129
+ export * from './deterministic/CustomMap.js';
129
130
 
130
131
  declare global {
131
132
  interface Window {