CutBox CLI

Coverage Report

Created: 2024-03-12 03:43

.../Sources/CutBoxCLICore/CutBoxCLICoreHelpers.swift
Line
Count
Source (jump to first uncovered line)
1
//
2
//  CutBoxCLICoreHelpers.swift
3
//
4
//  Created by jason on 1/9/23.
5
//
6
7
import Foundation
8
9
402
public func regexpMatch(_ string: String, _ pattern: String, caseSensitive: Bool = true) -> Bool {
10
402
    let range = NSRange(location: 0, length: string.utf16.count)
11
402
    if caseSensitive {
12
9
        if let regex = try? NSRegularExpression(pattern: pattern) {
13
5
            return regex.firstMatch(in: string, options: [], range: range) != nil
14
5
        }
15
397
    } else {
16
393
        let regexOptions: NSRegularExpression.Options = [.caseInsensitive]
17
393
        if let regex = try? NSRegularExpression(pattern: pattern, options: regexOptions) {
18
393
            return regex.firstMatch(in: string, options: [], range: range) != nil
19
393
        }
20
4
    }
21
4
    return false
22
397
}
23
24
18
public func iso8601() -> DateFormatter {
25
18
    let dateFormatter = DateFormatter()
26
18
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
27
18
    return dateFormatter
28
18
}
29
30
4
public func loadPlist(path: String) -> [String: Any] {
31
4
    guard let data = FileManager.default.contents(atPath: path),
32
4
          let plist = try? PropertyListSerialization.propertyList(
33
4
            from: data,
34
4
            options: [],
35
4
            format: nil) as? [String: Any] else {
36
1
        fatalError("Cannot read file at \(path)")
37
4
    }
38
4
    return plist
39
4
}
40
41
public func savePlist(path: String,
42
                      plist: [String: Any],
43
1
                      format: PropertyListSerialization.PropertyListFormat = .binary) {
44
1
    guard let data = try? PropertyListSerialization.data(
45
1
        fromPropertyList: plist,
46
1
        format: format,
47
1
        options: 0
48
1
    ) else {
49
0
        fatalError("Cannot convert plist to data")
50
1
    }
51
1
52
1
    do {
53
1
        try data.write(to: URL(fileURLWithPath: path))
54
1
    } catch {
55
0
        fatalError("Failed to write data to file: \(error)")
56
1
    }
57
1
}