Join non-empty array elements into a string
$ npm install --save join-non-empty-array
const joinArray = require("join-non-empty-array");
// Join, but ignore the empty element
joinArray([1, , 3, 4]);
//=> "1,3,4"
// Built-in join method
[1, , 3, 4].join();
//=> "1,,3,4"
// Using flat ignores empty slots; but not slots with falsy values
[0, , ' ', undefined, null, 2,3].flat().join();
//=> "0, ,,,2,3"
joinArray([0, , ' ', undefined, null, 2,3]);
//=> "0, ,undefined,null,2,3"
const options = {ignoreFalsy: true, ignoreWhiteSpace: true};
joinArray([0, , ' ', undefined, null, 2,3], options);
//=> "0,2,3"
Join an array into a string with options.
array
{Array} (Required): Array to join together.joiner
{String} (Optional; default =","
): String used to join elements of the array.options
{Object} (Optional).
Remove white space in array elements before determining if it is empty. The elements are not modified.
Type: boolean
default is set to false
const array = [
"\t", // empty when whitespace is ignored
"1 ", // not modified
"\t2" // not modified
];
joinArray(array);
//=> "\t,1 ,\t2"
joinArray(array, ",", {ignoreWhiteSpace: true});
//=> "1 ,\t2"
Remove white space from all elements in the array before joining.
Type: boolean
default is set to false
const array = [
"\t", // empty when whitespace is trimmed
"1 ", // white space removed
"\t2" // white space removed
];
joinArray(array);
//=> "\t,1 ,\t2"
joinArray(array, ",", {trimEntries: true});
//=> "1,2"
Treat falsy values (except zero!) as an empty element.
Type: boolean
default is set to false
const array = [
1,
null, // falsy
"2",
0, // falsy, but NOT treated as such!
undefined, // falsy
false // umm, yeah falsy
];
joinArray(array);
//=> "1,null,2,0,undefined,false"
joinArray(array, ",", {ignoreFalsy: true});
//=> "1,2,0" // zero is not treated as a falsy value!
Append the joiner to the end of the joined array
Type: boolean
default is set to false
const array = [
"", // empty when whitespace is trimmed away
"1 ", // not modified
" 2" // not modified
];
joinArray(array);
//=> "1 , 2"
joinArray(array, ",", {appendJoiner: true});
//=> "1 , 2,"
The library uses flat
set to this depth - Added in v1.1.0.
Type: number
default is set to Infinity
Note flat()
removes empty slots in the array to the set depth. Any
nested array(s) of greater depth are automatically flattened when join()
is
applied.
Set this option to zero (0
) to skip using the flatten function
const array = [0, , 1, " ", [" 3", null, [, 5, undefined, [, 7]]]];
array.join();
//=> "0,,1, , 3,,,5,,,7"
array.flat(Infinity).join();
//=> "0,1, , 3,,5,,7"
joinArray(array, "x", {flattenDepth: 0});
//=> "0x1x x 3,,,5,,,7"
joinArray(array, "x", {flattenDepth: 1});
//=> "0x1x x 3xnullx,5,,,7"
joinArray(array, "x", {flattenDepth: 2});
//=> "0x1x x 3xnullx5xundefinedx,7"
joinArray(array, "x", {flattenDepth: 3});
//=> "0x1x x 3xnullx5xundefinedx7"
joinArray(array, "x"); // flattenDepth = Infinity by default
//=> "0x1x x 3xnullx5xundefinedx7"
const joinArray = require("join-non-empty-array");
// params: array, joiner, options
console.log(joinArray([0, 1, " " , 2, 3], ";", {ignoreWhiteSpace: true}));
//=> "0;1;2;3"
const opts = {
ignoreWhiteSpace: true,
trimEntries: true,
ignoreFalsy: true,
appendJoiner: true,
flattenDepth: Infinity // default value
};
let array = ["\na", NaN, "b\n", 0, " ", "d\t", "\t\n", "\tf\n "];
console.log(joinArray(array, "-", opts));
//=> "a-b-0-d-f-"
array = [0, , 1, " ", [" 3", null, [, 5, undefined, [, 7]]]];
console.log(joinArray(array, "x", opts));
//=> "0x1x3x5x7x"
MIT