single-or-array-pattern

Normalize single items into one-element arrays for unified processing.

4.7k|374|Updated Mar 16, 2023
One-click install
npx skills add https://github.com/EpicenterHQ/epicenter --skill single-or-array-pattern
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: single-or-array-pattern
Source: https://github.com/EpicenterHQ/epicenter/tree/main/.agents/skills/single-or-array-pattern
Command: npx skills add https://github.com/EpicenterHQ/epicenter --skill single-or-array-pattern

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Pattern to support both single items and arrays by normalizing inputs to an array and delegating to an array-based implementation.

Core Features & Use Cases

  • Normalize single inputs to one-element arrays automatically.
  • Provide a single internal path for bulk processing.
  • Simplify API design by offering one flexible entry point for one or many items.

Quick Start

Normalize a single item or an array to an internal array and call the shared implementation.

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 normalize API inputs to arrays for robust batch processing in TypeScript?

You normalize API inputs by converting single items into one-element arrays using Array.isArray checks. This approach enforces a single internal array-based pathway, allowing CRUD operations and factory functions to handle one or many inputs efficiently.

What is the overload pattern for handling single or array inputs in API design?

The overload pattern for API design normalizes single inputs to one-element arrays automatically, delegating them to a shared array-based implementation. This provides a single flexible entry point and avoids maintaining separate code paths for single and bulk operations.

Why does my batch processing API fail when a single item is passed instead of an array?

Batch processing APIs often fail with single items because downstream code expects array methods. Normalizing inputs by wrapping single items into one-element arrays before processing ensures all data flows through a single consistent array-based implementation path.

What's the best way to simplify CRUD operations that must handle one or many items?

The best way to simplify CRUD operations is normalizing inputs to arrays at the entry point. By converting single items to one-element arrays, you provide a single internal path for bulk processing, reducing branching logic and simplifying your API design.

Can I use the single-or-array pattern for factory functions in TypeScript?

Yes, you can use the single-or-array pattern for factory functions. It normalizes inputs by converting single items into one-element arrays, allowing factory functions to process one or many inputs through a single array-based implementation path.

When should I not use array normalization for API inputs?

You should avoid array normalization for API inputs when the distinction between a single item and an array carries specific semantic meaning for your downstream processing. If wrapping a single item changes the operational logic, maintaining separate paths may be necessary.