0.1.0 • Published 7 months ago

ext-unionize v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Externally Unionize

This package provide extension for original unionize package, for create Externally tagged representation.

Externally tagged enum representation provide by default via Rust Serde and for Python Pydantic via typenum library.

Dependency from unionize unnecessary, but it small and not affect to compiled code (when not used), therefore insert into dependency intentionally.

Compatability with Python\Rust provided representation described in typenum package.

Example

Internally (original unionize)

import { unionize, ofType } from 'unionize';

const MyEnym = unionize({
  First: ofType<{ id: number; text: string }>(),
  Second: ofType<{ id: number; }>(),
});

// {"tag":"First","id":1,"text":"test"}
console.log(JSON.stringify(MyEnum.First({ id: 1, text: "test" })))

Adjacently (original unionize)

import { unionize, ofType } from 'unionize';

const MyEnym = unionize({
  First: ofType<{ id: number; text: string }>(),
  Second: ofType<{ id: number; }>(),
}, {tag: "tag", value: "value"});

// {"tag":"First","value":{"id":1,"text":"test"}}
console.log(JSON.stringify(MyEnum.First({ id: 1, text: "test" })))

Externally

import { ofType } from 'unionize';
import { extUnionize } from 'ext-unionize';

const MyEnym = extUnionize({
  First: ofType<{ id: number; text: string }>(),
  Second: ofType<{ id: number; }>(),
});

// {"First":{"id":1,"text":"test"}}
console.log(JSON.stringify(MyEnum.First({ id: 1, text: "test" })))
0.1.0

7 months ago