@leofcoin/codec-format-interface 1.4.3 → 1.5.1

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,123 +0,0 @@
1
- import bs32 from '@vandeurenglenn/base32';
2
- import bs58 from '@vandeurenglenn/base58';
3
- import isHex from '@vandeurenglenn/is-hex';
4
-
5
- export default class BasicInterface {
6
- handleDecode() {
7
- if (!this.decode) throw new Error('bad implementation: needs decode func')
8
- return this.decode()
9
- }
10
-
11
- handleEncode() {
12
- if (!this.encode) throw new Error('bad implementation: needs encode func')
13
- return this.encode()
14
- }
15
- isHex(string) {
16
- return isHex(string)
17
- }
18
- isBase32(string) {
19
- return bs32.isBase32(string)
20
- }
21
- isBase58(string) {
22
- return bs58.isBase58(string)
23
- }
24
- /**
25
- * @param {String} encoded
26
- */
27
- fromBs32(encoded) {
28
- this.encoded = bs32.decode(encoded)
29
- return this.handleDecode()
30
- }
31
-
32
- /**
33
- * @param {String} encoded
34
- */
35
- fromBs58(encoded) {
36
- this.encoded = bs58.decode(encoded)
37
- return this.handleDecode()
38
- }
39
-
40
- async toArray() {
41
- const array = []
42
- for await (const value of this.encoded.values()) {
43
- array.push(value)
44
- }
45
- return array
46
- }
47
-
48
- fromString(string) {
49
- this.encoded = new Uint8Array(string.split(','))
50
- return this.handleDecode()
51
- }
52
-
53
- fromArray(array) {
54
- this.encoded = new Uint8Array([...array])
55
- return this.handleDecode()
56
- }
57
-
58
- /**
59
- * @param {Buffer} encoded
60
- */
61
- fromEncoded(encoded) {
62
- this.encoded = encoded
63
- return this.handleDecode()
64
- }
65
-
66
- /**
67
- * @param {String} encoded
68
- */
69
- fromHex(encoded) {
70
- this.encoded = Buffer.from(encoded, 'hex')
71
- return this.handleDecode()
72
- }
73
-
74
- async toString(encoding = 'utf8') {
75
- if (!this.encoded) await this.handleEncode()
76
- return this.encoded.toString(encoding)
77
- }
78
-
79
- /**
80
- * @return {String} encoded
81
- */
82
- toHex() {
83
- return this.toString('hex')
84
- }
85
-
86
- /**
87
- * @return {String} encoded
88
- */
89
- async toBs32() {
90
- if (!this.encoded) await this.handleEncode()
91
- return bs32.encode(this.encoded)
92
- }
93
-
94
- /**
95
- * @return {String} encoded
96
- */
97
- async toBs58() {
98
- if (!this.encoded) await this.handleEncode()
99
- return bs58.encode(this.encoded)
100
- }
101
-
102
- /**
103
- * @param {Object} data
104
- */
105
- create(data) {
106
- const decoded = {}
107
- if (this.keys?.length > 0) {
108
- for (const key of this.keys) {
109
- Object.defineProperties(decoded, {
110
- [key]: {
111
- enumerable: true,
112
- configurable: true,
113
- set: (val) => value = data[key],
114
- get: () => data[key]
115
- }
116
- })
117
- }
118
-
119
- this.decoded = decoded
120
- return this.encode()
121
- }
122
- }
123
- }