Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/MaxGraey/as-variant
Variant data type for AssemblyScript
https://github.com/MaxGraey/as-variant
any assemblyscript dynamic type variant wasm webassembly
Last synced: about 1 month ago
JSON representation
Variant data type for AssemblyScript
- Host: GitHub
- URL: https://github.com/MaxGraey/as-variant
- Owner: MaxGraey
- License: mit
- Created: 2021-07-08T12:01:05.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-10T09:20:24.000Z (almost 2 years ago)
- Last Synced: 2024-11-11T03:35:59.488Z (about 1 month ago)
- Topics: any, assemblyscript, dynamic, type, variant, wasm, webassembly
- Language: TypeScript
- Homepage:
- Size: 166 KB
- Stars: 26
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-assemblyscript - MaxGraey/as-variant - `Variant` type providing dynamicness (Packages)
README
Variant Data Type for AssemblyScript
===[![Build Status](https://github.com/MaxGraey/as-variant/actions/workflows/test.yml/badge.svg?event=push)](https://github.com/MaxGraey/as-variant/actions/workflows/test.yml?query=branch%3Amain)
[![npm](https://img.shields.io/npm/v/as-variant.svg?color=007acc&logo=npm)](https://www.npmjs.com/package/as-variant)Supports any builtin types like i32, bool, string and any custom classes (managed and unmanaged).
### Basic Usage
```ts
import { Variant } from 'as-variant/assembly'
// before 0.20.x
// import { Variant } from 'as-variant'class Foo { }
let vNum = Variant.from(123) // stored as i32
let vStr = Variant.from('hello') // stored as string
let vFoo = Variant.from(new Foo) // stored as Foo referencevNum.set(2.0) // now stored as f64
assert(vNum.is()) // ok
assert(!vStr.is()) // ok
assert(vStr.is()) // ok
assert(vFoo.is()) // okassert(vNum.id != vStr.id) // compare dynamic IDs.
assert(vFoo.id == Variant.idof())let valF64 = vNum.get() // safely extract value
let willFail = vNum.get() // will throw exception!
```### Unsafe Usage:
```ts
let vNum = Variant.from(123)
// `getUnchecked` skips all checks. It may be danger.
assert(vNum.getUnchecked() == 123)
```### Use as value for Any Dynamic Dictionary
```ts
const dict = new Map()dict.set('str', Variant.from('hello'))
dict.set('num', Variant.from(124.0))dict.set('arr', Variant.from([1, 2, 3]))
assert(dict.get('arr').get()[2] == 3)
// or
assert(dict['arr'].get()[2] == 3)
```which equivalent to JavaScript:
```js
const dict = {
str: 'hello',
num: 124.0,
}dict.arr = [1, 2, 3]
assert(dict.arr[2] == 3)
```