Bluebird

04 Oct 2013 | By Alex Young | Comments | Tags promises async libraries node

The never-ending quest for the perfect async/promises library continues. Petka Antonov’s offering, Bluebird (GitHub: petkaantonov / bluebird, License: MIT, npm: bluebird), has all of your favourite APIs, impressive benchmarks, and Mocha unit tests. It works in browsers as well.

The Bluebird API reference has explanations of each method, and code examples. Here’s an example of using promises and finally to make an XMLHttpRequest:

function ajaxGetAsync(url) {
  return new Promise(function(resolve, reject) {
    var xhr = new XMLHttpRequest;
    xhr.addEventListener('error', reject);
    xhr.addEventListener('load', resolve);
    xhr.open('GET', url);
    xhr.send(null);
  }).finally(function() {
    $('#ajax-loader-animation').hide();
  });
}

Petka points out that the finally method works like Q’s finally method. The catch/caught method also works like Q (with fail renamed to caught), which is a bit of sugar for .then(null, errorHandler).

One of the tools Petka mentions for verifying the quality of the project was async-compare by Gorgi Kosev. This project aims to compare async patterns based on their complexity, performance, and “debuggability”. The stack traces generated by libraries like Bluebird and Q are captured and curated, so async-compare can be used to measure the distance between the function that created the error and the error in the stack trace. This is useful because debugging asynchronous code is notoriously difficult.

Even though Q and popular Node modules like async are good, I think Bluebird is worth looking at because it seems to have attention to the right details.

JavaScript Allongé

03 Oct 2013 | By Alex Young | Comments | Tags books

JavaScript Allongé by Reginald Braithwaite is a free book that focuses on using functions to write simpler JavaScript with less bugs:

JavaScript Allongé provides recipes for using functions to write software that is simpler, cleaner, and less complicated than alternative approaches that are object-centric or code-centric. JavaScript idioms like function combinators and decorators leverage JavaScript’s power to make code easier to read, modify, debug and refactor, thus avoiding problems before they happen.

I found the author’s blog post, JavaScript Allonge is Free, interesting and inspiring. Technical authors don’t often get to air the stories behind their work, but Reginald goes into a lot of detail behind JavaScript Allongé and how his life has changed since publishing it:

And as a bonus, I got my dream job, and now it’s time for me to walk the freedom talk. I’m now working for a company that is trying its dambdest to help every single programmer on the planet write more software and better software. And we give something away, crossing our fingers that more software and better software will be a good thing and that we’ll make enough to keep doing what we do.

I believe in this. That’s why I write, and that’s why I work at GitHub. And you helped me get here:

It’s encouraging to hear that such a talented writer and programmer has landed his dream job and become part of the GitHub animated gif squad.

Part of why I believed in myself enough to go through the terrors of interviewing for my job is the confidence you the readers gave me when you supported my books and told me that the ideas I was trying to share were useful. I can’t thank you enough for that.

The book is available as a PDF, ePub, and Kindle-friendly mobile file.

Node Roundup: 0.10.20, Asker, AbsurdJS

02 Oct 2013 | By Alex Young | Comments | Tags node modules npm css http

Node 0.10.20

Node 0.10.20 is out which means Node 0.10 is almost old enough to drink in most US states. How about that! This version fixes that annoying error that I seem to see when deploying to Heroku which reads: npm ERR! cb() never called.

Asker

Asker (GitHub: nodules / asker, License: MIT, npm: asker) is a http.request wrapper with retries, gzip decoding, and connection pool tuning. It was created by Phillip Kovalev for Yandex and is used on the auto.yandex.ru site.

Asker isn’t really a competitor to request, but a module designed around tailoring HTTP requests to comply with service level agreements. It makes certain things easier, like body encoding.

Node’s built-in HTTP connection pools are eschewed for a custom solution that allows you to prioritise services. This helps avoid situations where a critical service fails due to the exhaustion of TCP sockets. More is explained in the project’s readme file, and the project includes unit tests as well.

AbsurdJS

AbsurdJS (GitHub: krasimir / absurd, License: MIT, npm: absurd) by Krasimir Tsonev is a CSS preprocessor that converts JSON to CSS:

".navigation": {
  margin: "12px 0 0 0 ",
  type: "horizontal",
  a: {
    elementstyle: "button",
    responsive: "true"
  }
}

CSS isn’t all that different from JSON when you think about it, but you can’t easily include lumps of CSS in JavaScript files. AbsurdJS allows you to sidestep this problem by converting JSON to CSS. It includes a feature called storage that allows you to define custom properties that will be expanded to standard CSS.

