diff --git a/Package.swift b/Package.swift index addda0d..26f7c75 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.7 +// swift-tools-version:5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription diff --git a/Sources/SwiftFrameCore/Config/ConfigData.swift b/Sources/SwiftFrameCore/Config/ConfigData.swift index 13b2608..abb602f 100644 --- a/Sources/SwiftFrameCore/Config/ConfigData.swift +++ b/Sources/SwiftFrameCore/Config/ConfigData.swift @@ -67,11 +67,10 @@ struct ConfigData: Decodable, ConfigValidateable { // MARK: - Processing mutating func process() throws { - let regex: NSRegularExpression? - if let localesRegex, !localesRegex.isEmpty { - regex = try NSRegularExpression(pattern: localesRegex) + let regex: Regex? = if let localesRegex, !localesRegex.isEmpty { + try Regex(localesRegex) } else { - regex = nil + nil } deviceData = try deviceData.map { try $0.makeProcessedData(localesRegex: regex) } diff --git a/Sources/SwiftFrameCore/Config/DeviceData.swift b/Sources/SwiftFrameCore/Config/DeviceData.swift index a54173d..0110563 100644 --- a/Sources/SwiftFrameCore/Config/DeviceData.swift +++ b/Sources/SwiftFrameCore/Config/DeviceData.swift @@ -57,7 +57,7 @@ struct DeviceData: Decodable, ConfigValidateable { // MARK: - Methods - func makeProcessedData(localesRegex: NSRegularExpression?) throws -> DeviceData { + func makeProcessedData(localesRegex: Regex?) throws -> DeviceData { guard let templateImage = ImageLoader.loadRepresentation(at: templateImagePath.absoluteURL) else { throw NSError(description: "Error while loading template image at path \(templateImagePath.absoluteString)") } diff --git a/Sources/SwiftFrameCore/Extensions/Collection+Extensions.swift b/Sources/SwiftFrameCore/Extensions/Collection+Extensions.swift index d33acd2..ad27e50 100644 --- a/Sources/SwiftFrameCore/Extensions/Collection+Extensions.swift +++ b/Sources/SwiftFrameCore/Extensions/Collection+Extensions.swift @@ -1,24 +1,15 @@ import Foundation -extension Array where Element == URL { +extension [URL] { - func filterByFileOrFoldername(regex: NSRegularExpression?) throws -> Self { + func filterByFileOrFoldername(regex: Regex?) throws -> Self { guard let regex else { return self } return self.filter { url in let lastComponent = url.deletingPathExtension().lastPathComponent - return regex.matches(lastComponent) + return !lastComponent.matches(of: regex).isEmpty } } } - -extension NSRegularExpression { - - func matches(_ string: String) -> Bool { - let range = NSRange(location: 0, length: (string as NSString).length) - return firstMatch(in: string, options: [], range: range) != nil - } - -} diff --git a/Tests/SwiftFrameTests/RegexMatchTests.swift b/Tests/SwiftFrameTests/RegexMatchTests.swift index 1d55447..11fab52 100644 --- a/Tests/SwiftFrameTests/RegexMatchTests.swift +++ b/Tests/SwiftFrameTests/RegexMatchTests.swift @@ -4,52 +4,50 @@ import XCTest class RegexMatchTests: XCTestCase { - static let urls: [URL] = { - [ - URL(fileURLWithPath: "strings/en.strings"), - URL(fileURLWithPath: "strings/de.strings"), - URL(fileURLWithPath: "strings/fr.strings"), - URL(fileURLWithPath: "strings/ru.strings") - ] - }() + private let urls: [URL] = [ + URL(fileURLWithPath: "strings/en.strings"), + URL(fileURLWithPath: "strings/de.strings"), + URL(fileURLWithPath: "strings/fr.strings"), + URL(fileURLWithPath: "strings/ru.strings") + ] func testAllURLs() throws { - let urls = try RegexMatchTests.urls.filterByFileOrFoldername(regex: nil) - XCTAssertEqual(urls, RegexMatchTests.urls) + let filteredURLs = try urls.filterByFileOrFoldername(regex: nil) + XCTAssertEqual(filteredURLs, urls) } func testFranceFilteredOut() throws { - let regex = try NSRegularExpression(pattern: "^(?!fr$)\\w*$", options: .caseInsensitive) - let urls = try RegexMatchTests.urls.filterByFileOrFoldername(regex: regex) + let regex = try Regex("^(?!fr$)\\w*$") + let filteredURLs = try urls.filterByFileOrFoldername(regex: regex) - guard ky_assertEqual(urls.count, 3) else { + guard ky_assertEqual(filteredURLs.count, 3) else { return } - XCTAssertTrue(urls[0].absoluteString.hasSuffix("en.strings")) - XCTAssertTrue(urls[1].absoluteString.hasSuffix("de.strings")) - XCTAssertTrue(urls[2].absoluteString.hasSuffix("ru.strings")) + XCTAssertTrue(filteredURLs[0].absoluteString.hasSuffix("en.strings")) + XCTAssertTrue(filteredURLs[1].absoluteString.hasSuffix("de.strings")) + XCTAssertTrue(filteredURLs[2].absoluteString.hasSuffix("ru.strings")) } func testFranceAndRussiaFilteredOut() throws { - let regex = try NSRegularExpression(pattern: "^(?!fr|ru$)\\w*$", options: .caseInsensitive) - let urls = try RegexMatchTests.urls.filterByFileOrFoldername(regex: regex) + let regex = try Regex("^(?!fr|ru$)\\w*$") + let filteredURLs = try urls.filterByFileOrFoldername(regex: regex) - guard ky_assertEqual(urls.count, 2) else { + guard ky_assertEqual(filteredURLs.count, 2) else { return } - XCTAssertTrue(urls[0].absoluteString.hasSuffix("en.strings")) - XCTAssertTrue(urls[1].absoluteString.hasSuffix("de.strings")) + XCTAssertTrue(filteredURLs[0].absoluteString.hasSuffix("en.strings")) + XCTAssertTrue(filteredURLs[1].absoluteString.hasSuffix("de.strings")) } func testOnlyRussiaAndFrance() throws { - let regex = try NSRegularExpression(pattern: "ru|fr", options: .caseInsensitive) - let urls = try RegexMatchTests.urls.filterByFileOrFoldername(regex: regex) + let regex = try Regex("ru|fr") + let filteredURLs = try urls.filterByFileOrFoldername(regex: regex) - guard ky_assertEqual(urls.count, 2) else { + guard ky_assertEqual(filteredURLs.count, 2) else { return } - XCTAssertEqual(urls[0].lastPathComponent, "fr.strings") - XCTAssertEqual(urls[1].lastPathComponent, "ru.strings") + XCTAssertEqual(filteredURLs[0].lastPathComponent, "fr.strings") + XCTAssertEqual(filteredURLs[1].lastPathComponent, "ru.strings") } }