Giter VIP home page Giter VIP logo

ensure-array's Introduction

ensure-array

Simple convenience function which ensures that you are dealing with an array and you can eliminate noise from your code.

Build Status

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
    });
  }

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:

  1. if nothing passed to the function return empty array []
  2. if single argument passed is undefined or null return empty array []
  3. if single argument passed is already an array, return it unchanged
  4. otherwise return array containing all of the arguments

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
};

Installation

  npm install ensure-array

Usage

  var array = require('ensure-array');  // get handle to the function
  var foo = array(whatever);               // 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

ensure-array's People

Contributors

jeffbski avatar zertz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.