There’s a detailed post about how it all works here: Write Your CSS with JavaScript.

jQuery Roundup: One Page Scroll, crSpline

01 Oct 2013 | By Alex Young | Comments | Tags jquery plugins animation
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.

One Page Scroll

One Page Scroll (GitHub: peachananr / onepage-scroll) by Pete Rojwongsuriya enables pages to scroll in the vertical locking style like Apple’s new iPhone 5S page. It’s based around sections – any selector can be used – and jQuery’s animations:

$('.main').onepage_scroll({
   sectionContainer: 'section',
   easing: 'ease',
   animationTime: 1000,
   pagination: true,
   updateURL: false
});

I had a look at the source and it includes Eike Send’s $.fn.swipeEvents so it should work with touchscreens.

jquery.crSpline

A visualisation of movement along Catmull-Rom splines.

jquery.crSpline (GitHub: MmmCurry / jquery.crSpline, License: MIT) by M. Ian Graham is an animation plugin that moves objects along waypoints, smoothing the edges with splines.

The name crSpline comes from Catmull-Rom:

The curve is named after Edwin Catmull and Raphael Rom. In computer graphics, Catmull–Rom splines are frequently used to get smooth interpolated motion between key frames. For example, most camera path animations generated from discrete key-frames are handled using Catmull–Rom splines.

The plugin includes $.crSpline.buildSequence which can be used to generate spline animation objects. At the moment there isn’t much documentation, but demo.js should get you started.

RSS Pipes, type-check

30 Sep 2013 | By Alex Young | Comments | Tags rss node apps haskell functional

RSS Pipes

RSS Pipes (GitHub: dai-shi / rss-pipes, License: BSD) by Daishi Kato is an Express app for aggregating RSS feeds. It has a lightweight database wrapper for PostgreSQL and SQLite using the jugglingdb, which seems like a cleaner ORM that some of the Node database ORMs I’ve used, and it keeps the route handlers lightweight which is good practice in Express applications.

One of the useful features it has is filtering feeds. For example, you can filter for keywords or truncate the result set. In this respect it’s like a simple version of Yahoo! Pipes. The client-side portion of the project uses Bootstrap, so you could probably customise it if you wanted.

type-check

Haskell makes an appearance in type-check (GitHub: gkz / type-check, License: MIT, npm: type-check) by George Zahariev. It performs runtime type checking using a Haskell inspired syntax:

var typeCheck = require('type-check').typeCheck;
typeCheck('Number', 1);               // true
typeCheck('Number', 'str');           // false

typeCheck('Number | String', 2);      // true
typeCheck('Number | String', 'str');  // true

typeCheck('{x: Number, y: Boolean, ...}', {x: 2, y: false, z: 3});  // true

It works in browsers and Node, and apparently has 100% statement, branch, and line test coverage. It seems like something that might add an extra level of readability to test code, but could also be useful for validating user input.

Indentation

27 Sep 2013 | By Alex Young | Comments | Tags editing indentation

I like indenting with spaces, and it’s partly due to the types of programming languages I use. Some languages and styles depend on vertical alignment, so tabs and spaces end up getting mixed, which causes anything but a particular tab width to look weird. That was the case in the Objective-C projects at the company I’m currently working at.

The C++ programmer in our team loves tabs, however, and he pointed out Elastic tabstops. To understand how it works, take a look at the gif that shows code being edited as vertically aligned sections adapt automatically. There are several implementations of this idea, including Visual Studio and Eclipse plugins.

Sindre Sorhus sent in detect-indent which is a Node module for detecting and persisting indentation. New text can be inserted with the correct indentation, and it can help configure your editor.

It also works in browsers, and can be installed with both Bower and Component.

Sindre’s example uses JSON:

var fs = require('fs');
var detectIndent = require('detect-indent');
/*
{
    "ilove": "pizza"
}
*/
var file = fs.readFileSync('foo.json', 'utf8');
// tries to detect the indentation and falls back to a default if it can't
var indent = detectIndent(file) || '    ';
var json = JSON.parse(file);

json.ilove = 'unicorns';

fs.writeFileSync('foo.json', JSON.stringify(json, null, indent));
/*
{
    "ilove": "unicorns"
}
*/

From fs.readFile to read(2)

26 Sep 2013 | By Alex Young | Comments | Tags node libuv unix

Here’s a statement you might hear used to describe Node:

Node uses asynchronous I/O to avoid using threads for fast filesystem and network operations.

