array of items
single item if array length is 1, otherwise the array
oneOrMany(['single']) // 'single'
oneOrMany(['multiple', 'items']) // ['multiple', 'items']
oneOrMany([]) // []
oneOrMany([42]) // 42
function findUsers(query: string): User | User[] {
const results = database.search(query);
return oneOrMany(results); // Return single user or array
}
const result = findUsers('john');
if (Array.isArray(result)) {
console.log(`Found ${result.length} users`);
} else {
console.log(`Found user: ${result.name}`);
}
Returns a single item if array has only one element, otherwise returns the array.
Unwraps single-item arrays to the item itself, useful for APIs that can return either single items or collections.