https://github.com/rvanasa/mo-convert
90+ consistently named Motoko base library type conversions.
https://github.com/rvanasa/mo-convert
base-library candid convenience dfinity icp internet-computer json learning motoko tooling type-conversion
Last synced: 3 months ago
JSON representation
90+ consistently named Motoko base library type conversions.
- Host: GitHub
- URL: https://github.com/rvanasa/mo-convert
- Owner: rvanasa
- Created: 2023-08-26T00:10:15.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-13T23:59:00.000Z (3 months ago)
- Last Synced: 2025-02-14T00:32:13.089Z (3 months ago)
- Topics: base-library, candid, convenience, dfinity, icp, internet-computer, json, learning, motoko, tooling, type-conversion
- Language: Motoko
- Homepage: https://www.npmjs.com/package/mo-convert
- Size: 257 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `mo-convert` [](https://www.npmjs.com/package/mo-convert) [](https://opensource.org/licenses/MIT) [](https://github.com/rvanasa/mo-convert/issues)
> ### Systematic [Motoko](https://github.com/dfinity/motoko#readme) base library type conversions.
---
This repository includes Motoko source code and a JSON metadata file containing all base library type conversions
with consistent `B.fromA : A -> B` and `A.fromB : B -> A` signatures. `mo-convert` is intended as a stepping stone for community
language tooling and for those such as myself who have trouble remembering whether the function was named `Array.toBuffer()` or `Buffer.fromArray()`.## Quick Start (Motoko):
Add the following to your [`mops.toml`](https://mops.one/docs/install) config file:
```toml
[dependencies]
convert = "https://github.com/rvanasa/mo-convert"
```Include the following import in your Motoko source code:
```motoko
import C "mo:convert";
```Go to town:
```motoko
let nat = 123;
let text = C.Text.fromNat(nat); // => "123"let option = C.Option.fromResult(#ok true); // => ?true
let array = [1, 2, 3];
let buffer = C.Buffer.fromArray(array);// Chain conversions with the piping operator
let pipe = (
array
|> C.Iter.fromArray _
|> C.List.fromIter _
|> C.Array.fromList _
);
```## Quick Start (Node.js):
Run the following command to install the `mo-convert` npm package:
```sh
npm i --save-dev mo-convert
```Access the type conversion metadata:
```js
const { conversions } = require('mo-convert');console.log('All type conversions:', conversions);
```Example JSON entry:
```js
conversions[0] ==
{
from: "Array",
to: "Blob",
module: "Blob",
name: "fromArray",
signature: "Blob.fromArray(bytes : [Nat8]) : Blob",
prim: "arrayToBlob",
}
```