or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-arrify

Convert a value to an array

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/arrify@3.0.x

To install, run

npx @tessl/cli install tessl/npm-arrify@3.0.0

index.mddocs/

Arrify

Arrify provides a simple utility function that converts any value to an array format. It handles various input types including null/undefined (returns empty array), existing arrays (returns as-is), strings (wraps in array), and iterable objects like Sets (converts to array using spread operator).

Package Information

  • Package Name: arrify
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install arrify

Core Imports

import arrify from "arrify";

Note: This package is ES module only. CommonJS is not supported starting from v3.0.0.

Basic Usage

import arrify from "arrify";

// Convert string to array
arrify('πŸ¦„');
//=> ['πŸ¦„']

// Arrays are returned as-is
arrify(['πŸ¦„']);
//=> ['πŸ¦„']

// Convert Set to array
arrify(new Set(['πŸ¦„']));
//=> ['πŸ¦„']

// Null and undefined become empty arrays
arrify(null);
//=> []

arrify(undefined);
//=> []

Capabilities

Array Conversion

Converts any value to an array using intelligent type-based conversion rules.

/**
 * Convert a value to an array.
 * @param {any} value - The value to convert to an array
 * @returns {Array} The converted array
 */
function arrify(value);

Conversion Rules:

  • null or undefined β†’ empty array []
  • Existing array β†’ returns the same array (identity)
  • String β†’ wraps in array [string]
  • Iterable objects (Set, Map, etc.) β†’ spreads into array using [...value]
  • Any other value β†’ wraps in array [value]

Usage Examples:

import arrify from "arrify";

// String conversion
const stringResult = arrify('hello');
// Result: ['hello']

// Boolean conversion
const boolResult = arrify(true);
// Result: [true]

// Number conversion
const numResult = arrify(42);
// Result: [42]

// Object conversion
const objResult = arrify({ key: 'value' });
// Result: [{ key: 'value' }]

// Map conversion (iterable)
const mapResult = arrify(new Map([[1, 2], ['a', 'b']]));
// Result: [[1, 2], ['a', 'b']]

// Set conversion (iterable)
const setResult = arrify(new Set([1, 2, 3]));
// Result: [1, 2, 3]

// Array identity (returns same reference)
const arr = ['existing'];
const arrayResult = arrify(arr);
// Result: ['existing'] (same reference as arr)
console.log(arrayResult === arr); // true

TypeScript Support

This package includes comprehensive TypeScript definitions with advanced generic types:

/**
 * Convert a value to an array.
 * Specifying null or undefined results in an empty array.
 */
function arrify<ValueType>(
  value: ValueType
): ValueType extends (null | undefined)
  ? []
  : ValueType extends string
    ? [string]
    : ValueType extends readonly unknown[]
      ? ValueType
      : ValueType extends Iterable<infer T>
        ? T[]
        : [ValueType];

The TypeScript types provide precise return type inference based on the input type:

  • arrify(null) β†’ []
  • arrify(undefined) β†’ []
  • arrify('text') β†’ [string]
  • arrify(['a', 'b']) β†’ string[] (preserves array type)
  • arrify(new Set<number>([1, 2])) β†’ number[]
  • arrify(42) β†’ [number]