single-or-array-pattern

Normalize single items or arrays into uniform arrays for processing.

35|4|Updated Dec 18, 2024
One-click install
npx skills add https://github.com/wellcrafted-dev/wellcrafted --skill single-or-array-pattern-wellcrafted-dev
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: single-or-array-pattern
Source: https://github.com/wellcrafted-dev/wellcrafted/tree/main/.claude/skills/single-or-array-pattern
Command: npx skills add https://github.com/wellcrafted-dev/wellcrafted --skill single-or-array-pattern-wellcrafted-dev

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides a robust pattern for functions that need to accept either a single value or an array of values, simplifying API design and reducing code duplication.

Core Features & Use Cases

  • Input Normalization: Seamlessly handles both single items and arrays, converting them to a uniform array format internally.
  • Unified Logic: Allows a single implementation path for processing one or many inputs.
  • Use Case: When designing a function to create users, you can allow callers to pass a single User object or an array of User objects, and the function will process them all efficiently.

Quick Start

Use the single-or-array-pattern skill to create a function that accepts a single item or an array of items.

Frequently Asked Questions about single-or-array-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create a TypeScript function that accepts either a single item or an array?

To accept either a single item or an array, you can normalize the input by converting single values into arrays internally. This standardizes parameter types, allowing a single implementation path to process one or many inputs uniformly without code duplication.

What is the single or array pattern for API design?

The single or array pattern is a design technique for API design where functions accept flexible input types. It normalizes single items and arrays into a uniform array format internally, simplifying caller syntax and standardizing internal logic paths for batch processing.

Why does my function logic duplicate when handling single values versus arrays?

Function logic duplicates because separate code branches are written for single and array inputs. You can eliminate this by normalizing inputs to arrays initially, allowing a unified logic path to process both input types efficiently without branching.

Can I use this input normalization pattern for CRUD operations and batch processing?

Yes, this input normalization pattern is applicable to CRUD operations, batch processing, and factory functions. It standardizes parameter types to handle flexible input requirements, allowing the same function to process individual records or batches.

What is the best way to standardize TypeScript function parameters for flexible inputs?

The best way to standardize flexible inputs is to normalize parameters by wrapping single items into arrays immediately upon function entry. This reduces code duplication and ensures uniform processing logic regardless of the original input shape.