arrayiffy-if-string3.12.0

Put non-empty strings into arrays, turn empty-ones into empty arrays. Bypass everything else.

§ Quick Take

import { strict as assert } from "assert";
import arrayiffy from "arrayiffy-if-string";

assert.deepEqual(arrayiffy("aaa"), ["aaa"]);

assert.deepEqual(arrayiffy(""), []);

assert.equal(arrayiffy(true), true);

assert.equal(arrayiffy(), undefined);

§ Purpose

  • If it's a non-empty string, put it into an array and return it.
  • If it's empty string, return an empty array.
  • If it's anything else, just return it.

It's main purpose is to prepare the input argument options' objects.

Also, check out check-types-mini.

§ API

arrayiffy(something)

In other words, it's a function which takes one input argument.

§ Competition

Mr. Sorhus' arrify opens in a new tab API is slightly different, it casts everything to array (null into empty array, for example). arrayiffy-if-string on other hand, wraps only strings into arrays, bypassing the rest (null into null, for example).

It depends what you need.

§ Changelog

See it in the monorepo opens in a new tab, on Sourcehut.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 arrify opens in a new tab
Convert a value to an array
📦 check-types-mini 5.9.1
Check the types of your options object's values after user has customised them