@novasamatech/scale 0.7.4-0 → 0.7.5-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/dist/errEnum.d.ts +5 -2
- package/dist/errEnum.js +10 -5
- package/dist/errEnum.spec.js +24 -0
- package/package.json +1 -1
package/dist/errEnum.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import type { Codec, CodecType } from 'scale-ts';
|
|
2
2
|
import type { CodecError, ErrCodec } from './err.js';
|
|
3
|
+
type ErrEnumArguments<T> = [value: Codec<T>, message: string | ((value: T) => string)];
|
|
3
4
|
type MapErrEnum<Name extends string, T extends Record<string, ErrEnumArguments<any>>> = {
|
|
4
5
|
[K in keyof T]: ErrCodec<CodecType<T[K][0]>, K extends string ? `${Name}::${K}` : Name>;
|
|
5
6
|
};
|
|
6
7
|
type ErrEnumInput<Name extends string, T extends Record<string, ErrEnumArguments<any>>> = {
|
|
7
8
|
[K in keyof T]: CodecError<CodecType<T[K][0]>, K extends string ? `${Name}::${K}` : Name>;
|
|
8
9
|
}[keyof T];
|
|
9
|
-
type
|
|
10
|
-
|
|
10
|
+
type ErrEnumCodec<Name extends string, T extends Record<string, ErrEnumArguments<any>>> = Codec<ErrEnumInput<Name, T>> & MapErrEnum<Name, T> & {
|
|
11
|
+
[Symbol.hasInstance](v: unknown): v is ErrEnumInput<Name, T>;
|
|
12
|
+
};
|
|
13
|
+
export declare function ErrEnum<const Name extends string, const T extends Record<string, ErrEnumArguments<any>>>(name: Name, inner: T): ErrEnumCodec<Name, T>;
|
|
11
14
|
export {};
|
package/dist/errEnum.js
CHANGED
|
@@ -2,9 +2,14 @@ import { enhanceCodec } from 'scale-ts';
|
|
|
2
2
|
import { Enum } from './enum.js';
|
|
3
3
|
import { Err } from './err.js';
|
|
4
4
|
export function ErrEnum(name, inner) {
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
const variants = Object.fromEntries(Object.entries(inner).map(([k, [value, message]]) => [k, Err(`${name}::${k}`, value, message, k)]));
|
|
6
|
+
const codec = enhanceCodec(Enum(variants), v => ({ tag: v.instance, value: v }), v => v.value);
|
|
7
|
+
const result = Object.assign(codec, variants);
|
|
8
|
+
// defineProperty (not Object.assign) so the symbol stays non-enumerable —
|
|
9
|
+
// otherwise Object.assign'ing the codec into another target would propagate
|
|
10
|
+
// the hasInstance hook and silently change that target's instanceof.
|
|
11
|
+
Object.defineProperty(result, Symbol.hasInstance, {
|
|
12
|
+
value: (v) => Object.values(variants).some(C => v instanceof C),
|
|
13
|
+
});
|
|
14
|
+
return result;
|
|
10
15
|
}
|
package/dist/errEnum.spec.js
CHANGED
|
@@ -11,6 +11,30 @@ describe('ErrEnum', () => {
|
|
|
11
11
|
expect(error.name).toBe('ErrorCodec::TestError');
|
|
12
12
|
expect(error.message).toBe('Test message');
|
|
13
13
|
});
|
|
14
|
+
it('should support `instanceof` against the enum itself', () => {
|
|
15
|
+
const A = ErrEnum('A', {
|
|
16
|
+
First: [_void, 'First'],
|
|
17
|
+
Second: [_void, 'Second'],
|
|
18
|
+
});
|
|
19
|
+
const B = ErrEnum('B', {
|
|
20
|
+
Other: [_void, 'Other'],
|
|
21
|
+
});
|
|
22
|
+
const first = new A.First(undefined);
|
|
23
|
+
const other = new B.Other(undefined);
|
|
24
|
+
expect(first instanceof A).toBe(true);
|
|
25
|
+
expect(first instanceof A.First).toBe(true);
|
|
26
|
+
expect(first instanceof A.Second).toBe(false);
|
|
27
|
+
expect(other instanceof A).toBe(false);
|
|
28
|
+
expect(new Error('plain') instanceof A).toBe(false);
|
|
29
|
+
const e = first;
|
|
30
|
+
if (e instanceof A) {
|
|
31
|
+
expect(e.name).toBe('A::First');
|
|
32
|
+
expect(e.payload).toBeUndefined();
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
throw new Error('narrowing failed');
|
|
36
|
+
}
|
|
37
|
+
});
|
|
14
38
|
it('should correctly serialize/deserialize', () => {
|
|
15
39
|
const ErrorCodec = ErrEnum('ErrorCodec', {
|
|
16
40
|
First: [_void, 'First'],
|