10. November 2015

Babel Typecheck

Babel Typecheck (GitHub: codemix/babel-plugin-typecheck, License: MIT, npm: babel-plugin-typecheck)

Babel Typecheck is a Babel plugin for static and runtime type checking. It does so using Flow type. What it does is turn a code block like this:

function sendMessage (to: User, message: string): boolean {
  return socket.send(to, message);
}

Into this:

function sendMessage(to, message) {
  var _socket$send;

  if (!(to instanceof User)) throw new TypeError("Value of argument 'to' violates contract.");
  if (typeof message !== "string") throw new TypeError("Value of argument 'message' violates contract.");
  _socket$send = socket.send(to, message);
  if (typeof _socket$send !== "boolean") throw new TypeError("Function 'sendMessage' return value violates contract.");
  return _socket$send;
}

What this does is it executes the same as when you would write your function like this:

function sendMessage (to, message) {
  return socket.send(to, message);
}

But with one big difference, when you call this function with the wrong arguments your application just breaks. When using Babel Typecheck it would present you with a type error on transpiling so you will not make silly mistakes again. Well not the type error kind at least.

In the example above we see a few new things, mostly attached to the function call:

    (to: User, message: string): boolean

Which means that the to parameter expects to be an instance of User. The message parameter expects to be a String. The : at the and of the function call is to indicate what the function will return which is a Boolean in this example.

This function demonstrates which kinds of annotations are supported:

function foo(
    aNum: number,
    anOptionalString: ?string, // will allow null/undefined
    anObject: Object,
    aDate: Date,
    anError: Error,
    aUnionType: Object|string,
    aClass: User,
    aShape: {foo: number, bar: ?string},
    anArray: Array,
    arrayOf: string[] | Array<string>,
    {x, y}: {x: string, y: number}, // destructuring works
    es6Defaults: number = 42
) : number {
  return aNum;
}

When I saw the syntax of this plugin the first thing that jumped into my mind was why not just use Typescript then. Since Typescript had it’s own upstream this might not be inline with the ESnext(ES6/ES7) feature we have grown to love. By including this plugin in your .babelrc to can have the power and security of strongly typed Javascript and all the new features provided by Babel.

09. November 2015

Squiggle

Squiggle (GitHub: wavebeem/squiggle, License: MIT, npm: squiggle-lang)

Squiggle is a strict, expression-oriented, compile-to-JS programming language. Yes, yet another compile-to-JS language. At the beginning I was sceptic about it, but once I started reading the tutorial posted on the site I adjusted my opinion. Not everything is there yet, but I need to say that I like the way it looks and might even consider picking it up.

So some of the features that drew my attention are:

  • Arity checked functions
    • When calling a function created in Squiggle with not enough arguments, it throws an exception.
  • Frozen literals
    • Array and object literals are frozen with Object.freeze by default, so you can’t accidentally mutate them.
  • Easy updates
    • Operators ++ to concatenate two arrays or two strings, and ~ to merge two objects into a new frozen object with the prototype of the first object.
  • Destructuring assignment
    • Grab object properties or array elements when you assign variables, like: let [x, y] = [1, 2] or let {x, y} = {x: 1, y: 2}.
  • Pattern matching
    • Is similar to the Javascript switch but with destructuring power built-in and no dangerous fall-through.
  • No type coercion
    • Standard operators like +, -, *, and more, have been replaced with strict version that do not perform any type coercions, throwing exceptions on bad inputs.
  • Deep equality
    • The operator == performs a deep equality check.

There are a lot more features which just make it work a bit nicer. If you want a good overview of what this language can do I would recommend you reading the tutorial section of the website and then give it a shot in the browser.

Here is a small example provided by the developer in which you can see how it would work together met Node’s HTTP package.

let http = require "http"

let port = 1337
let host = "127.0.0.1"

def handler(res, res) =
    let headers = {"Content-Type": "text/plain"}
    let _ = res.writeHead(200, headers)
    let _ = res.end("Hello world\n")
    in undefined

let server = http.createServer(handler)
let _ = server.listen(port, host)
let _ = console.log("Server running at http://" ++ host + ":" ++ port ++ "/")
in undefined

Since there is a comment section present after the latest update, I would love to here what you guys think of this and the blog in general.

06. November 2015

ES6 Quiz

Today I will not be checking any new library. It is time to invest in some more in depth knowledge of how ES6/ES2015/ESnext works. This week I was checking my reddit feed looking for something awesome to show you all, when I stumbled across this quiz from Perfect Skills.

I shared this quiz at my job and noticed that a lot of people were “familiar” with ES6. However nobody got a perfect score, Ok, the quiz is actually really hard, but still as frontend/javascript developer we should know the language we are working with or going to work with in the very near future. That is the reason why I would recommend everybody taking this quiz, because it will enlighten you on how much you do not know or fully understand about the new standard in Javascript.

The answers and explanation can be found here, but I recommend that you do not look at them before taking the test.

I will be redoing this website over the weekend so I might not be able to provide you with a new post on Monday. The reason why I need to redo it, is because the static site generator I’m using to supply you with new posts each day broke on me this week. I spend several hours debugging without any luck so I will be switching to Wintersmith with which I only had good experiences so hopefully I do not need to redo it any time soon. I will also be looking into including a comment section so I can here more back from you guys.

Thanks for all your loyal support and have a nice weekend.

05. November 2015

inHerito

inHerito (GitHub: Ositoozy/inherito, License: BSD, bower: rot.js)

inHerito is a compostable factory that allows you create objects you want ease and “correct” inheritance. It allows you to use properties and function methods from other objects without overusing and over inheriting. Every object that is created gets it’s own logging information if option is set to true and allows you to keep a mindful eye on your objects. This is my take on solving the original OOP problem of over inheriting. Now you have “Objects Linked to Other Objects” and inherit only if you say so.

Then the first question that popped into my head was do we need yet another design pattern, well no ofcourse not but it could be useful for bootstrapping a new application to set the tone and get the correct mindset.

As the developer claims:

Never be affraid again to have more than one level deep of inherited object inheritance tree. In OOP we usually create newer objects for the sake of inheritance and not for the sake of semantical correctness.

  • Ex: Let’s consider a dog. A good and sane OOP developer would most likely create an abstract tree that resides the dog animal this way
    Animal // And Followed by animal properties (ex blood, heart, etc.)
    Animal.k9.dog // And Followed by dog properties (bark, bite, age, how many legs, etc.)
    

But let’s say in that same program we want to make the robot dog, which semantically inherits a of the dog’s properties to begin with. But we know semantically that robot dog belongs basically from two classes. It’s not an Animal anymore but shares a lot with dog.

let robotDog = dog.create({
    inherit: [bite, bark],
    material: metal
});

Is it a dog? true
Is it a robot? true

This project currently is very young but I like the direction where it is going. Momentairley it can be used as a base template by just cloning the repo. Sadly there are no other ways to generate a project holding this methodologies but as the developer tells me there will be in the future.

04. November 2015

Rot.js

Rot.js (GitHub: ondras/rot.js, License: BSD, bower: rot.js)

Rot.js is a Roguelike Toolkit in JavaScript. In a nutshell it is a set of JavaScript libraries, designed to help with a roguelike development in browser environment. It is modeled after libtcod and offers the following features:

  • JS prototype enhancements
  • RNG, Map generation, FOV, Lighting
  • Pathfinding, turn scheduling
  • Canvas-based ASCII display

It comes with some very extensive documentation and has a tutorial.