Skip to content

Commit cd60b9a

Browse files
docs: added more details to the text-processor README.md and tests (#256)
1 parent 9f0203d commit cd60b9a

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

projects/arrays/text-processor/README.md

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ I'd like to set up code that can turn those pieces of text into print-ready line
1313
Before we can do any of our fancy typography work on text, we need to split text into lines.
1414
Much of our text data is stored in strings where the only whitespace is a single `" "` space between words.
1515

16+
More specifically: I'm going to give you an array of lines - each represented as a string.
17+
For each of those lines, give me back an array of strings (so the final result is an array of array of strings).
18+
Each of the strings in the array has as many of the words from the first line as it can fit, as long as there's a `" "` space between them.
19+
1620
Additionally, for each line, I'll need the ability to align it to the left, middle, or right.
1721
Alignment means adding spaces to the beginning and/or end of the string to fit it to a specified character width.
1822

@@ -47,19 +51,46 @@ Parameters:
4751

4852
Return type: An array of array of strings.
4953

50-
### Example
54+
### Examples
55+
56+
In this first example, each output string must be 3 characters long.
57+
The first string can fit `"ab"` but can't fit the additional word `"c"` because that would be 4 characters (`"ab c"`).
58+
The second string can git `"c"` and `"d"` with a space between them.
59+
60+
- Input: `alignTexts(["ab c d"], { width: 3 })`
61+
- Output:
62+
63+
```json
64+
[["ab ", "c d"]]
65+
```
66+
67+
Here, there are three lines to be split, and each becomes one of the arrays in the output array.
68+
`align: "right"` indicates that spaces must be added before characters (to their left), not after them (to their right).
69+
The first line can only fit one word (first `"ab"`, then `"cd"`) in the allowed 4 spaces.
70+
The second line again can only fit one word in its allowed spaces.
71+
The third line is the only one that can fit two words together: `"a"` and `"bc"`, which together with a space take up 4 characters.
5172

52-
- Input: `alignTexts(["ab cd", "abc def", "abcd ef"], { width: 4 })`
73+
- Input: `alignTexts(["ab cd", "abc def", "a bc def"], { align: "right", width: 4 })`
5374
- Output:
5475

5576
```json
5677
[
57-
["ab ", "cd "],
58-
["abc ", "def "],
59-
["abcd", "ef "]
78+
[" ab", " cd"],
79+
[" abc", " def"],
80+
["a bc", " def"]
6081
]
6182
```
6283

84+
This last example shows aligning text to the middle.
85+
If there's an extra space, it's added to the right (at the end).
86+
87+
- Input: `alignTexts(["a", "ab", "abc", "abcd"], { align: "middle", width: 4 })`
88+
- Output:
89+
90+
```json
91+
[[" a "], [" ab "], ["abc "], ["abcd"]]
92+
```
93+
6394
## Files
6495

6596
- `index.ts`: Add your `alignTexts` function and type annotations here

projects/arrays/text-processor/src/index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { expectType } from "tsd";
44
import * as index from "./index";
55
import * as solution from "./solution";
66

7+
process.env.TEST_SOLUTIONS = "1";
8+
79
const { alignTexts } = process.env.TEST_SOLUTIONS ? solution : index;
810

911
describe(alignTexts, () => {
@@ -75,6 +77,11 @@ describe(alignTexts, () => {
7577
[["abc def"], { align: "left", width: 9 }, [["abc def "]]],
7678
[["abc def"], { align: "left", width: 10 }, [["abc def "]]],
7779
[["abc def"], { align: "left", width: 11 }, [["abc def "]]],
80+
[
81+
["a", "ab", "abc", "abcd"],
82+
{ align: "middle", width: 4 },
83+
[[" a "], [" ab "], ["abc "], ["abcd"]],
84+
],
7885
[[""], { align: "middle", width: 0 }, [[""]]],
7986
[[""], { align: "middle", width: 1 }, [[" "]]],
8087
[[""], { align: "middle", width: 2 }, [[" "]]],

0 commit comments

Comments
 (0)