What is make-array?
The make-array package is a utility that helps in converting different types of values into arrays. It is particularly useful when you want to ensure that a value is always treated as an array, regardless of its original type.
What are make-array's main functionalities?
Convert single value to array
This feature allows you to convert a single non-array value into an array containing that value. It ensures that the input is always returned as an array.
const makeArray = require('make-array');
const singleValue = 'hello';
const array = makeArray(singleValue); // ['hello']
Convert array-like objects to array
This feature converts array-like objects (such as arguments objects or objects with a length property) into proper arrays.
const makeArray = require('make-array');
const arrayLike = {0: 'a', 1: 'b', length: 2};
const array = makeArray(arrayLike); // ['a', 'b']
Handle null or undefined
This feature ensures that null or undefined values are converted into empty arrays, providing a consistent array output.
const makeArray = require('make-array');
const result = makeArray(null); // []
const result2 = makeArray(undefined); // []
Other packages similar to make-array
arrayify
The arrayify package provides similar functionality by converting a value into an array. It is comparable to make-array in that it also handles single values, array-like objects, and null/undefined values, ensuring the output is always an array.
to-array
The to-array package is another utility that converts array-like objects to arrays. It is similar to make-array but focuses more on array-like objects rather than single values or null/undefined handling.
cast-array
The cast-array package from lodash is a utility that casts a value as an array if it's not already one. It is similar to make-array in its core functionality but is part of the larger lodash library, which offers a wide range of utility functions.

make-array
Creates a real Array from almost anything.
Install
$ npm i make-array --save
Usage
var makeArray = require('make-array');
makeArray();
makeArray(undefined);
makeArray(null);
makeArray(1);
makeArray([1, 2]);
makeArray({
'0': 1,
'1': 2,
length: 2
});
function foo (){
return makeArray(arguments);
}
foo(1, 2, 3);
makeArray(subject, [host])
- subject
mixed
things you want to make it an array - host
Array=
if host
is specified, the newly-created array will append to the end of the host
Returns Array
. If host
is specified, it will return the host
itself.
var host = [1, 2];
function foo(){
return arguments;
}
var result = makeArray(foo({}, []), host);
result;
result === host;
Changelog
1.0.0: bump version to mark it as stable.
License
MIT