~askonomm/farm

8dc5d55e3376ecefb0cb4100b904be3b0d8f06a5 — Asko N├Ámm 2 months ago 0c66ef5 master v1.0.4
Fix an issue with index out of range if files not found, and the relative time is a shit so remove it
1 files changed, 10 insertions(+), 67 deletions(-)

M Sources/Farm/Farm.swift
M Sources/Farm/Farm.swift => Sources/Farm/Farm.swift +10 -67
@@ 1,6 1,6 @@
import Foundation
import Yams
import Ink
import Foundation

public struct FarmItem: Codable {
    public var meta: [String: String]


@@ 91,7 91,6 @@ public struct Farm {
                                        let dateFormatter = DateFormatter()
                                        dateFormatter.dateFormat = "yyyy-MM-dd"
                                        metadata[yamlDataItem.key] = dateFormatter.string(from: date!)
                                        metadata["relativeDate"] = relativeTime(date!)
                                    }
                                } else {
                                    metadata[yamlDataItem.key] = yamlDataItem.value as? String


@@ 108,68 107,7 @@ public struct Farm {
        
        return items
    }
    
    private func relativeTime(_ date: Date) -> String {
        let calendar = Calendar.current
        let now = Date()
        let unitFlags: NSCalendar.Unit = [.second, .minute, .hour, .day, .weekOfYear, .month, .year]
        let components = (calendar as NSCalendar).components(unitFlags, from: date, to: now, options: [])
        
        if let year = components.year, year >= 2 {
            return "\(year) years ago"
        }
        
        if let year = components.year, year >= 1 {
            return "Last year"
        }
        
        if let month = components.month, month >= 2 {
            return "\(month) months ago"
        }
        
        if let month = components.month, month >= 1 {
            return "Last month"
        }
        
        if let week = components.weekOfYear, week >= 2 {
            return "\(week) weeks ago"
        }
        
        if let week = components.weekOfYear, week >= 1 {
            return "Last week"
        }
        
        if let day = components.day, day >= 2 {
            return "\(day) days ago"
        }
        
        if let day = components.day, day >= 1 {
            return "Yesterday"
        }
        
        if let hour = components.hour, hour >= 2 {
            return "\(hour) hours ago"
        }
        
        if let hour = components.hour, hour >= 1 {
            return "An hour ago"
        }
        
        if let minute = components.minute, minute >= 2 {
            return "\(minute) minutes ago"
        }
        
        if let minute = components.minute, minute >= 1 {
            return "A minute ago"
        }
        
        if let second = components.second, second >= 3 {
            return "\(second) seconds ago"
        }
        
        return "Just now"
    }
    

    /**
     * Sorts content by meta key, in descending order.
     */


@@ 218,9 156,14 @@ public struct Farm {
    public func get(key: String, value: String) -> FarmItem? {
        let files = self.getFiles()
        let parsedFiles = self.parseFiles(files: files)
        
        return parsedFiles.filter {
        let filteredFiles = parsedFiles.filter {
            $0.meta[key] != nil && $0.meta[key] == value
        }[0]
        }
        
        if filteredFiles.isEmpty {
            return nil
        } else {
            return filteredFiles[0]
        }
    }
}