GithubHelp home page GithubHelp logo

sfomuseum / go-edtf-wasm Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 15.42 MB

Go package for exposing sfomuseum/go-edtf functionality as WebAssembly binaries.

License: Other

Makefile 10.34% Go 42.04% HTML 25.74% JavaScript 18.41% CSS 3.48%
golang edtf wasm webassembly

go-edtf-wasm's Introduction

go-edtf-wasm

Go package for exposing sfomuseum/go-edtf functionality as WebAssembly binaries.

WASM

Building

The easiest thing is to run the wasm Makefile target, like this:

$> make wasm
GOOS=js GOARCH=wasm go build -mod vendor -o www/wasm/parse.wasm cmd/parse/main.go

This will place a copy of the parse.wasm binary in www/wasm/parse.wasm.

The binary exposes a single parse_edtf function that takes a single string as its input and returns a JavaScript promise. The promise returns a JSON-encoded edtf.EDTFDate if successful and an error string if not.

For example (with error handling omitted for the sake of brevity):

var raw_el = document.getElementById("raw");
var edtf_str = raw_el.value;

var result_el = document.getElementById("result");
result_el.innerHTML = "";
    
parse_edtf(edtf_str).then(rsp => {

	var edtf_d = JSON.parse(rsp)
	
	var pre = document.createElement("pre");
	pre.innerText = JSON.stringify(edtf_d, '', 2);
	
	result_el.appendChild(pre);
	
}).catch(err => {
	console.log("Failed to parse EDTF string", err)
});

Serving go-edtf-wasm

The package comes with a handy server tool for serving the parse.wasm binary and a simple web page for parsing EDTF date strings.

$> make cli
go build -mod vendor -o bin/server cmd/server/main.go

The to run the server:

$> ./bin/server 
2021/01/07 17:56:48 Listening on http://localhost:8080

WASI(p)

Generate a WASM (WASI) file using Go 1.21.0 or higher.

Building

The easiest thing is to run the wasip Makefile target, like this:

$> make wasip
GOARCH=wasm GOOS=wasip1 go build -mod vendor -ldflags="-s -w" -o www/wasip/parse.wasm ./cmd/parse-wasi/main.go

This will place a copy of the parse.wasm binary in www/wasip/parse.wasm.

Note that WASM (WASI) files produced by Go are substantially larger than WASM files produced by TinyGo (see below).

WASI

Generate a WASM (WASI) file using TinyGo 0.28.1.

Note: As of this writing this will not work with Go 1.21, only Go 1.20.x

Building

The easiest thing is to run the wasi Makefile target, like this:

$> make wasi
tinygo build -no-debug -o www/wasi/parse.wasm -target wasi ./cmd/parse-wasi/main.go

This will place a copy of the parse.wasm binary in www/wasi/parse.wasm.

Note that this requires having a copy of TinyGo v0.28.0 or higher installed and findable in your local path.

Python

Yes. For example:

import edtf.parser

p = edtf.parser.Parser()
d = p.parse("2022")

print(d["end"])

Please consult the sfomuseum/py-edtf package for details.

See also

go-edtf-wasm's People

Contributors

dependabot[bot] avatar thisisaaronland avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

go-edtf-wasm's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.