Writing code
List
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
import { List } from '@skmtc/lang-typescript'
class List<Values extends Stringable[] = Stringable[], Separator extends string = string, Bookends extends BookendsType = BookendsType>Array of stringable values
String used to separate items
Style of bookends ('[]', '{}', '()', 'none')
Examples
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}"// 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"// 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
constructor(
values: (ExtractArrayItem<Values> | undefined)[],
options?: ConstructorOptions<Separator, Bookends>
)Creates a new List instance.
Array of values (undefined values are automatically filtered out)
Configuration options
String to use between items (default: ', ')
Style of bookends to wrap the list (default: 'none')
Whether to return empty string for empty lists (default: false)
Properties
The array of values in this list
The separator string used between items
The bookend style for wrapping the list
Whether to skip rendering when the list is empty
Create an empty List that will render as an empty string ''
Create a List with a single value that will render as value
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.
Create a record with Stringable values. KeyValue pairs will be joined with : as separator and the resulting List will be wrapped with {}
Create a record with undefined or empty array or List values filtered out.
Join key and value using : as separator
Join key and value using . as separator
Join values using , as separator and wrap in { and }
Join values using , as separator and wrap in [ and ]
Join values using , as separator and wrap in ( and )
Join values using \n as separator without a wrapper
Create a KeyList using the keys of @param record
Create a EntryList using the entries of @param record
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.
Check if value is not undefined and is not an empty array or List.
Methods
toString
toString(): stringConverts 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.
Formatted string representation of the list