Skip to content

List Converter — TypeScript source

Transform a list between separators (newline, comma, space, pipe, semicolon, tab) with trim, dedupe, sort, and empty-removal options. Runs entirely in your browser, with a shareable link.

This is the TypeScript implementation — the same logic the interactive tool runs, in a shareable, citable form.

// Pure list converter - split/join items between separators with trim, dedup,
// and sort options. Zero deps - the unit-test surface for the List Converter tool.

export type Separator = 'comma' | 'newline' | 'space' | 'pipe' | 'semicolon' | 'tab';

const SEP_CHAR: Record<Separator, string> = {
  comma: ',',
  newline: '\n',
  space: ' ',
  pipe: '|',
  semicolon: ';',
  tab: '\t',
};

export interface ListOptions {
  from?: Separator | string;
  to?: Separator | string;
  trim?: boolean;
  removeEmpty?: boolean;
  unique?: boolean;
  sort?: boolean;
  caseInsensitive?: boolean;
}

function resolveSep(s: Separator | string | undefined, fallback: Separator): string {
  if (s === undefined) return SEP_CHAR[fallback];
  if (s in SEP_CHAR) return SEP_CHAR[s as Separator];
  return s; // treat as a literal custom separator string
}

export function convertList(input: string, opts: ListOptions = {}): string {
  const fromCh = resolveSep(opts.from, 'newline');
  const toCh = resolveSep(opts.to, 'comma');

  let items = input.split(fromCh);
  if (opts.trim) items = items.map((s) => s.trim());
  if (opts.removeEmpty) items = items.filter((s) => s !== '');
  if (opts.unique) {
    const seen = new Set<string>();
    items = items.filter((s) => {
      const key = opts.caseInsensitive ? s.toLowerCase() : s;
      if (seen.has(key)) return false;
      seen.add(key);
      return true;
    });
  }
  if (opts.sort) {
    items = [...items].sort((a, b) =>
      opts.caseInsensitive ? a.toLowerCase().localeCompare(b.toLowerCase()) : a.localeCompare(b),
    );
  }
  return items.join(toCh);
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →