The JavaScript blog.


rest array ES7 JSON

Array Future, JSONFormData

Posted on .

Array Future

Kiko Beats sent in array-future (GitHub: Kikobeats/array-future, License: MIT, npm: array-future), a module that collects together several ES7 proposals for instance methods on Array:


[1, 2, 3].average();
// 2

[1, 2, 3].first();
// 1

[1, 2, 3].shuffle();
// [ 2, 3, 1 ]

[1, 2, 3].includes(3);
// true

The includes method is implemented as described by this documentation. Each method is tested with Mocha, and there's documentation in the readme.


I've been doing a lot of work with the FormData API, and I found a JSONFormData polyfill by Roman Liutikov in the DailyJS submission queue. It implements the W3C HTML JSON form submission API:

Instead of capturing form data as essentially an array of key-value pairs which is the bread and butter of existing form encodings, it relies on a simple name attribute syntax that makes it possible to capture rich data structures as JSON directly.

This implementation lets you submit data to servers as JSON by setting a form's enctype to application/json. It also supports REST API HTTP verbs like PUT and DELETE.

Submitting data with JavaScript is similar to FormData -- just use new JSONFormData(form).formData to get an object based on the form.

If you're writing RESTful services with JSON and want to make a quick web interface, then JSONFormData should help speed things up.


decorator mysql ES7 angularjs

DecorateThis, jsRequest, ODataAngularResources

Posted on .


DecorateThis (GitHub: mako-taco/DecorateThis, License: Apache 2.0, npm: decorate-this) by Jake Scott is the first library to my knowledge that takes advantage of ES7 decorators. Decorators allow you to annotate classes and properties.

An example of a decorator is @readonly:

class Person {  
  name() { return `${this.first} ${this.last}` }

This is far less complex than the older Object.defineProperty syntax:

Object.defineProperty(Person.prototype, 'name', {  
  value: specifiedFunction,
  enumerable: false,
  configurable: true,
  writable: true

The DecorateThis module provides some very handy decorators for argument type checking, including:

  • Memoization: @memoized
  • Type validation: primitive types @param(Number), and a class T is checked with @param(T)
  • Duck type checks: @param({ hello: String, info: { age: Number, color: String } });

If these checks fail type errors are thrown, which means you'll need exception handlers for places where these checks should be caught and handled gracefully.


Bruno Panuto and Danilo Valente have been working on a JavaScript file loader called jsRequest (GitHub: danilo-valente/jsRequest, License: MIT) since back in 2013. It handles asynchronous loading, and the syntax supports loading multiple files with a chained API:


It supports extra options that allow success and failure callbacks to be supplied, and it also has a progress callback. If you need to order requests, then you can use the wait method.

jsRequest tracks the request history, so if you need to check what files have been loaded you can inspect the jsRequest.history object.


I recently wrote about OData Server, and Raphael Atallah sent in ODataAngularResources (GitHub: devnixs/ODataAngularResources, License: MIT), a fork of Angular's $resource that allows making OData queries using a fluent API.

Once you've added the dependency, you can do cool stuff like this:

var User = $odataresource('/user/:userId', { userId:'@id' });  
var myUsers = User.odata()  
  .filter('Name', 'John')

You can also use top, orderBy, and skip, and request single elements. The readme has full examples including details on how to create more complex queries with predicates.