ensure-array
Simple convenience function which ensures that you are dealing with an array and you can eliminate noise from your code.
For Example:
var array = ; { ; }
Instead of doing something like this:
{ if bar === undefined return; if bar === null return; if !Array bar = bar; bar; }
Description
It gets rid of the noise and coerces what is provided into an array, so you do not have to litter your code with a bunch of extraneous checks.
Here is the logic behind the function:
- if nothing passed to the function return empty array []
- if single argument passed is undefined or null return empty array []
- if single argument passed is already an array, return it unchanged
- otherwise return array containing all of the arguments
Here is the actual code which makes it happen
module { if argumentslength === 0 return ; //no args, ret [] if argumentslength === 1 //single argument if a === undefined || a === null return ; // undefined or null, ret [] if Array return a; // isArray, return it return Arrayprototypeslice; //return array with copy of all arguments};
Installation
npm install ensure-array
Usage
var array = ; // get handle to the function var foo = ; // foo will now safely be an array
Status
- 2017-11-02 - 1.0.0 - Modernized by @Zertz
- 2011-12-08 - 0.0.4 - Update tapr / tap versions
- 2011-12-01 - 0.0.3 - Updated to support any version of Node.js
License
Contributors
- Author: Jeff Barczewski (@jeffbski)
- Modernized on 2017-11-02 by Pier-Luc Gendreau (@Zertz)
Contributing
- Source code repository: http://github.com/jeffbski/ensure-array
- Ideas and pull requests are encouraged - http://github.com/jeffbski/ensure-array/issues
- You may contact me at @jeffbski or through github at http://github.com/jeffbski