Skip to content

Commit

Permalink
Exclude more reserved file name characters
Browse files Browse the repository at this point in the history
  • Loading branch information
jenspav committed Dec 2, 2024
1 parent ee2bcca commit 3681e44
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
package nl.avisi.structurizr.site.generatr

fun String.normalize(): String = lowercase().replace("\\s+".toRegex(), "-")
// based on https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names
private const val reservedChars = "|\\?*<\":>+[]/'"
private val reservedNames = setOf(
"CON", "PRN", "AUX", "NUL",
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"
)

fun String.normalize(): String =
lowercase()
.replace("\\s+".toRegex(), "-")
.filterNot { reservedChars.contains(it) }
.trim()
.let {
if (reservedNames.contains(it.uppercase()))
"${it}-"
else
it
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nl.avisi.structurizr.site.generatr.site

import assertk.assertThat
import assertk.assertions.isEqualTo
import nl.avisi.structurizr.site.generatr.normalize
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.TestFactory

class StringUtilitiesTest {

@TestFactory
fun `normalize strips invalid chars`() = listOf(
listOf("doc", "doc"),
listOf("d c", "d-c"),
listOf("doc:", "doc"),
listOf(" doc ", "-doc-"),
listOf("aux", "aux-"),
).map { (actual, expected) ->
DynamicTest.dynamicTest("normalize replaces $actual with $expected") {
assertThat(actual.normalize()).isEqualTo(expected)
}
}
}

0 comments on commit 3681e44

Please sign in to comment.