// // Foundation+Extensions.swift // KissMeConsole // // Created by ened-book-m1 on 2023/05/26. // import Foundation import KissMe extension String { func maxSpace(_ length: Int) -> String { let count = unicodeScalars.reduce(0) { $0 + ($1.value >= 0x80 ? 2: 1) } if count < length { return appending(String(repeating: " ", count: length - count)) } return self } func maxSpace(_ length: Int, digitBy: Int) -> String { guard let number = Int(self) else { return self } // Add comma let formatter = NumberFormatter() formatter.numberStyle = .decimal guard let str = formatter.string(from: NSNumber(value: number)) else { return self } // Add space let count = str.count if (count + count / 3) <= length { return str.appending(String(repeating: " ", count: length - count)) } return self } } extension String { func parseCandleDate() -> String? { let fileNameFrag = split(separator: ".") guard fileNameFrag.count == 2 else { return nil } let candlePrefix = "candle-" guard fileNameFrag[0].prefix(candlePrefix.count) == candlePrefix, fileNameFrag[1] == "csv" else { return nil } let fileDateFrag = fileNameFrag[0].suffix(fileNameFrag[0].count - candlePrefix.count) return String(fileDateFrag) } } extension FileManager { /// period: If nil, all period of csv collected. /// candleDate: If nil, all date of csv collected. /// static func collectCsv(period: KissConsole.CandleFilePeriod?, candleDate: Date?) throws -> [URL] { guard let enumerator = FileManager.subPathFiles("data") else { throw GeneralError.noCsvFile } var csvUrls = [URL]() for case let fileUrl as URL in enumerator { guard fileUrl.pathExtension == "csv" else { continue } let fileName = fileUrl.lastPathComponent let periodDir = fileUrl.deletingLastPathComponent() let periodAtPath = periodDir.lastPathComponent let productNoDir = periodDir.deletingLastPathComponent() let productNo = productNoDir.lastPathComponent guard let _ = Int(productNo) else { continue } if let period = period { guard periodAtPath == period.rawValue else { continue } } if let candleDate = candleDate { guard candleDate.yyyyMMdd == fileName.parseCandleDate() else { continue } } csvUrls.append(fileUrl) } return csvUrls } static func collectShorts() throws -> [URL] { guard let enumerator = FileManager.subPathFiles("data") else { throw GeneralError.noCsvFile } var csvUrls = [URL]() for case let fileUrl as URL in enumerator { guard fileUrl.pathExtension == "csv" else { continue } //let fileName = fileUrl.lastPathComponent let categoryDir = fileUrl.deletingLastPathComponent() let categoryAtPath = categoryDir.lastPathComponent let productNoDir = categoryDir.deletingLastPathComponent() let productNo = productNoDir.lastPathComponent guard let _ = Int(productNo) else { continue } guard categoryAtPath == "shorts" else { continue } csvUrls.append(fileUrl) } return csvUrls } }