diff --git a/src/main/kotlin/Word.kt b/src/main/kotlin/Word.kt index b907abe..2b7bb1d 100644 --- a/src/main/kotlin/Word.kt +++ b/src/main/kotlin/Word.kt @@ -23,6 +23,11 @@ class Word(val parts: Sequence) { */ fun toNotation(notation: StringNotation) = notation.print(this) + /** + * Creates a new word, with all its parts transformed by the provided [transform] function. + */ + fun mapParts(transform: (String) -> String) = Word(parts.map(transform)) + /** * Appends a part to this word. */ diff --git a/src/test/kotlin/WordTest.kt b/src/test/kotlin/WordTest.kt index 99842e3..be1d88b 100644 --- a/src/test/kotlin/WordTest.kt +++ b/src/test/kotlin/WordTest.kt @@ -23,6 +23,23 @@ class WordTest { @Test fun `allows to add parts`() { - expect((Word("with") + "more" + "parts")).feature(Word::partsList).containsExactly("with", "more", "parts") + expect((Word("with") + "more" + "parts")) + .feature(Word::partsList) + .containsExactly("with", "more", "parts") + } + + @Test + fun `allows to add words`() { + expect(Word("with") + Word("more", "parts")) + .feature(Word::partsList) + .containsExactly("with", "more", "parts") + } + + @Test + fun `allows to transform parts`() { + expect(Word("a", "b", "c")) + .feature(Word::mapParts, String::toUpperCase) + .feature(Word::partsList) + .containsExactly("A", "B", "C"); } }