Skip to content

Commit

Permalink
Handle negative ints and doubles
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Nov 19, 2023
1 parent 656f18d commit 0fca5df
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ object DataType {
case object NullData extends DataType

private val isBoolean: Regex = """^(true|false)$""".r
private val isDouble: Regex = """^([0-9]+?)\.([0-9]+)$""".r
private val isInt: Regex = """^([0-9]+)$""".r
private val isInt: Regex = """^(\-?[0-9]+)$""".r
private val isDouble: Regex = """^(\-?[0-9]+?)\.([0-9]+)$""".r
private val isNull: Regex = """^$""".r

def decideType: String => DataType = {
Expand Down Expand Up @@ -276,12 +276,17 @@ final case class DataFrame(data: Array[Array[DataType]], columnCount: Int) {
data.tail

def alignColumnTypes: DataFrame = {
val transposed = rows.transpose
val stringKeys: Array[DataType] = transposed.head.map(_.toStringData)
val typeRows: Array[Array[DataType]] = transposed.tail
val columns =
rows.transpose

val stringKeys: Array[DataType] =
columns.head.map(_.toStringData)

val typedColumns: Array[Array[DataType]] = columns.tail
.map(d => DataType.convertToBestType(d.toList).toArray)

val cleanedRows: Array[Array[DataType]] =
(stringKeys +: typeRows).transpose
(stringKeys +: typedColumns).transpose

this.copy(
data = headers.asInstanceOf[Array[DataType]] +: cleanedRows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class EmbedDataTests extends munit.FunSuite {
List(
EmbedData.extractRowData("name,game,highscore,allowed", ","),
EmbedData.extractRowData("bob,tron,10000.00,true", ","),
EmbedData.extractRowData("Fred,tanks,476,false", ",")
EmbedData.extractRowData("Fred,tanks,476,false", ","),
EmbedData.extractRowData("Stan,,-2,true", ",")
)

val actual =
Expand All @@ -35,6 +36,12 @@ class EmbedDataTests extends munit.FunSuite {
DataType.StringData("tanks"),
DataType.DoubleData(476.0),
DataType.BooleanData(false)
),
List(
DataType.StringData("Stan"),
DataType.NullData,
DataType.DoubleData(-2),
DataType.BooleanData(true)
)
)

Expand All @@ -49,6 +56,7 @@ class EmbedDataTests extends munit.FunSuite {
|enum GameScores(val game: String, val highscore: Double, val allowed: Boolean):
| case Bob extends GameScores("tron", 10000.0, true)
| case Fred extends GameScores("tanks", 476.0, false)
| case Stan extends GameScores(null, -2.0, true)
""".stripMargin

assertEquals(actualEnum.trim, expectedEnum.trim)
Expand All @@ -61,6 +69,7 @@ class EmbedDataTests extends munit.FunSuite {
|enum GameScores(val game: String, val highscore: Double, val allowed: Boolean) extends ScoreData:
| case Bob extends GameScores("tron", 10000.0, true)
| case Fred extends GameScores("tanks", 476.0, false)
| case Stan extends GameScores(null, -2.0, true)
""".stripMargin

assertEquals(actualEnumWithExtends.trim, expectedEnumWithExtends.trim)
Expand All @@ -75,7 +84,8 @@ class EmbedDataTests extends munit.FunSuite {
| val data: Map[String, GameScores] =
| Map(
| "bob" -> GameScores("tron", 10000.0, true),
| "Fred" -> GameScores("tanks", 476.0, false)
| "Fred" -> GameScores("tanks", 476.0, false),
| "Stan" -> GameScores(null, -2.0, true)
| )
""".stripMargin

Expand Down Expand Up @@ -175,4 +185,17 @@ class EmbedDataTests extends munit.FunSuite {
assertEquals(actual, expected)
}

test("decideType - int") {
assertEquals(DataType.decideType("10"), DataType.IntData(10))
assertEquals(DataType.decideType("-10"), DataType.IntData(-10))
}

test("decideType - double") {
assertEquals(DataType.decideType("10.0"), DataType.DoubleData(10.0))
assertEquals(DataType.decideType("-10.0"), DataType.DoubleData(-10.0))
assertEquals(DataType.decideType("-10."), DataType.StringData("-10."))
assertEquals(DataType.decideType(".0"), DataType.StringData(".0"))
assertEquals(DataType.decideType("."), DataType.StringData("."))
}

}

0 comments on commit 0fca5df

Please sign in to comment.