# List

CLASS in `@skmtc/lang-typescript 0.19.1`

A powerful utility class for building formatted lists of stringable items.

The `List` class is a core component of the SKMTC DSL system, providing type-safe
construction of formatted lists with customizable separators and bookends. It's
extensively used throughout the codebase for generating code constructs like
function parameters, object properties, array literals, and more.


**Type Safety**


The class uses TypeScript's generic system to provide compile-time guarantees
about the list structure, separator, and bookend styles. This enables rich
type inference and helps prevent formatting errors.


**Common Patterns**


- **Objects**: `List.toObject()` creates `{key: value, ...}` structures
- **Arrays**: `List.toArray()` creates `[item, item, ...]` structures
- **Parameters**: `List.toParams()` creates `(param, param, ...)` structures
- **Lines**: `List.toLines()` creates newline-separated content

## Signature

```ts
import { List } from '@skmtc/lang-typescript'

class List<Values extends Stringable[] = Stringable[], Separator extends string = string, Bookends extends BookendsType = BookendsType>
```

- `Values`: Array of stringable values
- `Separator`: String used to separate items
- `Bookends`: Style of bookends ('[]', '{}', '()', 'none')

## Examples

Basic usage:

```ts
import { List } from '@skmtc/lang-typescript';

// Create a comma-separated list
const items = new List(['apple', 'banana', 'cherry']);
console.log(items.toString()); // "apple, banana, cherry"

// Create an array-style list
const array = new List(['a', 'b', 'c'], { bookends: '[]' });
console.log(array.toString()); // "[a, b, c]"

// Create an object-style list
const obj = new List(['x: 1', 'y: 2'], { bookends: '{}' });
console.log(obj.toString()); // "{x: 1, y: 2}"
```

Using type aliases:

```ts
// Type-safe array construction
const params: ListParams<string> = List.toParams(['id', 'name', 'email']);
console.log(params.toString()); // "(id, name, email)"

// Object properties with filtering
const props = List.toObject(['title', undefined, 'author'], { skipEmpty: true });
console.log(props.toString()); // "{title, author}"

// Line-separated content
const lines = List.toLines(['import React from "react"', 'import { useState } from "react"']);
console.log(lines.toString());
// import React from "react"
// import { useState } from "react"
```

Advanced usage with builders:

```ts
// Function signature generation
const signature = List.toParams([
  'id: string',
  'options?: RequestOptions',
  'callback?: (result: T) => void'
]);

// Interface properties
const interfaceProps = List.toObject([
  'readonly id: string',
  'name: string',
  'createdAt: Date',
  'updatedAt?: Date'
]);

console.log(`interface User ${interfaceProps}`);
// interface User {readonly id: string, name: string, createdAt: Date, updatedAt?: Date}
```

## Constructor

### constructor

```ts
constructor(
  values: (ExtractArrayItem<Values> | undefined)[],
  options?: ConstructorOptions<Separator, Bookends>
)
```

Creates a new List instance.

- `values`: Array of values (undefined values are automatically filtered out)
- `options`: Configuration options
- `options.separator`: String to use between items (default: ', ')
- `options.bookends`: Style of bookends to wrap the list (default: 'none')
- `options.skipEmpty`: Whether to return empty string for empty lists (default: false)

## Properties

- `values` (ExtractArrayItem<Values>[]): The array of values in this list
- `separator` (Separator): The separator string used between items
- `bookends` (Bookends): The bookend style for wrapping the list
- `skipEmpty` (boolean): Whether to skip rendering when the list is empty
- `List.toEmpty` (() => List<never, ', ', 'none'>): Create an empty List that will render as an empty string ''
- `List.toSingle` ((value: Stringable) => List<[Stringable], ', ', 'none'>): Create a List with a single value that will render as `value`
- `List.toConditional` (<Value extends Stringable, Condition extends boolean>(value: Value, condition: Condition) => ToConditionalReturn<List<[Value], ', ', 'none'>, Condition>): If condition is true, return a List with a single value that will render as `value`. Otherwise, return an empty List that will render as an empty string ''. Useful for conditional rendering. For example, rendering a function arg object when it has at least one property.
- `List.toRecord` ((record: Record<string, Stringable | Stringable[] | List>) => List<Stringable[], ', ', '{}'>): Create a record with Stringable values. KeyValue pairs will be joined with `: ` as separator and the resulting List will be wrapped with `{}`
- `List.toFilteredRecord` ((record: Record<string, undefined | Stringable | Stringable[] | List>) => List<Stringable[], ', ', '{}'>): Create a record with `undefined` or empty array or List values filtered out.
- `List.toKeyValue` (<Value extends Stringable | Stringable[] | List>(key: string, value: Value) => List<[string, Value], ': ', 'none'>): Join `key` and `value` using `: ` as separator
- `List.toObjectKey` (<Value extends Stringable>(key: string, value: Value) => List<[string, Value], '.', 'none'>): Join `key` and `value` using `.` as separator
- `List.toObject` (<V extends Stringable>(values: (V | undefined)[], args?: SkipEmptyOption) => ListObject<V>): Join `values` using `, ` as separator and wrap in `{` and `}`
- `List.toArray` (<V extends Stringable>(values: (V | undefined)[]) => ListArray<V>): Join `values` using `, ` as separator and wrap in `[` and `]`
- `List.toParams` (<V extends Stringable>(values: (V | undefined)[]) => ListParams<V>): Join `values` using `, ` as separator and wrap in `(` and `)`
- `List.toLines` (<V extends Stringable>(values: (V | undefined)[]) => ListLines<V>): Join `values` using `\n` as separator without a wrapper
- `List.fromKeys` ((record: Record<string, Stringable> | undefined) => KeyList): Create a KeyList using the keys of @param record
- `List.fromEntries` (<T extends Stringable>(record: Record<string, T> | undefined) => EntryList<T>): Create a EntryList using the entries of @param record
- `List.toFilteredKeyValue` (<Value extends undefined | Stringable | Stringable[] | List>(key: string, value: Value) => ToFilteredKeyValueReturn<Value>): Create a keyValue pair if `value` is not undefined and is not an empty array or List. Return `undefined` otherwise. Useful for filtering out `undefined values from a record.
- `List.hasValue` ((value: undefined | Stringable | Stringable[] | List) => value is Stringable | Stringable[] | List): Check if `value` is not undefined and is not an empty array or List.

## Methods

### toString

```ts
toString(): string
```

Converts the list to its string representation.

Joins all values with the configured separator and wraps with bookends.
Returns empty string if skipEmpty is enabled and list is empty.

Returns: Formatted string representation of the list
