{"id":16866414,"url":"https://github.com/maxgraey/as-variant","last_synced_at":"2025-03-23T16:32:44.379Z","repository":{"id":37030503,"uuid":"384109820","full_name":"MaxGraey/as-variant","owner":"MaxGraey","description":"Variant data type for AssemblyScript","archived":false,"fork":false,"pushed_at":"2023-02-10T09:20:24.000Z","size":170,"stargazers_count":26,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-18T22:03:37.979Z","etag":null,"topics":["any","assemblyscript","dynamic","type","variant","wasm","webassembly"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MaxGraey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-07-08T12:01:05.000Z","updated_at":"2024-04-01T18:24:39.000Z","dependencies_parsed_at":"2024-01-18T03:48:22.420Z","dependency_job_id":null,"html_url":"https://github.com/MaxGraey/as-variant","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaxGraey%2Fas-variant","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaxGraey%2Fas-variant/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaxGraey%2Fas-variant/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaxGraey%2Fas-variant/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MaxGraey","download_url":"https://codeload.github.com/MaxGraey/as-variant/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245131144,"owners_count":20565780,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["any","assemblyscript","dynamic","type","variant","wasm","webassembly"],"created_at":"2024-10-13T14:50:32.467Z","updated_at":"2025-03-23T16:32:43.695Z","avatar_url":"https://github.com/MaxGraey.png","language":"TypeScript","readme":"Variant Data Type for AssemblyScript\n===\n\n[![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)\n[![npm](https://img.shields.io/npm/v/as-variant.svg?color=007acc\u0026logo=npm)](https://www.npmjs.com/package/as-variant)\n\nSupports any builtin types like i32, bool, string and any custom classes (managed and unmanaged).\n\n### Basic Usage\n\n```ts\nimport { Variant } from 'as-variant/assembly'\n// before 0.20.x\n// import { Variant } from 'as-variant'\n\nclass Foo { }\n\nlet vNum = Variant.from(123)      // stored as i32\nlet vStr = Variant.from('hello')  // stored as string\nlet vFoo = Variant.from(new Foo)  // stored as Foo reference\n\nvNum.set(2.0)                     // now stored as f64\n\nassert(vNum.is\u003cf64\u003e())            // ok\nassert(!vStr.is\u003cf64\u003e())           // ok\nassert(vStr.is\u003cstring\u003e())         // ok\nassert(vFoo.is\u003cFoo\u003e())            // ok\n\nassert(vNum.id != vStr.id)        // compare dynamic IDs.\nassert(vFoo.id == Variant.idof\u003cFoo\u003e())\n\nlet valF64   = vNum.get\u003cf64\u003e()    // safely extract value\nlet willFail = vNum.get\u003cstring\u003e() // will throw exception!\n```\n\n### Unsafe Usage:\n\n```ts\nlet vNum = Variant.from(123)\n// `getUnchecked` skips all checks. It may be danger.\nassert(vNum.getUnchecked\u003ci32\u003e() == 123)\n```\n\n### Use as value for Any Dynamic Dictionary\n\n```ts\nconst dict = new Map\u003cstring, Variant\u003e()\n\ndict.set('str', Variant.from('hello'))\ndict.set('num', Variant.from(124.0))\n\ndict.set('arr', Variant.from([1, 2, 3]))\n\nassert(dict.get('arr').get\u003ci32[]\u003e()[2] == 3)\n// or\nassert(dict['arr'].get\u003ci32[]\u003e()[2] == 3)\n```\n\nwhich equivalent to JavaScript:\n\n```js\nconst dict = {\n  str: 'hello',\n  num: 124.0,\n}\n\ndict.arr = [1, 2, 3]\n\nassert(dict.arr[2] == 3)\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxgraey%2Fas-variant","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxgraey%2Fas-variant","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxgraey%2Fas-variant/lists"}