Security News
Highlights from the 2024 Rails Community Survey
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
ensure-array
Advanced tools
Ensure that an object is an array. Moves error checking out of your code.
Simple convenience function which ensures that you are dealing with an array and you can eliminate noise from your code.
For Example:
var array = require('ensure-array');
function foo(bar) {
array(bar).forEach(function (x) {
//do something with each item
});
}
Instead of doing something like this:
function foo(bar) {
if (bar === undefined) return;
if (bar === null) return;
if (!Array.isArray(bar)) bar = [bar];
bar.forEach(function (x) {
//do something with each item
});
}
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:
Here is the actual code which makes it happen
module.exports = function array(a, b, n) {
if (arguments.length === 0) return []; //no args, ret []
if (arguments.length === 1) { //single argument
if (a === undefined || a === null) return []; // undefined or null, ret []
if (Array.isArray(a)) return a; // isArray, return it
}
return Array.prototype.slice.call(arguments); //return array with copy of all arguments
};
npm install ensure-array
var array = require('ensure-array'); // get handle to the function
var foo = array(whatever); // foo will now safely be an array
FAQs
Ensure that an object is an array. Moves error checking out of your code.
We found that ensure-array demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.