This statement is false (in more ways than one), and understanding why will help you better understand Node.

First, consider the question: what is asynchronous?

fs.readFile('file.txt', function(err, data) {
  if (err) throw err;
  console.log(data);
});

That is an asynchronous API. The program will continue running after the fs.readFile. The callback may be run at some point in the future. As JavaScript programmers we like this because JavaScript’s scoping rules mean the callback gets a closure, so we find it easy to reason about what variables are available to the callback, while being able to do thing while something potentially slow (I/O, web requests) responds.

Even if you’re not a Node programmer, you’re probably comfortable with the last example. It’s not amazingly different to a jQuery Ajax request, where callbacks are passed to $.ajax.

Now, assuming you’re not a C/C++ programmer and don’t know too much about Node’s internals, here’s the amazing thing about fs.readFile – it calls a (potentially) blocking system call.

How is that possible? We know our program continues when we try to read a file, so what’s going on? The first step is to look at Node’s fs source.

Bindings

If you look at the source for fs.readFile in lib/fs.js, you’ll see binding.read. Whenever you see binding in Node’s core modules you’re looking at a portal into the land of C++. This binding is made available using NODE_SET_METHOD(target, "read", Read). If you know any C, you might think this is a macro – it was originally, but it’s now a function. The reference to Read here is a C++ function in src/node_file.cc.

Like other filesystem code in Node, Read defines a synchronous and asynchronous version using ASYNC_CALL and SYNC_CALL. These are macros used to bind to libuv. If you dig into libuv, which you can find in deps/uv or on GitHub at joyent / libuv, then you’ll discover something interesting: the filesystem code actually uses libuv’s own streams, buffers, and native filesystem wrapping code.

Filesystem Wrapping

Going back to ASYNC_CALL in Read, one of the arguments is read: the syscall read (help: man 2 read). But wait, doesn’t this function block? Yes, but that’s not the end of the story. As summarised in An Introduction to libuv:

The libuv filesystem operations are different from socket operations. Socket operations use the non-blocking operations provided by the operating system. Filesystem operations use blocking functions internally, but invoke these functions in a thread pool and notify watchers registered with the event loop when application interaction is required.

Let’s look a little deeper. The ASYNC_CALL macro uses FSReqWrap and calls uv_fs_ methods. The one that gets bound to for reading is uv_fs_read:

int uv_fs_read(uv_loop_t* loop, uv_fs_t* req,
               uv_file file,
               void* buf,
               size_t len,
               int64_t off,
               uv_fs_cb cb) {
  INIT(READ);
  req->file = file;
  req->buf = buf;
  req->len = len;
  req->off = off;
  POST;
}

What’s that POST macro at the end? It checks to see if a callback has been provided, and if so uses uv__work_submit from src/unix/threadpool.c to read the file in a thread queue. Notice I’m just talking about Unix here, but in following fs.readFile down to the syscall that does the work it’s interesting to find uv__work_submit.

In this example the Node API method fs.readFile is asynchronous, but that doesn’t necessarily mean it’s non-blocking underneath. As the libuv book points out, socket (network) code is non-blocking, but filesystems are more complicated. Some things are event-based (kqueue), others use threads, and I’m currently trying to work out what Windows is doing (I’m only a libuv tourist for now).

I’m collating my notes on libuv for a talk on Node’s internals at The Great British Node Conference. The talk started more as a walkthrough of the core modules and binding, but I enjoyed looking at libuv in more detail so hopefully I can fit some of this stuff in there. It’d be good to see you there!

Node Roundup: 0.10.19, klei-migrate, division

25 Sep 2013 | By Alex Young | Comments | Tags node modules npm cluster databases

Node 0.10.19 Released

Node 0.10.19 was released yesterday. This version updates uv, npm, and also some core modules: readline, stream, and tls.

When you read uv: Upgrade to v0.10.17 in the release notes, don’t you wonder what that means? If you’re interested in the changes in libuv, all you need to do is checkout the relevant tag from GitHub. In this case it’s libuv/tree/v0.10.17:

2013.09.25, Version 0.10.17 (Stable)
Changes since version 0.10.16:

* build: remove GCC_WARN_ABOUT_MISSING_NEWLINE (Ben Noordhuis)
* darwin: fix 10.6 build error in fsevents.c (Ben Noordhuis)

If you switch to 0.11.13, the latest unstable release, you’ll see a whole load of interesting changes – uv_fs_stat has been rewritten, FSEventStream is now shared between multiple filesystem watchers, so it removes the limit on the maximum number of file watchers that can be created on OS X. These are just some random changes that caught my eye, there are plenty more.

