From e625860463be3cdbbc89a97dcb3e1910c20472f0 Mon Sep 17 00:00:00 2001 From: Joshua Gleitze Date: Tue, 11 Feb 2020 20:09:29 +0200 Subject: [PATCH] feat: Word#mapParts --- src/main/kotlin/Word.kt | 5 +++++ src/test/kotlin/WordTest.kt | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) 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"); } }