<commit_before>import Foundation

// MARK: - File

/// A simple typealias for strings representing files
public typealias File = String

extension File {

    public var fileType: FileType? {
        return FileType(from: self)
    }

}

// MARK: - FileType

public enum FileType: String {
    case h, json, m, markdown = "md", pbxproj, plist, storyboard, swift

    @available(swift, deprecated: 4.2, message: "Replace with CaseIterable conformance")
    static internal var allCases: [FileType] {
        return [.h, .json, .m, .markdown, .pbxproj, .plist, .storyboard, .swift]
    }
}

// MARK: - Public extensions

extension FileType {

    public var `extension`: String {
        return rawValue
    }

    public init?(from fileName: String) {
        let allCasesDelimited = FileType.allCases.map { $0.extension }.joined(separator: "|")

        // TODO: move ugly regex stuff to tested extension where it belongs, write tests
        guard
            let regex = try? NSRegularExpression(pattern: "\\.(\(allCasesDelimited))$", options: []),
            let match = regex.firstMatch(in: fileName, options: [], range: NSRange(location: 0, length: fileName.count)),
            let range = Range(match.range, in: fileName)
        else {
            return nil
        }

        let rawValue = String(fileName[range])
        self.init(rawValue: rawValue)<commit_msg>Refactor FileType.init(from fileName:) w/ regex extension<commit_after>import Foundation

// MARK: - File

/// A simple typealias for strings representing files
public typealias File = String

extension File {

    public var fileType: FileType? {
        return FileType(from: self)
    }

}

// MARK: - FileType

public enum FileType: String {
    case h, json, m, markdown = "md", pbxproj, plist, storyboard, swift

    @available(swift, deprecated: 4.2, message: "Replace with CaseIterable conformance")
    static internal var allCases: [FileType] {
        return [.h, .json, .m, .markdown, .pbxproj, .plist, .storyboard, .swift]
    }
}

// MARK: - Public extensions

extension FileType {

    public var `extension`: String {
        return rawValue
    }

    public init?(from fileName: String) {
        let allCasesDelimited = FileType.allCases.map { $0.extension }.joined(separator: "|")

        guard
            let pattern = try? NSRegularExpression(pattern: "\\.(\(allCasesDelimited))$"),
            let rawValue = pattern.firstMatchingString(in: fileName)
        else {
            return nil
        }

        self.init(rawValue: rawValue)
{'fraction_non_alphanumeric': 0.11391407173827355, 'fraction_numerical': 0.0027591643673630273, 'mean_word_length': 2.7159590043923867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3710659', 'n_tokens_mistral': 778, 'n_tokens_neox': 738, 'n_words': 226}