Skip to content

Commit

Permalink
fix: don't replace / for (), consider then part of the chord instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Tgo1014 committed Jun 23, 2024
1 parent a697d2e commit f82d601
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ afterEvaluate {
from components.java
groupId = 'com.github.Tgo1014'
artifactId = 'Khord'
version = '1.1.0'
version = '1.1.1'
}
}
}
Expand Down
39 changes: 15 additions & 24 deletions src/main/kotlin/tgo1014/khord/Khord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,39 +128,30 @@ public object Khord {
}

private fun String.splitIntoWordsWithIndexes(): List<TextWord> {
var finalWord = this
val regex = "[^\\s\\n()]+".toRegex() // find anything but spaces and new lines
val parenthesisIndexList = finalWord.substringIndices("(", ")")
parenthesisIndexList.forEach {
val substring = finalWord.substring(it.first + 1, it.second) // Start is inclusive, so +1 to get just the content
if (substring.toIntOrNull() != null) {
finalWord = finalWord.replaceRange(it.first, it.first + 1, "/")
finalWord = finalWord.replaceRange(it.second, it.second + 1, " ")
}
}
return regex.findAll(finalWord).map {
val regex = "[^\\s\\n]+".toRegex() // find anything but spaces and new lines
val wordList = regex.findAll(this).map {
TextWord(
word = it.value,
startIndex = it.range.first,
endIndex = it.range.last + 1,
)
}.toList()
}

private fun String.substringIndices(startChar: String, endChar: String): List<Pair<Int, Int>> {
val substringIndexesList = mutableListOf<Pair<Int, Int>>()
var startIndex = -1
this.forEachIndexed { index, c ->
if (c.toString() == startChar) {
startIndex = index
return@forEachIndexed
val finalList = mutableListOf<TextWord>()
// When we have parenthesis, but they are not part of a chord, split them into their own TextWord
wordList.forEach {
if (it.word.first() == '(' && !it.word.contains(")")) {
finalList.add(TextWord(word = "(", startIndex = it.startIndex, endIndex = it.startIndex + 1))
finalList.add(TextWord(word = it.word.removeRange(0..0), startIndex = it.startIndex + 1, endIndex = it.endIndex))
return@forEach
}
if (c.toString() == endChar && startIndex != -1) {
substringIndexesList.add(startIndex to index)
startIndex = -1
if (it.word.last() == ')' && !it.word.contains("(")) {
finalList.add(TextWord(word = it.word.substring(0..<it.word.lastIndex), startIndex = it.startIndex, endIndex = it.endIndex - 1))
finalList.add(TextWord(word = ")", startIndex = it.endIndex - 1, endIndex = it.endIndex))
return@forEach
}
finalList.add(it)
}
return substringIndexesList.toList()
return finalList
}

private fun String.fixWeirdLineBreaks() = replace("\"", "")
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/tgo1014/khord/models/ChordRoot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ public enum class ChordRoot(

private val chordMap = mutableMapOf<String, ChordRoot>()
.apply {
values().forEach { chordRoot ->
ChordRoot.entries.forEach { chordRoot ->
chordRoot.allAliasList.forEach {
this[it] = chordRoot
}
}
}
.toMap()

val allChords = values()
val allChords = entries
.flatMap { it.aliasList + it.root }
.sortedByDescending { it.length }

fun asCircularList() = CircularList(values().toList())
fun asCircularList() = CircularList(entries)

fun from(chordString: String): ChordRoot {
val two = chordString.take(2)
Expand Down
24 changes: 15 additions & 9 deletions src/test/kotlin/tgo1014/khord/KhordTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tgo1014.khord

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import tgo1014.khord.models.Chord
import tgo1014.khord.models.ChordRoot
Expand Down Expand Up @@ -94,9 +93,9 @@ class KhordTest {
@Test
fun `GIVEN chords with parenthesis WHEN searching chords THEN return proper chords`() {
val result = Khord.find("(C F# G)")
assertTrue {
result[0].chord == "C" && result[1].chord == "F#" && result[2].chord == "G"
}
assertEquals("C", result[0].chord )
assertEquals("F#", result[1].chord )
assertEquals("G", result[2].chord )
}

@Test
Expand Down Expand Up @@ -134,15 +133,22 @@ class KhordTest {
@Test
fun `GIVEN chord with parenthesis WHEN searching for it THEN replace it with slash`() {
val result = Khord.find("G6(9)")
assert(result.first().chord == "G6/9")
assertEquals("G6(9)", result.first().chord)
}

@Test
fun `GIVEN string with multiple parenthesis WHEN find chords THEN map chords properly and ignore text`() {
val result = Khord.find("G6(9) G6(9) (test)")
assert(result[0].chord == "G6/9")
assert(result[1].chord == "G6/9")
assert(result.getOrNull(2) == null)
assertEquals("G6(9)", result[0].chord)
assertEquals("G6(9)", result[1].chord)
assertNull(result.getOrNull(2))
}

@Test
fun `GIVEN chord with parenthesis WHEN transposing THEN transposes correctly`() {
val chord = Khord.find("G6(9)").first()
val result = Khord.transposeChord(chord, ChordRoot.C, ChordRoot.Db)
assertEquals("G#6(9)", result)
}

}

0 comments on commit f82d601

Please sign in to comment.