Skip to content

Commit

Permalink
feat: Word#mapParts
Browse files Browse the repository at this point in the history
  • Loading branch information
jGleitz committed Feb 11, 2020
1 parent d133391 commit e625860
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/kotlin/Word.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class Word(val parts: Sequence<String>) {
*/
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.
*/
Expand Down
19 changes: 18 additions & 1 deletion src/test/kotlin/WordTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit e625860

Please sign in to comment.