1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-26 07:13:24 +03:00
vimr/VimR/FoundationCommons.swift

104 lines
2.3 KiB
Swift
Raw Normal View History

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Foundation
2016-09-25 18:50:33 +03:00
extension URL {
2016-09-07 21:12:18 +03:00
2016-12-11 21:47:17 +03:00
func isDirectParent(of url: URL) -> Bool {
guard self.isFileURL && url.isFileURL else {
return false
}
let myPathComps = self.pathComponents
let targetPathComps = url.pathComponents
guard targetPathComps.count == myPathComps.count + 1 else {
return false
}
return Array(targetPathComps[0..<myPathComps.count]) == myPathComps
}
2016-10-08 20:58:21 +03:00
func isParent(of url: URL) -> Bool {
2016-09-25 18:50:33 +03:00
guard self.isFileURL && url.isFileURL else {
2016-09-07 21:12:18 +03:00
return false
}
2016-09-25 18:50:33 +03:00
let myPathComps = self.pathComponents
let targetPathComps = url.pathComponents
2016-09-07 21:12:18 +03:00
guard targetPathComps.count > myPathComps.count else {
return false
}
return Array(targetPathComps[0..<myPathComps.count]) == myPathComps
}
2016-11-12 18:42:10 +03:00
func isContained(in parentUrl: URL) -> Bool {
if parentUrl == self {
return false
}
let pathComps = self.pathComponents
let parentPathComps = parentUrl.pathComponents
guard pathComps.count > parentPathComps.count else {
return false
}
guard Array(pathComps[0..<parentPathComps.endIndex]) == parentPathComps else {
return false
}
return true
}
2016-11-12 18:42:10 +03:00
var parent: URL {
if self.path == "/" {
return self
}
return self.deletingLastPathComponent()
}
/// Wrapper function for NSURL.getResourceValue for Bool values.
/// Returns also `false` when
/// - there is no value for the given `key` or
/// - the value cannot be converted to `NSNumber`.
///
/// - parameters:
/// - key: The `key`-parameter of `NSURL.getResourceValue`.
2016-09-25 18:50:33 +03:00
func resourceValue(_ key: String) -> Bool {
var rsrc: AnyObject?
do {
2016-09-25 18:50:33 +03:00
try (self as NSURL).getResourceValue(&rsrc, forKey: URLResourceKey(rawValue: key))
} catch let error as NSError {
// FIXME error handling
print("\(#function): \(self) -> ERROR while getting \(key): \(error)")
return false
}
if let result = rsrc as? NSNumber {
return result.boolValue
}
return false
}
2016-11-12 18:42:10 +03:00
var isDir: Bool {
2016-09-25 18:50:33 +03:00
return self.resourceValue(URLResourceKey.isDirectoryKey.rawValue)
}
2016-11-12 18:42:10 +03:00
var isHidden: Bool {
2016-09-25 18:50:33 +03:00
return self.resourceValue(URLResourceKey.isHiddenKey.rawValue)
}
2016-11-12 18:42:10 +03:00
var isPackage: Bool {
2016-09-25 18:50:33 +03:00
return self.resourceValue(URLResourceKey.isPackageKey.rawValue)
}
}