These libuv changes have a direct impact on Node’s filesystem and networking modules, so it’s worth paying attention if you can.

klei-migrate

klei-migrate (GitHub: klei-dev / migrate, License: MIT, npm: klei-migrate) by Joakim Bengtson is a database independent migration command-line tool. It also works as a module, and has a Grunt plugin.

Migrations can be created with klei-migrate new, and then run with klei-migrate run. There are also other commands designed to help deal with switching between git branches, which can be tied into a post-checkout hook.

division

division (GitHub: codename- / division, License: MIT, npm: division) is another cluster module wrapper:

division provides an easy to use, chainable API, with some built-in extensions (like signals handlers) and is built with performance, zero-downtime restart (of workers), stability and simplicity in mind. The main advantage of division is that it does not depend of other modules - less dependencies equal less places where something could went wrong.

var division = require('division');
var cluster = new division();

// Configuration for development environment
cluster.configure('development', function() {
  // Put your development configuration here
  cluster.set('args', ['--some-process-args', 'send-to-workers']);
});

cluster.configure('production', function() {
  // Put your production configuration here
  cluster.enable('silent');
});

cluster.configure(function() {
  this.set('path', 'app.js');
});

cluster.set('size', 2);
cluster.use('debug').use('signals');

// Start your application as a cluster!
cluster.run(function() {
  // `this` is pointing to the Master instance
});

jQuery Roundup: 2.1 Beta, dna.js

24 Sep 2013 | By Alex Young | Comments | Tags jquery plugins templates
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.

jQuery 1.11 and 2.1 Beta 1

jQuery 1.11 and 2.1 Beta 1 have been released. The biggest news is jQuery now supports AMD, and is built with it. Internal dependencies are also managed with Bower.

By using AMD and Bower the jQuery developers are showing that they’re receptive to the needs of modern client-side developers.

dna.js

dna.js (GitHub: dnajs / dna.js, License: GPLv3/MIT, jQuery: dna) by Dem Pilafian is a template library that uses valid HTML rather than a new syntax or inline JavaScript templates. It clones an element as many times as you need, and interpolates values.

A template looks like this:

<div>
  <h2>Featured Books</h2>
  <div id="book" class="dna-template">
    <div>Title:  <span>~~title~~</span></div>
    <div>Author: <span>~~author~~</span></div>
  </div>
</div>

And you can clone it with dna.clone('book', { title: 'The DOM', author: 'Jan' }).

Node PiGlow

23 Sep 2013 | By Alex Young | Comments | Tags node modules hardware
The PiGlow in action.

The PiGlow is a small board for the Raspberry Pi that adds 18 single colour LEDs. Each can be controlled through I2C over the GPIO header, and you can even dim LEDs.

node-piglow (GitHub: zaphod1984 / node-piglow, License: MIT, npm: piglow) by Manuel Ernst is a module for talking to the PiGlow. It uses i2c and allows you to address the LEDs like this:

piGlow.l_0_0 = 100; // lights up the first LED in the first LEG with a brightness of 100
piGlow.l_0_0; // lights up the first LED of the first LEG with maximum brightness (255)

piGlow.leg_0 = 100; // lights up the first leg (8 consecutive LEDs) with brightness of 100

piGLow.leg_0; // maximum brightness for the first leg

piGlow.ring_0 = 100; // sets LED 1 of leg 1, LED 1 of leg 2 and LED 1 of leg 3 to 100

iGlow.ring_0; // sets LED 1 of leg 1, LED 1 of leg 1 and LED 1 of leg 2 to 255

Manuel made node-piglow-load with it which uses the PiGlow to visualise system load. Since the API is very simple you could hook it up to lots of other interesting things, perhaps build status from your next big Node project. I’ve had my eye on the PiGlow for a few weeks for fun little hardware projects so I think node-piglow might make me get my wallet out.

JavaScript Y Combinator

20 Sep 2013 | By Michael Hurley | Comments | Tags functional guest-posts
This is a guest post by Michael Hurley. The original is here: JavaScript Y Combinator.

At the end of The Little Schemer, the authors lead you step-by-step through the process of deriving the Y combinator. They do this repeatedly abstracting a length function–and then magically, the Y combinator appears. It is a pretty neat trick, and certainly mind-bending on your first read through the book.

Since JavaScript has first-class functions, we can derive the Y combinator in JavaScript as well. I will take a different approach than The Little Schemer. This approach owes a lot to a couple of blog posts I read on the subject1.

Since this is a post about recursive functions, then we can use that old chestnut, factorial. Here is a possible implementation of factorial in JavaScript:

function basicFactorial(n) {
  return n === 0 ? 1 : n * basicFactorial(n-1);
}

Let’s start by making a non-recursive basicFactorial, which we’ll call nonRecursive:

function nonRecursive(f) {
  return function(n) {
    return n === 0 ? 1 : n * f(n-1);
  }
}

All we’ve done here is replace the recursive call of basicFactorial. Instead, we pass in a function that will get called. We can pass any function that returns something that supports the * operator:

nonRecursive(function(x) { return 0; })(100); // => 0
nonRecursive(function(x) { return 1; })(100); // => 100
nonRecursive(function(x) { return 10; })(100); // => 1000
// ... etc.

But it starts to get a little interesting when we pass basicFactorial in there. Then we get back … basicFactorial!

nonRecursive(basicFactorial)(4) === basicFactorial(4); 
nonRecursive(basicFactorial)(10) === basicFactorial(10); 
nonRecursive(basicFactorial)(17) === basicFactorial(17);
// ... etc.

In other words, basicFactorial is a fixed point of the function nonRecursive.

This is pointless here, since we have already defined basicFactorial. But suppose we had not defined basicFactorial. Wouldn’t it be nice if there was a function that we could pass nonRecursive to that would return the fixed point of it, i.e. the factorial function?

That is what the Y combinator does. Pass nonRecursive to Y, and out comes the factorial function:

Y(nonRecursive)(100); // 9.33262154439441e+157

Note that:

Y(nonRecursive)(100) === nonRecursive(basicFactorial)(100);

Or in other words:

Y(nonRecursive)(100) === nonRecursive(Y(nonRecursive))(100);

So if we have Y, we do not need to define basicFactorial at all, we let Y derive it from the non-recursive function nonRecursive. Now let’s look at it from the other direction, and build up to Y. Here again, is the functional nonRecursive that we want to calculate the fixed point of:

function nonRecursive(f) {
  return function(n) {
    return n === 0 ? 1 : n * f(n-1);
}
}

As noted above, pass basicFactorial in, and nonRecursive returns basicFactorial. Notice that we have pretty much defined factorial in the body of nonRecursive: return n === 0 ? 1 : n * f(n-1);–why not use that? So here’s our next try: Apply nonRecursive to itself. This requires a small change to the body of nonRecursive, to self-apply the passed-in function to get the body out and apply it to the inner argument.

function nonRecursive(f) {
  return function(n) {
    return n === 0 ? 1 : n * f(f)(n-1); 
  };
}
nonRecursive(nonRecursive)(5); // => 120

Now we want to isolate the fixed point function. Let’s wrap that in a function g:

function nonRecursive(f) {
  return function(x) {
    var g = function(q) {
      return function(n) {
        return n === 0 ? 1 : n * q(n-1);
      };
    };
    return g(f(f))(x);
  };
}

Since inner function g does not depend on anything in closure, we can pull it out:

function g(f) {
  return function(n) {
    return n === 0 ? 1 : n * f(n-1);
  };
}

The pulled-out function may look familiar–it’s nonRecursive again. Here’s what’s left over after g (a.k.a. nonRecursive) is pulled out; let’s call it almostY:

function almostY(f) {
  return function(x) {
    return g(f(f))(x);
  };
}
almostY(almostY)(5); // => 120

We’ve pulled g out of almostY, but almostY still depends on g. The final step is to wrap almostY in a function that takes the functional g as an argument. Then almostY will have no dependencies.

So, let’s wrap it in a function that takes our non-recursive factorial functional and returns the fixed point of it. And since this is the last step, let’s call that function Y:

function Y(f) {
  var p = function(h) {
    return function(x) {
      return f(h(h))(x);
    };
  };
  return p(p);
}

Y(g)(6); // => 720

Holy crap! It works! But it’s not just for factorial–Y will provide a fixed point for any unary function, e.g.

function nonRecursiveFibonacci(f) {
  return function(n) {
    return n < 2 ? n : f(n-1) + f(n-2); 
  };
}
Y(nonRecursiveFibonacci)(10); // => 55

As presented, this version of Y can only handle unary functions, and it will blow up the stack for relatively low values of n. It is straightforward to extend Y to handle functions of any arity, and to memoize it.

Notes

  1. I found these articles helpful: Fixed-point combinators in JavaScript: Memoizing recursive functions and Deriving the Y combinator.

