@@ -13,6 +13,10 @@ I'd like to set up code that can turn those pieces of text into print-ready line
13
13
Before we can do any of our fancy typography work on text, we need to split text into lines.
14
14
Much of our text data is stored in strings where the only whitespace is a single ` " " ` space between words.
15
15
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
+
16
20
Additionally, for each line, I'll need the ability to align it to the left, middle, or right.
17
21
Alignment means adding spaces to the beginning and/or end of the string to fit it to a specified character width.
18
22
@@ -47,19 +51,46 @@ Parameters:
47
51
48
52
Return type: An array of array of strings.
49
53
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.
51
72
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 }) `
53
74
- Output:
54
75
55
76
``` json
56
77
[
57
- [" ab " , " cd " ],
58
- [" abc " , " def " ],
59
- [" abcd " , " ef " ]
78
+ [" ab " , " cd " ],
79
+ [" abc " , " def " ],
80
+ [" a bc " , " def " ]
60
81
]
61
82
```
62
83
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
+
63
94
## Files
64
95
65
96
- ` index.ts ` : Add your ` alignTexts ` function and type annotations here
0 commit comments