## Coverage Report

#### Created: 2023-09-09 18:04

    .../cutbox_command/Sources/CutBoxCLICore/HistoryManager.swift

    Line

    Count

    Source

    1

    import Foundation

    2

    3

    struct HistoryManager {

    4

        let plist: [String: Any]

    5

        let stringKey = "string"

    6

        let timestampKey = "timestamp"

    7

        let favoriteKey = "favorite"

    8

    9

    23

        init(plist: [String: Any]) {

    10

    23

            self.plist = plist

    11

    23

        }

    12

    13

    23

        func loadHistoryEntries() -> [HistoryEntry] {

    14

    23

            guard let historyDict = plist["historyStore"] as? [[String: Any?]] else {

    15

    1

                return []

    16

    22

            }

    17

    22

    18

    86

            return historyDict.compactMap { itemAsHistoryEntry($0 as [String: Any]) }

    19

    23

        }

    20

    21

    86

        func itemAsHistoryEntry(_ item: [String: Any]) -> HistoryEntry? {

    22

    86

            guard let string: String = item[stringKey] as? String else {

    23

    20

                return nil

    24

    66

            }

    25

    66

    26

    66

            return HistoryEntry(string: string,

    27

    66

                                timestamp: item[timestampKey] as? String,

    28

    66

                                favorite: item[favoriteKey] as? String)

    29

    86

        }

    30

    31

    21

        func filterEntries(_ entries: [HistoryEntry], params: CommandParams) -> [HistoryEntry] {

    32

    21

            var filteredEntries = entries

    33

    21

    34

    21

            if params.favorites {

    35

    3

                filteredEntries = filteredEntries.filter { entry in

    36

    3

                    return entry.favorite == "favorite"

    37

    3

                }

    38

    21

            }

    39

    21

    40

    21

            if params.missingDate {

    41

    3

                filteredEntries = filteredEntries.filter { entry in

    42

    3

                    return entry.timestamp == nil

    43

    3

                }

    44

    21

            }

    45

    21

    46

    21

            if let sinceDate = params.sinceDate {

    47

    12

                filteredEntries = filteredEntries.filter { entry in

    48

    12

                    if entry.timestamp != nil {

    49

    8

                        return Double(entry.timeIntervalSince1970!) >= sinceDate

    50

    8

                    }

    51

    4

                    return false

    52

    12

                }

    53

    21

            }

    54

    21

    55

    21

            if let beforeDate = params.beforeDate {

    56

    8

                filteredEntries = filteredEntries.filter { entry in

    57

    8

                    if entry.timestamp != nil {

    58

    6

                        return Double(entry.timeIntervalSince1970!) <= beforeDate

    59

    6

                    }

    60

    2

                    return false

    61

    8

                }

    62

    21

            }

    63

    21

    64

    21

            if let limit = params.limit {

    65

    2

                filteredEntries = Array(filteredEntries.prefix(limit))

    66

    21

            }

    67

    21

    68

    21

            return filteredEntries

    69

    21

        }

    70

    71

    21

        func searchEntries(_ entries: [HistoryEntry], params: CommandParams) -> [HistoryEntry] {

    72

    21

            guard let query = params.query, let searchMode = params.searchMode else {

    73

    14

                return entries

    74

    14

            }

    75

    7

    76

    21

            return entries.filter { entry in

    77

    21

                switch searchMode {

    78

    21

                case .fuzzy:

    79

    6

                    return entry.string.localizedCaseInsensitiveContains(query)

    80

    21

                case .regex:

    81

    6

                    return regexpMatch(entry.string, query)

    82

    21

                case .regexi:

    83

    3

                    return regexpMatch(entry.string, query, caseSensitive: false)

    84

    21

                case .exact:

    85

    6

                    return entry.string.contains(query)

    86

    21

                }

    87

    21

            }

    88

    21

        }

    89

    }