Skip to content

Commit 838376a

Browse files
authored
Merge pull request #26 from jGleitz/word-map-parts
feat: Word#mapParts
2 parents d133391 + e625860 commit 838376a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/main/kotlin/Word.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class Word(val parts: Sequence<String>) {
2323
*/
2424
fun toNotation(notation: StringNotation) = notation.print(this)
2525

26+
/**
27+
* Creates a new word, with all its parts transformed by the provided [transform] function.
28+
*/
29+
fun mapParts(transform: (String) -> String) = Word(parts.map(transform))
30+
2631
/**
2732
* Appends a part to this word.
2833
*/

src/test/kotlin/WordTest.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ class WordTest {
2323

2424
@Test
2525
fun `allows to add parts`() {
26-
expect((Word("with") + "more" + "parts")).feature(Word::partsList).containsExactly("with", "more", "parts")
26+
expect((Word("with") + "more" + "parts"))
27+
.feature(Word::partsList)
28+
.containsExactly("with", "more", "parts")
29+
}
30+
31+
@Test
32+
fun `allows to add words`() {
33+
expect(Word("with") + Word("more", "parts"))
34+
.feature(Word::partsList)
35+
.containsExactly("with", "more", "parts")
36+
}
37+
38+
@Test
39+
fun `allows to transform parts`() {
40+
expect(Word("a", "b", "c"))
41+
.feature(Word::mapParts, String::toUpperCase)
42+
.feature(Word::partsList)
43+
.containsExactly("A", "B", "C");
2744
}
2845
}

0 commit comments

Comments
 (0)