Given a text file of many lines, where fields within a line are delineated by a single $
character, write a program that aligns each column of fields by ensuring that words in each column are separated by at least one space. Further, allow for each word in a column to be either left justified, right justified, or center justified within its column.
Use the following text to test your programs:
Given$a$text$file$of$many$lines where$fields$within$a$line$ are$delineated$by$a$single$'dollar'$character write$a$program that$aligns$each$column$of$fields by$ensuring$that$words$in$each$ column$are$separated$by$at$least$one$space. Further,$allow$for$each$word$in$a$column$to$be$either$left$ justified,$right$justified or$center$justified$within$its$column.
Note that:
The example input texts lines may, or may not, have trailing dollar characters. All columns should share the same alignment. Consecutive space characters produced adjacent to the end of lines are insignificant for the purposes of the task. Output text will be viewed in a mono-spaced font on a plain text editor or basic terminal. The minimum space between columns should be computed from the text and not hard-coded. It is not a requirement to add separating characters between or around columns.formatText
is a function.
testString: assert(typeof formatText === 'function', 'formatText
is a function.');
- text: 'formatText
with the above input and "right" justification should produce the following: '
testString: 'assert.strictEqual(formatText(testInput, ''right''), rightAligned, ''formatText
with the above input and "right" justification should produce the following: '');'
- text: 'formatText
with the above input and "left" justification should produce the following: '
testString: 'assert.strictEqual(formatText(testInput, ''left''), leftAligned, ''formatText
with the above input and "left" justification should produce the following: '');'
- text: 'formatText
with the above input and "center" justification should produce the following: '
testString: 'assert.strictEqual(formatText(testInput, ''center''), centerAligned, ''formatText
with the above input and "center" justification should produce the following: '');'
```