Installation
1. Install the latest version with yarn:
yarn add arrayiffy-if-string
or with npm:
npm install arrayiffy-if-string
2. Import it in your code:
import { arrayiffy } from "arrayiffy-if-string";
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 an empty string, return an empty array.
- If it’s anything else, just return it.
...including proper typings.
It’s main purpose is to prepare the input argument options’ objects.
API — arrayiffy()
The main function arrayiffy()
is imported like this:
import { arrayiffy } from "arrayiffy-if-string";
It is a function which takes one input argument:
function arrayiffy (something: T): StringInABox<T>;
Competition
Mr. Sorhus’ arrify
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.