Skip to content

yuvimittal/text-processor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Text Processor

A small TypeScript exercise for working with arrays of strings and simple text layout.

This project helps you take raw text lines and break them into fixed-width lines suitable for printing or simple typesetting. Each original line is split into one or more output strings where each output string contains as many whole words as will fit within a specified character width (words in the input are separated by single spaces).

In addition to breaking lines, the processor can pad the resulting strings so they are aligned left, right, or centered within the given width.

Quick overview

  • Input: an array of string lines (each line contains words separated by single spaces).
  • Output: an array where each input line is transformed into an array of strings; each string is at most width characters long and contains full words with single-space separators.
  • Alignment: the optional align option controls whether padding (spaces) is added on the left, right, or both sides (center). Centering places any extra space on the right when an odd number of padding spaces is needed.

Getting started

  1. In one terminal start the TypeScript compiler in watch mode:
npm run tsc -- --watch
  1. In another terminal run the tests (also can be started in watch mode):
npm run test -- --watch

Specification

Implement and export a function named alignTexts in index.ts with these parameters:

  1. texts: string[] — an array of input lines
  2. options: { align?: 'left' | 'middle' | 'right'; width: number }

Return value: string[][] — for each input line, an array of output lines (each output line being a string of length up to width, padded according to align).

Important behavior notes:

  • Splitting: use text.split(" ") — words are always separated by a single space in inputs.
  • Wrapping: put as many words in each output string as can fit (including the single space between words).
  • Padding: if an output string is shorter than width, add spaces to satisfy the alignment rule:
    • left (default): pad on the right
    • right: pad on the left
    • middle: distribute padding to both sides; if there's an odd extra space, put it on the right side

Examples

  1. Example with width 3:

Input: alignTexts(["ab c d"], { width: 3 })

Output:

[ ["ab ", "c d"] ]

Explanation: the first output can contain "ab" (plus a trailing space). Adding "c" would exceed width. The second output contains "c d".

  1. Example with right alignment and width 4:

Input: alignTexts(["ab cd", "abc def", "a bc def"], { align: "right", width: 4 })

Output:

[
  ["  ab", "  cd"],
  [" abc", " def"],
  ["a bc", " def"]
]
  1. Center alignment (extra space goes to the right):

Input: alignTexts(["a", "ab", "abc", "abcd"], { align: "middle", width: 4 })

Output:

[ [" a  "], [" ab "], ["abc "], ["abcd"] ]

Files in this repo

  • index.ts — implement alignTexts and add TypeScript annotations
  • index.test.ts / index-alternate.test.ts — tests that validate the requirements
  • solution.ts and solution-alternate.ts — example solution(s)

Notes and hints

  • You can rely on text.split(" ") for tokenizing words because inputs use a single space between words.
  • Keep the function signature and return type exactly as specified so tests can type-check the implementation.

About

A simple text processor written in Typescript!

Topics

Resources

Stars

Watchers

Forks