Underscore, Array.prototype, and You

19 Sep 2013 | By The Angry JavaScript Nerd | Comments | Tags node underscore

What’s the most dependent module on npm? Underscore.js! Why do people use it? Presumably because they can’t be bothered to learn how JavaScript works.

There are things I like about Underscore. No global meddling, sane internals – it’s a solid piece of work. But I occasionally find myself working with programmers who are probably better than me yet have weaker JavaScript skills, and they reach for Underscore like a drunk reaching for cheap gin.

At this point I stay their hand and point to Annotated ECMAScript, highlighting the superpowers baked into Array.prototype. Granted it lacks some of the things Underscore has, but it often does what you want.

Mozilla’s documentation is also good because it shows you how to duplicate the functionality with lengthy code samples, which is educational if you take the time to read it.

If this is new to you or you’re a little uncomfortable with Array.prototype, start with forEach then map and some. They’re the methods that crop up in my own code a lot.

Another related JavaScript quirk is array type checking. Because JavaScript says typeof [1, 2, 3] is 'object' you might want to pack your bags and give up altogether. I don’t blame you. But hiding in that ES5 documentation is a beastie called Array.isArray. You’ll find it in util in Node, which is puzzling – you’ll be OK using Array.isArray([1, 2, 3]).

I don’t think it’s a bad thing to depend on Underscore, but it makes me feel like people don’t learn Node or indeed JavaScript well enough before flinging their modules up on npm.

Node Roundup: Bedecked, Knockout.sync.js, express-promise

18 Sep 2013 | By Alex Young | Comments | Tags node modules npm apps express knockout mvc

Bedecked

Who wants to write presentations in Keynote or PowerPoint? No one! That’s why Justin Russell made Bedecked (GitHub: jtrussell / bedecked, License: MIT, npm: bedecked):

A small module for quickly and simply authoring snazzy presentations in Markdown - or Jade, or vanilla HTML. Inspired by hackynote (thiagofelix/hackynote), Bedecked splits your Markdown into slides and spits them out as a single html file that you can share with Dropbox, S3, or whatever you like.

The command-line tool allows you to transform Markdown files into presentations. It uses stdout, so you can redirect the output into a .html file.

It can also run as a server which supports live reloading.

Knockout.sync.js

Knockout.sync.js (GitHub: imrefazekas / knockout.sync.js, License: MIT, npm: knockout.sync.js) by Imre Fazekas adds syncing to Knockout, supported by a Node server. This allows data to be persisted to a server and shared between clients as desired.

Timestamps can be used to allow clients to respond to outdated data, and Socket.IO is used in the client.

express-promise

express-promise (GitHub: luin / express-promise, License: MIT, npm: express-promise) by Zihua Li is middleware for folding repetitive asynchronous operations into leaner synchronous-style operations.

This is the kind of thing you’re probably used to writing:

app.get('/users/:userId', function(req, res) {
  User.find(req.params.userId).then(function(user) {
    Project.getMemo(req.params.userId).then(function(memo) {
      res.json({ user: user, memo: memo });
    });
  });
});

But with express-promise you can do this:

app.use(require('express-promise')());

app.get('/users/:userId', function(req, res) {
  res.json({
    user: User.find(req.params.userId),
    memo: Project.getMemo(req.params.userId)
  });
});

The readme has more documentation, with examples for Mongoose and Sequelize.

jQuery Roundup: persistState, CLNDR.js

17 Sep 2013 | By Alex Young | Comments | Tags jquery plugins widgets calendars time forms
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.

persistState

persistState (GitHub: togakangaroo / persistState, License: MIT) by George Mauer saves the state of UI widgets. It can be applied to a selector, so you can save the state of a specific set of widgets rather than all of them.

I like the idea of using this to save the state of form controls, particularly in a multi-stage form that’s handled client-side. Values are saved to localStorage, and the API of persistState allows you to swap in your own serialisation handler.

Saving the state of form controls looks like this:

$.ow.persistState.elementPersistence['textarea,input:not(:checkbox),select'] = {
  saveState: function($el) {
    return { val: $el.val() };
  },

  restoreState: function($el, state) {
    if (!state) return;
    if ($el.val() !== state.val)
      $el.val(state.val).trigger('change');
  }
};

CLNDR.js

CLNDR.js (GitHub: kylestetz / CLNDR, License: MIT) by Kyle Stetz is a calendar plugin:

There are wonderful and feature-rich calendar modules out there and they all suffer the same problem: they give you markup (and often a good heap of JS) that you have to work with and style. This leads to a lot of hacking, pushing, pulling, and annoying why-can’t-it-do-what-I-want scenarios.

