diff --git a/MemoryChainKit/Sources/Extensions/AVFoundation/AVAssetExtension.swift b/MemoryChainKit/Sources/Extensions/AVFoundation/AVAssetExtension.swift index 08eaf5a..a84ceb2 100644 --- a/MemoryChainKit/Sources/Extensions/AVFoundation/AVAssetExtension.swift +++ b/MemoryChainKit/Sources/Extensions/AVFoundation/AVAssetExtension.swift @@ -73,30 +73,4 @@ public extension AVAsset { return isPortrait } } -import Foundation -import UIKit - -extension PHAsset { - var originalFilename: NSString { - if let resource = PHAssetResource.assetResources(for: self).first { - return resource.originalFilename as NSString - } else { - return self.value(forKey: "filename") as? NSString - ?? ("IMG_" + CCUtility.getIncrementalNumber() + getExtension()) as NSString - } - } - - private func getExtension() -> String { - switch mediaType { - case .video: - return ".mp4" - case .image: - return ".jpg" - case .audio: - return ".mp3" - default: - return ".unknownType" - } - } -} diff --git a/MemoryChainKit/Sources/Extensions/CoreData/NSManagedObjectExtension.swift b/MemoryChainKit/Sources/Extensions/CoreData/NSManagedObjectExtension.swift index 08a54f5..b8d356c 100644 --- a/MemoryChainKit/Sources/Extensions/CoreData/NSManagedObjectExtension.swift +++ b/MemoryChainKit/Sources/Extensions/CoreData/NSManagedObjectExtension.swift @@ -21,4 +21,15 @@ public extension NSManagedObject { didAccessValue(forKey: key) return result.flatMap({ValueType(rawValue: $0)}) } + + /// Lookup an object by its NSManagedObjectID + /// + /// - Parameters: + /// - objectID: The `NSManagedObject` subclass' objectID as defined by Core Data. + /// - context: An NSManagedObjectContext that contains the associated object. + /// - Returns: The `NSManagedObject` subclass associated with the given `objectID`, if it exists. + static func lookup(withObjectID objectID: NSManagedObjectID, in context: NSManagedObjectContext) -> Self? { + return try? context.existingObject(with: objectID) as? Self + } + } diff --git a/MemoryChainKit/Sources/Extensions/Foudantion/NotificationCenterExtension.swift b/MemoryChainKit/Sources/Extensions/Foudantion/NotificationCenterExtension.swift index 62810e9..d61a61f 100644 --- a/MemoryChainKit/Sources/Extensions/Foudantion/NotificationCenterExtension.swift +++ b/MemoryChainKit/Sources/Extensions/Foudantion/NotificationCenterExtension.swift @@ -8,13 +8,7 @@ import UIKit import Foundation -public extension NotificationationCenter { -func postOnMainThread(name: String, object anObject: Any? = nil, userInfo aUserInfo: [AnyHashable: Any]? = nil, second: Double = 0) { - DispatchQueue.main.asyncAfter(deadline: .now() + second) { - NotificationCenter.default.post(name: Notification.Name(rawValue: name), object: anObject, userInfo: aUserInfo) - } - } - } + public extension NotificationCenter { func postOnMainThread(name: String, object anObject: Any? = nil, userInfo aUserInfo: [AnyHashable : Any]? = nil) { // if UIApplication.shared.applicationState == .background { diff --git a/MemoryChainKit/Sources/Extensions/Foudantion/StringExtension.swift b/MemoryChainKit/Sources/Extensions/Foudantion/StringExtension.swift index 98bd311..70212a7 100644 --- a/MemoryChainKit/Sources/Extensions/Foudantion/StringExtension.swift +++ b/MemoryChainKit/Sources/Extensions/Foudantion/StringExtension.swift @@ -831,4 +831,78 @@ public extension String { guard let message = data(using: .utf8) else { return nil } return message.hashed(type, output: output) } + + /// + /// Attempts to remove excessive whitespace in text by replacing multiple new lines with just 2. + /// This first trims whitespace and newlines from the ends + /// Then normalizes the newlines by replacing {Space}{Newline} with a single newline char + /// Then finally it looks for any newlines that are 3 or more and replaces them with 2 newlines. + /// + /// Example: + /// ``` + /// This is the first line + /// + /// + /// + /// + /// This is the last line + /// ``` + /// Turns into: + /// ``` + /// This is the first line + /// + /// This is the last line + /// ``` + /// + func condenseWhitespace() -> String { + return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) + .replacingOccurrences(of: "\\s\n", with: "\n", options: .regularExpression, range: nil) + .replacingOccurrences(of: "[\n]{3,}", with: "\n\n", options: .regularExpression, range: nil) + } +} + +//MARK: - regex +public extension String { + + /// Find all matches of the specified regex. + /// + /// - Parameters: + /// - regex: the regex to use. + /// - options: the regex options. + /// + /// - Returns: the requested matches. + /// + func matches(regex: String, options: NSRegularExpression.Options = []) -> [NSTextCheckingResult] { + let regex = try! NSRegularExpression(pattern: regex, options: options) + let fullRange = NSRange(location: 0, length: count) + + return regex.matches(in: self, options: [], range: fullRange) + } + + /// Replaces all matches of a given RegEx, with a template String. + /// + /// - Parameters: + /// - regex: the regex to use. + /// - template: the template string to use for the replacement. + /// - options: the regex options. + /// + /// - Returns: a new string after replacing all matches with the specified template. + /// + func replacingMatches(of regex: String, with template: String, options: NSRegularExpression.Options = []) -> String { + + let regex = try! NSRegularExpression(pattern: regex, options: options) + let fullRange = NSRange(location: 0, length: count) + + return regex.stringByReplacingMatches(in: self, + options: [], + range: fullRange, + withTemplate: template) + } + + + /// Returns a NSRange instance starting at position 0, with the entire String's Length + /// + var foundationRangeOfEntireString: NSRange { + return NSRange(location: 0, length: utf16.count) + } }