Skip to content

Commit 5f47860

Browse files
authored
fix: regex faster (#172)
* fix: regex faster * fix: refactor * feat: restrict hashing of .min.js files
1 parent 8fad853 commit 5f47860

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/main/kotlin/app/extractors/Extractor.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Extractor : ExtractorInterface {
1414
val TYPE_KEYWORD = 3
1515
val TYPE_SYNTAX = 4
1616
val SEPARATOR = ">"
17+
val RESTRICTED_EXTS = listOf(".min.js")
1718
}
1819

1920
fun create(extension: String): ExtractorInterface {
@@ -36,6 +37,7 @@ class Extractor : ExtractorInterface {
3637

3738
override fun extract(files: List<DiffFile>): List<CommitStats> {
3839
return files.groupBy { file -> file.extension }
40+
.filter { (extension, _) -> !RESTRICTED_EXTS.contains(extension) }
3941
.map { (extension, files) -> create(extension).extract(files) }
4042
.fold(mutableListOf()) { accStats, stats ->
4143
accStats.addAll(stats)

src/main/kotlin/app/extractors/JavascriptExtractor.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,19 @@ class JavascriptExtractor : ExtractorInterface {
2323
}
2424

2525
override fun extractImports(fileContent: List<String>): List<String> {
26-
val imports = mutableSetOf<String>()
27-
2826
val splitRegex =
2927
Regex("""\s+|,|;|:|\*|\n|\(|\)|\\[|]|\{|}|\+|=|\.|>|<|#|@|\$""")
30-
val twoOrMoreWordsRegex = Regex("""(".+?\s.+?[^"]*"|'.+?\s.+?[^']*')""")
31-
val fileTokens = twoOrMoreWordsRegex.replace(fileContent.joinToString(separator = " ").toLowerCase(), "")
32-
.split(splitRegex)
28+
val twoOrMoreWordsRegex = Regex("""(".+?\s.+?"|'.+?\s.+?')""")
3329

34-
imports.addAll(fileTokens.filter { token -> token in LIBRARIES })
30+
val line = fileContent.joinToString(separator = " ").toLowerCase()
31+
val fileTokens = twoOrMoreWordsRegex.replace(line, "").split(splitRegex)
3532

36-
return imports.toList()
33+
return fileTokens.filter { token -> token in LIBRARIES }.distinct()
3734
}
3835

3936
override fun getLineLibraries(line: String,
4037
fileLibraries: List<String>): List<String> {
41-
42-
return super.getLineLibraries(line, fileLibraries, evaluator, LANGUAGE_NAME)
38+
return super.getLineLibraries(line, fileLibraries, evaluator,
39+
LANGUAGE_NAME)
4340
}
4441
}

src/main/kotlin/app/utils/FileHelper.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ object FileHelper {
1717
private val jarPath = getJarPath()
1818
private val settingsPath = jarPath.resolve(dirName)
1919

20+
private val specificExts = listOf(".min.js")
21+
2022
fun getPath(name: String, vararg parts: String): Path {
2123
val path = settingsPath.resolve(Paths.get("", *parts))
2224
if (Files.notExists(path)) {
@@ -34,7 +36,12 @@ object FileHelper {
3436
}
3537

3638
fun getFileExtension(path: String): String {
37-
val fileName = Paths.get(path).fileName.toString()
39+
val fileName = Paths.get(path).fileName.toString().toLowerCase()
40+
for (ext in specificExts) {
41+
if (fileName.endsWith(ext)) {
42+
return ext
43+
}
44+
}
3845
return fileName.substringAfterLast(delimiter = '.',
3946
missingDelimiterValue = "")
4047
}

0 commit comments

Comments
 (0)