@novasamatech/scale 0.5.3-0 → 0.5.4-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.
- package/README.md +174 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,180 @@
|
|
|
1
|
-
# @novasamatech/
|
|
1
|
+
# @novasamatech/scale
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
3
|
+
Additional SCALE codecs based on scale-ts library.
|
|
6
4
|
|
|
7
5
|
## Installation
|
|
8
6
|
|
|
9
7
|
```shell
|
|
10
|
-
npm install @novasamatech/
|
|
8
|
+
npm install @novasamatech/scale --save -E
|
|
11
9
|
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Hex
|
|
14
|
+
|
|
15
|
+
Wrapper around Bytes codec with mapping to hex strings.
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { Hex, toHex, fromHex } from '@novasamatech/scale';
|
|
19
|
+
|
|
20
|
+
// Create a Hex codec (byte array size, not hex string length)
|
|
21
|
+
const hexCodec = Hex(32); // 32 bytes
|
|
22
|
+
|
|
23
|
+
// Encode to hex string
|
|
24
|
+
const encoded = hexCodec.enc(new Uint8Array([1, 2, 3, 4]));
|
|
25
|
+
// Result: "0x01020304"
|
|
26
|
+
|
|
27
|
+
// Decode from hex string
|
|
28
|
+
const decoded = hexCodec.dec("0x01020304");
|
|
29
|
+
// Result: Uint8Array([1, 2, 3, 4])
|
|
30
|
+
|
|
31
|
+
// Helper functions
|
|
32
|
+
const hexString = toHex(new Uint8Array([255, 0, 128]));
|
|
33
|
+
// Result: "0xff0080"
|
|
34
|
+
|
|
35
|
+
const bytes = fromHex("0xff0080");
|
|
36
|
+
// Result: Uint8Array([255, 0, 128])
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Nullable
|
|
40
|
+
|
|
41
|
+
Codec for nullable values (null/undefined mapping).
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { Nullable } from '@novasamatech/scale';
|
|
45
|
+
import { u32 } from 'scale-ts';
|
|
46
|
+
|
|
47
|
+
const nullableCodec = Nullable(u32);
|
|
48
|
+
|
|
49
|
+
// Encode null as undefined
|
|
50
|
+
const encoded = nullableCodec.enc(null);
|
|
51
|
+
|
|
52
|
+
// Decode undefined as null
|
|
53
|
+
const decoded = nullableCodec.dec(encoded);
|
|
54
|
+
// Result: null
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Status
|
|
58
|
+
|
|
59
|
+
Enum without values - maps a list of constants to u8 indices.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { Status } from '@novasamatech/scale';
|
|
63
|
+
|
|
64
|
+
const ConnectionStatus = Status('Connecting', 'Connected', 'Disconnected');
|
|
65
|
+
|
|
66
|
+
// Encode status to u8
|
|
67
|
+
const encoded = ConnectionStatus.enc('Connected');
|
|
68
|
+
// Result: 1
|
|
69
|
+
|
|
70
|
+
// Decode u8 to status
|
|
71
|
+
const decoded = ConnectionStatus.dec(1);
|
|
72
|
+
// Result: 'Connected'
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Enum
|
|
76
|
+
|
|
77
|
+
Type-safe enum codec wrapper.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { Enum, enumValue, isEnumVariant, assertEnumVariant } from '@novasamatech/scale';
|
|
81
|
+
import { u32, str } from 'scale-ts';
|
|
82
|
+
|
|
83
|
+
const MyEnum = Enum({
|
|
84
|
+
Text: str,
|
|
85
|
+
Number: u32,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Create enum values
|
|
89
|
+
const textValue = enumValue('Text', 'hello');
|
|
90
|
+
const numberValue = enumValue('Number', 42);
|
|
91
|
+
|
|
92
|
+
// Encode/decode
|
|
93
|
+
const encoded = MyEnum.enc(textValue);
|
|
94
|
+
const decoded = MyEnum.dec(encoded);
|
|
95
|
+
|
|
96
|
+
// Check variant type
|
|
97
|
+
if (isEnumVariant(decoded, 'Text')) {
|
|
98
|
+
console.log(decoded.value); // Type is string
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Assert variant type
|
|
102
|
+
assertEnumVariant(decoded, 'Number', 'Expected Number variant');
|
|
103
|
+
console.log(decoded.value); // Type is number
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Err
|
|
107
|
+
|
|
108
|
+
Custom error codec with typed payloads.
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import { Err } from '@novasamatech/scale';
|
|
112
|
+
import { u32 } from 'scale-ts';
|
|
113
|
+
|
|
114
|
+
const InvalidId = Err(
|
|
115
|
+
'InvalidId',
|
|
116
|
+
u32,
|
|
117
|
+
(id) => `Invalid ID: ${id}`
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
// Create error instance
|
|
121
|
+
const error = new InvalidId(42);
|
|
122
|
+
console.log(error.name); // 'InvalidId'
|
|
123
|
+
console.log(error.message); // 'Invalid ID: 42'
|
|
124
|
+
console.log(error.payload); // 42
|
|
125
|
+
|
|
126
|
+
// Encode/decode errors
|
|
127
|
+
const encoded = InvalidId.enc(error);
|
|
128
|
+
const decoded = InvalidId.dec(encoded);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### ErrEnum
|
|
132
|
+
|
|
133
|
+
Enum of error types.
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
import { ErrEnum } from '@novasamatech/scale';
|
|
137
|
+
import { u32, str } from 'scale-ts';
|
|
138
|
+
|
|
139
|
+
const ApiError = ErrEnum('ApiError', {
|
|
140
|
+
NotFound: [u32, (id) => `Resource ${id} not found`],
|
|
141
|
+
InvalidInput: [str, (msg) => `Invalid input: ${msg}`],
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// Create error instances
|
|
145
|
+
const notFoundError = new ApiError.NotFound(123);
|
|
146
|
+
const invalidInputError = new ApiError.InvalidInput('bad data');
|
|
147
|
+
|
|
148
|
+
// Encode/decode
|
|
149
|
+
const encoded = ApiError.enc(notFoundError);
|
|
150
|
+
const decoded = ApiError.dec(encoded);
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Result Helpers
|
|
154
|
+
|
|
155
|
+
Helper functions for working with Result types.
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
import { resultOk, resultErr, unwrapResultOrThrow } from '@novasamatech/scale';
|
|
159
|
+
|
|
160
|
+
// Create results
|
|
161
|
+
const success = resultOk(42);
|
|
162
|
+
// Result: { success: true, value: 42 }
|
|
163
|
+
|
|
164
|
+
const failure = resultErr('error message');
|
|
165
|
+
// Result: { success: false, value: 'error message' }
|
|
166
|
+
|
|
167
|
+
// Unwrap or throw
|
|
168
|
+
const value = unwrapResultOrThrow(
|
|
169
|
+
success,
|
|
170
|
+
(err) => new Error(`Operation failed: ${err}`)
|
|
171
|
+
);
|
|
172
|
+
// Result: 42
|
|
173
|
+
|
|
174
|
+
// Throws error if result is failure
|
|
175
|
+
unwrapResultOrThrow(
|
|
176
|
+
failure,
|
|
177
|
+
(err) => new Error(`Operation failed: ${err}`)
|
|
178
|
+
);
|
|
179
|
+
```
|
|
180
|
+
|