-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #648 from jenspav/string-normalize
Normalize file names even more
- Loading branch information
Showing
3 changed files
with
52 additions
and
10 deletions.
There are no files selected for viewing
20 changes: 19 additions & 1 deletion
20
src/main/kotlin/nl/avisi/structurizr/site/generatr/StringUtilities.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StringUtilitiesTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters