Code Monkey home page Code Monkey logo

gostdjs's Introduction

Go Stdlib in JS

This is an attempt to implement useful features of the Go standard library in Javascript.

This is written in plain Javascript and doesn't have any dependencies so you should be able to run it in lots of places.

Types

We check types on the inputs to a function and always try to return the correct types from a function.

Go strings are JS strings, except JS strings are assumed to be encoded as UTF-16, which means that the encoding behavior needs to change in some circumstances.

Functions that take a rune in Go take an integer in Javascript.

We use custom int64 and uint64 types due to Javascript's lack of precision for these integers. Several functions return them, for example a time.Duration stores an int64 internally. The downside is that normal math operators - -, +, *, /, don't work with these types. Instead you have to use custom operators:

  • add - add another Int64/Uint64.
  • addn - add an integer
  • sub - sub another Int64/Uint64
  • subn - sub another integer
  • mul/muln - multiplication
  • mod/modn - modulo arithmetic
  • lt/ltn - less than
  • lte/lten - less than or equal to
  • eq/eqn - equal to
  • gt/gtn - greater than
  • gte/gten - greater than or equal to

Each of these will throw if you pass an object that has the wrong type, e.g if you pass an Int64 object to addn. This should help you verify that your code is doing the thing you expect it to, and failing when it does something you don't expect it to.

time.Duration objects support all of these functions. If a Go function accepts an int64 or a uint64, those should be converted to the equivalent Int64/Uint64 objects before using this library. If a Go function accepts an int, we use normal Javascript numbers.

Int64 objects can only do operations with other Int64 objects. To convert a Uint64 to an Int64, call toSigned(), and call .toUnsigned() in the opposite direction.

Running the tests

Run make test. The tests use the most recent versions of mocha and should, as well as eslint for validation.

Installation

Copy the source code directly to your project. You should inspect the source of the project and see if it makes sense for you. You may only need a single method, in which case you should just copy that into your source tree directly.

To require a module, require the index.js file in the package. So to import the time package:

const time = require('./gostdjs/time/index.js');

Installation via NPM encourages bloat and discourages people from inspecting the source code to see if the tool a) does what they want and b) does not do what they don't want. If this makes it harder for you to install - good! It should be more expensive in time and effort to install third party packages.

I will tag new releases via Git, along with a change log, and you can use Git and the Github compare tool to diff changes between releases.

Performance

For now performance is a secondary goal to correctness. A lot of the optimizations present in the Go standard library around e.g. short vs long strings are missing here.

Developing

Errors

Functions that don't use I/O and return an error as the last parameter should instead throw an error. Use internal.throwSystem in catch handlers to distinguish between system errors (type and syntax errors) and user errors.

Strings/Runes

To convert between strings and their ASCII character equivalents, do 'a'.charCodeAt(0). Unfortunately you can't do math between uint8 types and bytes. To get a Unicode code point at a character, do 'uchar'.codePointAt(0), which may yield a number larger than 256.

Testing

To convert tests, switch something like this:

if d1 != d2 {
    t.Errorf("%d != %d", d1, d2)
}

to:

d1.should.equal(d2, util.format("%d != %d", d1, d2))

Note util.format does not line up exactly with fmt.Printf parameters, so you might need to massage it a little bit.

gostdjs's People

Contributors

kevinburke avatar

Stargazers

Dylan Pyle avatar

Watchers

James Cloos avatar  avatar

gostdjs's Issues

Support distributed cancelation

How would this work? You can't cancel a callback, but you could imagine some kind of wrapper or something that throws an error immediately when you get control again.

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.