Files
KissMe/KissMeConsole/Sources/Foundation+Extensions.swift

144 lines
4.1 KiB
Swift
Raw Normal View History

2023-05-26 21:59:13 +09:00
//
2023-05-29 15:49:39 +09:00
// Foundation+Extensions.swift
2023-05-26 21:59:13 +09:00
// KissMeConsole
//
// Created by ened-book-m1 on 2023/05/26.
//
import Foundation
2023-06-03 07:56:55 +09:00
import KissMe
2023-05-26 21:59:13 +09:00
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
}
}
2023-05-28 16:08:49 +09:00
2023-06-08 23:41:57 +09:00
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)
}
2024-11-11 23:17:39 +09:00
var isYear: Bool {
guard let year = Int(self), (1000...9999).contains(year) else {
return false
}
return true
}
var isMonth: Bool {
guard let month = Int(self), (1...12).contains(month) else {
return false
}
return true
}
var isDay: Bool {
guard let day = Int(self), (1...31).contains(day) else {
return false
}
return true
}
2023-06-08 23:41:57 +09:00
}
2023-06-08 22:54:26 +09:00
extension FileManager {
2023-06-11 12:39:39 +09:00
/// 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
}
2023-06-29 20:20:16 +09:00
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
}
2023-06-08 22:54:26 +09:00
}