CLNDR doesn’t generate markup (well, it has some reasonable defaults, but that’s an aside). Instead, CLNDR asks you to create a template and in return it supplies your template with a great set of objects that will get you up and running in a few lines.

CLNDR requires Moment.js, which is OK because I don’t leave home without it!

Writing for DailyJS

16 Sep 2013 | By Alex Young | Comments | Tags news

Every day we receive many submissions for Node modules, jQuery plugins, and weird and wonderful JavaScript projects. However, we’re also interested in your articles about JavaScript. They can be tutorials for projects you’ve created, or tutorials on well-known projects like AngularJS, Backbone.js, Express, and so on.

Articles don’t need to be long – three to five paragraphs is more than enough given how often we post. You can cross-post from your own blogs. We pay for original articles, and will negotiate a fee: as a guide we’ve paid $25 for short articles in the past.

If you’d like to write for us, send article ideas to contact.dailyjs.com.

Zephyros and node-zephyros

13 Sep 2013 | By Alex Young | Comments | Tags node modules mac

Intuitive, friendly, fast: none of these things describe window management on a Mac. Particularly if you use more than one screen with a laptop and occasionally unplug the second screen. What a mess! Anyway, those of us in such situations have adopted a variety of tools for forcing windows to behave. I currently use Spectacle, which is free and open source!

What about a programmatic API? Daniele Polencic sent in node-zephyros (GitHub: danielepolencic / node-zephyros, License: MIT), which is a Node module that talks to Zephyros:

The OS X window manager for hackers. At its core, Zephyros just runs quietly in your menu bar, and listens for your script. You typically write a script that binds global hot keys to do stuff, like moving or resizing windows.

You can bind keys to JavaScript callbacks, and even call the Zephyros API:

var Zephyros = require('zephyros');

var z = new Zephyros();

z.bind('t', ['Cmd', 'Shift']).clipboardContents().then(function(clipboard) {
  console.log('clipboard:', clipboard);
});

Daniele’s project originated from a talk at LNUG about Zephyros:

Originally the library was a nice excuse to get my hands dirty with Michael Fogus’ book, Functional JavaScript. It features lazy chains, promises and mixins, and the tests are written in Mocha.

If you’re wondering how the tests work, then take a look at mockServer.js. The module communicates with Zephyros using sockets, so it can be tested by creating a mock server that generates the expected responses.

Now you should be able to get those windows under control.

Static Site Generators for Yeoman

12 Sep 2013 | By The Angry JavaScript Nerd | Comments | Tags node modules yeoman rants

I often find myself being the only guy in the team who can make (or wants to make) a good ol’ fashioned website. No dynamic stuff, just a simple static marketing site to sell a product. “No problem,” I say confidently, dreaming up designs I can implement rapidly with Vim, Bootstrap, and Glyphish.

The problem I’ve ran into consistently over the last year or so is Yeoman doesn’t do what I think it does. This is what it does in my head:

  • Unites Grunt, Bower
  • Runs a little web server so I can see my site without having to run a web server
  • Doesn’t install any Ruby nonsense
  • Uses idiomatic Node

Here’s what it actually does when I use generator-webapp:

  • Installs loads of weird stuff I don’t need to do with testing and image optimisation
  • Make a Gruntfile.js that isn’t formatted using the coding style of most community Node projects
  • Seems to need Ruby due to Sass when I make it install Bootstrap

Then I realise generator-webapp might not be for me, so I try starting a project from scratch. Then I get into an incredible mess trying to automate the minimization of each Bower component’s JavaScript, CSS, and copying assets to a suitable location with Grunt.

I Miss Makefiles

Here’s how you copy a file in the shell:

* cp tmp/*/*.min.js site/js/

Here’s how you do it with Grunt:

copy: {
  dist: {
    files: [{
      expand: true,
      cwd: 'tmp/',
      src: ['*.min.js'],
      dest: 'site/js/',
      filter: 'isFile'
    },

Not only is it a whole bunch of lines to do something that should be simple, it also has weirdly named properties. I see isFile and start an internal monologue about everything being a file because it’s Unix.

I could write a Makefile in two lines that does this.

Yeoman Static Site Generators

This time I decided to persevere: I tried a bunch of static site generators for Yeoman.

  • Armadillo: Installed lots of stuff I didn’t need, and needed Ruby
  • Go Static: Was more for blogs than simple sites, and seemed to make files indented with tabs

There were more but I only have bad things to say about them. What I ended up with was this:

  • grunt-contrib-connect for running a web server. It was more complex than it needed to be because it defaults to exiting automatically rather than running a server, you need to specify a keepalive setting
  • grunt-contrib-concat for concatenating Bootstrap’s CSS, JavaScript, and any other dependencies in bower_components
  • grunt-contrib-copy for copying the files from bower_components to my website’s asset directories

The Shit Sandwich

I think the reason I have difficulty with Yeoman and Grunt is I see client-side development as “open source stuff” and “my stuff”. I want open source stuff poured out into buckets that I never look at, in a way that’s easy for other people to repeat should they want to install the dependencies fresh (I keep the files in the repository), or experiment with upgraded versions of each module.

Conversely, my stuff should be elegantly encapsulated with a module loader like RequireJS, kept separate and decoupled.

Instead of a neatly organized bento box with very clear sections I end up with a shit sandwich.

Node Roundup: 0.11.7, 0.10.18, npm-check-updates, social-cms-backend

11 Sep 2013 | By Alex Young | Comments | Tags node modules express npm

Node 0.11.7, 0.10.18

Node 0.11.7 and 0.10.18 were released last week. The unstable branch has seen changes across many of the core modules, particularly buffer and stream.

Meanwhile, 0.10.8 has fixes for stream and updates uv.

npm-check-updates

npm-check-updates (GitHub: tjunnone / npm-check-updates, License: Apache 2.0, npm: npm-check-updates) by Tomas Junnonen is a module for displaying out of date modules in your package.json. It can also automatically update package.json.

I tried it against a few projects and it works OK, but it struggled with private repository URIs – it just shows the 404 npm returns.

social-cms-backend

social-cms-backend (GitHub: dai-shi / social-cms-backend, License: BSD, npm: social-cms-backend) by Daishi Kato is Express middleware that provides REST APIs for creating a social networks using AngularJS.

It uses passport for authentication, so it can work with various social networks.

Once it’s installed you can set up persistence using MongoDB, and Facebook with passport’s Facebook strategy:

var express = require('express');
var SCB = require('social-cms-backend');
var app = express();
app.use(SCB.middleware({
  mongodb_url: 'mongodb://localhost:27017/socialcmsdb',
  passport_strategy: 'facebook',
  facebook_app_id: process.env.FACEBOOK_APP_ID,
  facebook_app_secret: process.env.FACEBOOK_APP_SECRET
}));
app.listen(3000);

jQuery Roundup: Mapael, Velge

10 Sep 2013 | By Alex Young | Comments | Tags jquery plugins widgets tags maps
Note: You can send your plugins and articles in for review through our contact form or @dailyjs.

Mapael

Mapael

Mapael (GitHub: neveldo / jQuery-Mapael, License: MIT) by Vincent Brouté is a Raphael-based vector map plugin:

With Mapael you can display a map of the world with clickable countries. You can build data visualisations by setting some parameters in order to automatically set a color to each area of your map and generate the legend. Moreover, you can plot cities on a map with their latitude and longitude.

The basic API looks like this:

$('.container').mapael({
  map: {
    name: 'world_countries'
  }
});

Vincent has some examples in the readme, for example, this map of France on JSFiddle.

Velge

Velge (GitHub: dscout / velge, License: MIT, bower: velge) by Parker Selbert is a tag widget, inspired by the tagging UI found in Pivotal Tracker.

It supports sorting, validation, data normalization, pattern matching, keyboard shortcuts, and callback methods for things like tag addition and removal.

The API is based around a constructor:

var velge = new Velge($('.container'), { single: true });

The instance will then accept callbacks like this:

velge
  .onAdd(addCallback)
  .onRem(remCallback)

It’s tested with Mocha and installable with Bower!

Drake: Password Syncing with Google Drive

09 Sep 2013 | By Alex Young | Comments | Tags google-drive security

Drake (GitHub: marksteve / drake, License: MIT) by Mark Steve Samson is a password management tool. It syncs with Google Drive, and uses SJCL for client-side encryption.

Drake

It has a simple password generation feature – it just creates a random string, so you can select a portion from it. It would be nice if it had some options for length and punctuation like 1Password.

The interesting thing about Drake is it’s entirely client-side. It uses Google’s APIs to sync data, but runs in your browser. Mark said he developed it using Chrome, so it could be a password management solution for Chromebook users who don’t want to sync all of their passwords using Chrome’s built-in system.

The code is organised using TJ Holowaychuk’s Component project, and can be built locally using make.