documentcloud/underscore — pull request #816 by krawaller (2012-10-07 13:56:08+00:00)
Title: Added '_.ensureArray' function

This pull request adds a `_.ensureArray` utility function, which we frequently found ourselves `mixin` into underscore. As the name implies, it helps when you want to feed a perhaps-array value into an API that expects an array:

```
// arrays are passed straight through
_.ensureArray([1,2,3]); // => [1,2,3]

// non-array truthy values are wrapped in an array
_.ensureArray({a:1,b:2}); // => [{a:1,b:2}]

// falsy values result in an empty array
_.ensureArray(null); // => []
```

We are perhaps treading a bit too far into the land of `_.toArray` here, but the functions behave very differently since `toArray` always treats the argument as a collection.