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
widthcharacters long and contains full words with single-space separators. - Alignment: the optional
alignoption 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
- In one terminal start the TypeScript compiler in watch mode:
npm run tsc -- --watch- In another terminal run the tests (also can be started in watch mode):
npm run test -- --watchSpecification
Implement and export a function named alignTexts in index.ts with these parameters:
texts: string[] — an array of input linesoptions: { 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 rightright: pad on the leftmiddle: distribute padding to both sides; if there's an odd extra space, put it on the right side
Examples
- 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".
- 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"]
]- 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— implementalignTextsand add TypeScript annotationsindex.test.ts/index-alternate.test.ts— tests that validate the requirementssolution.tsandsolution-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.